Account.jsx 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import UserInfo from '../component/Account/UserInfo.jsx'
  4. import MenuSubComponent from '../component/Account/MenuSubComponent.jsx'
  5. import PersonalData from '../component/Account/PersonalData.jsx'
  6. // import Calendar from '../component/Account/Calendar.jsx'
  7. import Notification from '../component/Account/Notification.jsx'
  8. import Password from '../component/Account/Password.jsx'
  9. import Timezone from '../component/Account/Timezone.jsx'
  10. import {
  11. Delimiter,
  12. PageWrapper,
  13. PageTitle,
  14. PageContent
  15. } from 'tracim_frontend_lib'
  16. import {
  17. newFlashMessage,
  18. setWorkspaceListMemberList,
  19. updateUserName,
  20. updateUserEmail,
  21. updateUserAuth,
  22. updateUserWorkspaceSubscriptionNotif
  23. } from '../action-creator.sync.js'
  24. import {
  25. getWorkspaceMemberList,
  26. putUserName,
  27. putUserEmail,
  28. putUserPassword,
  29. putUserWorkspaceDoNotify
  30. } from '../action-creator.async.js'
  31. import { translate } from 'react-i18next'
  32. import { setCookie } from '../helper.js'
  33. class Account extends React.Component {
  34. constructor (props) {
  35. super(props)
  36. this.state = {
  37. subComponentMenu: [{
  38. name: 'personalData',
  39. menuLabel: props.t('My profil'),
  40. active: true
  41. }, {
  42. name: 'notification',
  43. menuLabel: props.t('Workspaces and notifications'),
  44. active: false
  45. }, {
  46. name: 'password',
  47. menuLabel: props.t('Password'),
  48. active: false
  49. }, {
  50. name: 'timezone',
  51. menuLabel: props.t('Timezone'),
  52. active: false
  53. }]
  54. // {
  55. // name: 'calendar',
  56. // menuLabel: 'Calendrier personnel',
  57. // active: false
  58. // }]
  59. }
  60. }
  61. componentDidMount () {
  62. const { props } = this
  63. if (props.system.workspaceListLoaded && props.workspaceList.length > 0) this.loadWorkspaceListMemberList()
  64. }
  65. loadWorkspaceListMemberList = async () => {
  66. const { props } = this
  67. const fetchWorkspaceListMemberList = await Promise.all(
  68. props.workspaceList.map(async ws => ({
  69. idWorkspace: ws.id,
  70. fetchMemberList: await props.dispatch(getWorkspaceMemberList(props.user, ws.id))
  71. }))
  72. )
  73. const workspaceListMemberList = fetchWorkspaceListMemberList.map(wsMemberList => ({
  74. idWorkspace: wsMemberList.idWorkspace,
  75. memberList: wsMemberList.fetchMemberList.status === 200
  76. ? wsMemberList.fetchMemberList.json
  77. : [] // handle error ?
  78. }))
  79. props.dispatch(setWorkspaceListMemberList(workspaceListMemberList))
  80. }
  81. handleClickSubComponentMenuItem = subMenuItemName => this.setState(prev => ({
  82. subComponentMenu: prev.subComponentMenu.map(m => ({...m, active: m.name === subMenuItemName}))
  83. }))
  84. handleSubmitNameOrEmail = async (newName, newEmail, checkPassword) => {
  85. const { props } = this
  86. if (newName !== '') {
  87. const fetchPutUserName = await props.dispatch(putUserName(props.user, newName))
  88. switch (fetchPutUserName.status) {
  89. case 200:
  90. props.dispatch(updateUserName(newName))
  91. if (newEmail === '') props.dispatch(newFlashMessage(props.t('Your name has been changed'), 'info'))
  92. // else, if email also has been changed, flash msg is handled bellow to not display 2 flash msg
  93. break
  94. default: props.dispatch(newFlashMessage(props.t('Error while changing name'), 'warning')); break
  95. }
  96. }
  97. if (newEmail !== '') {
  98. const fetchPutUserEmail = await props.dispatch(putUserEmail(props.user, newEmail, checkPassword))
  99. switch (fetchPutUserEmail.status) {
  100. case 200:
  101. props.dispatch(updateUserEmail(fetchPutUserEmail.json.email))
  102. const newAuth = setCookie(fetchPutUserEmail.json.email, checkPassword)
  103. props.dispatch(updateUserAuth(newAuth))
  104. if (newName !== '') props.dispatch(newFlashMessage(props.t('Your name and email has been changed'), 'info'))
  105. else props.dispatch(newFlashMessage(props.t('Your email has been changed'), 'info'))
  106. break
  107. default: props.dispatch(newFlashMessage(props.t('Error while changing email'), 'warning')); break
  108. }
  109. }
  110. }
  111. handleChangeSubscriptionNotif = async (idWorkspace, doNotify) => {
  112. const { props } = this
  113. const fetchPutUserWorkspaceDoNotify = await props.dispatch(putUserWorkspaceDoNotify(props.user, idWorkspace, doNotify))
  114. switch (fetchPutUserWorkspaceDoNotify.status) {
  115. // @TODO: Côme - 2018/08/23 - uncomment this when fetch implements the right endpoint (blocked by backend)
  116. // case 200:
  117. // break
  118. default:
  119. props.dispatch(updateUserWorkspaceSubscriptionNotif(props.user.user_id, idWorkspace, doNotify))
  120. // props.dispatch(newFlashMessage(props.t('Error while changing subscription'), 'warning'))
  121. break
  122. }
  123. }
  124. handleSubmitPassword = async (oldPassword, newPassword, newPassword2) => {
  125. const { props } = this
  126. const fetchPutUserPassword = await props.dispatch(putUserPassword(props.user, oldPassword, newPassword, newPassword2))
  127. switch (fetchPutUserPassword.status) {
  128. case 204:
  129. const newAuth = setCookie(props.user.email, newPassword)
  130. props.dispatch(updateUserAuth(newAuth))
  131. props.dispatch(newFlashMessage(props.t('Your password has been changed'), 'info'))
  132. break
  133. default: props.dispatch(newFlashMessage(props.t('Error while changing password'), 'warning')); break
  134. }
  135. }
  136. handleChangeTimezone = newTimezone => console.log('(NYI) new timezone : ', newTimezone)
  137. render () {
  138. const { props, state } = this
  139. const subComponent = (() => {
  140. switch (state.subComponentMenu.find(({active}) => active).name) {
  141. case 'personalData':
  142. return <PersonalData onClickSubmit={this.handleSubmitNameOrEmail} />
  143. // case 'calendar':
  144. // return <Calendar user={props.user} />
  145. case 'notification':
  146. return <Notification
  147. idMyself={props.user.user_id}
  148. workspaceList={props.workspaceList}
  149. onChangeSubscriptionNotif={this.handleChangeSubscriptionNotif}
  150. />
  151. case 'password':
  152. return <Password onClickSubmit={this.handleSubmitPassword} />
  153. case 'timezone':
  154. return <Timezone timezone={props.timezone} onChangeTimezone={this.handleChangeTimezone} />
  155. }
  156. })()
  157. return (
  158. <PageWrapper customClass='account'>
  159. <PageTitle
  160. parentClass={'account'}
  161. title={props.t('My account')}
  162. />
  163. <PageContent parentClass='account'>
  164. <UserInfo user={props.user} />
  165. <Delimiter customClass={'account__delimiter'} />
  166. <div className='account__userpreference'>
  167. <MenuSubComponent
  168. subMenuList={state.subComponentMenu}
  169. onClickMenuItem={this.handleClickSubComponentMenuItem}
  170. />
  171. <div className='account__userpreference__setting'>
  172. { subComponent }
  173. </div>
  174. </div>
  175. </PageContent>
  176. </PageWrapper>
  177. )
  178. }
  179. }
  180. const mapStateToProps = ({ user, workspaceList, timezone, system }) => ({ user, workspaceList, timezone, system })
  181. export default connect(mapStateToProps)(translate()(Account))