AdminWorkspaceUser.jsx 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import React from 'react'
  2. import { translate } from 'react-i18next'
  3. import i18n from '../i18n.js'
  4. import {
  5. addAllResourceI18n,
  6. handleFetchResult,
  7. CardPopup
  8. } from 'tracim_frontend_lib'
  9. import { debug } from '../helper.js'
  10. import {
  11. getWorkspaceList,
  12. getWorkspaceMemberList,
  13. deleteWorkspace,
  14. getUserList,
  15. getUserDetail,
  16. putUserDisable,
  17. putUserEnable,
  18. putUserProfile,
  19. postAddUser
  20. } from '../action.async.js'
  21. import AdminWorkspace from '../component/AdminWorkspace.jsx'
  22. import AdminUser from '../component/AdminUser.jsx'
  23. require('../css/index.styl')
  24. class AdminWorkspaceUser extends React.Component {
  25. constructor (props) {
  26. super(props)
  27. this.state = {
  28. appName: 'admin_workspace_user',
  29. isVisible: true,
  30. config: props.data ? props.data.config : debug.config,
  31. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  32. content: props.data ? props.data.content : debug.content,
  33. popupDeleteWorkspaceDisplay: false,
  34. workspaceToDelete: null
  35. }
  36. // i18n has been init, add resources from frontend
  37. addAllResourceI18n(i18n, this.state.config.translation)
  38. i18n.changeLanguage(this.state.loggedUser.lang)
  39. document.addEventListener('appCustomEvent', this.customEventReducer)
  40. }
  41. customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
  42. switch (type) {
  43. case 'admin_workspace_user_showApp':
  44. console.log('%c<AdminWorkspaceUser> Custom event', 'color: #28a745', type, data)
  45. this.setState({config: data.config})
  46. break
  47. default:
  48. break
  49. }
  50. }
  51. componentDidMount () {
  52. console.log('%c<AdminWorkspaceUser> did mount', `color: ${this.state.config.hexcolor}`)
  53. if (this.state.config.type === 'workspace') this.loadWorkspaceContent()
  54. else if (this.state.config.type === 'user') this.loadUserContent()
  55. }
  56. componentDidUpdate (prevProps, prevState) {
  57. const { state } = this
  58. console.log('%c<AdminWorkspaceUser> did update', `color: ${this.state.config.hexcolor}`, prevState, state)
  59. if (prevState.config.type !== state.config.type) {
  60. if (state.config.type === 'workspace') this.loadWorkspaceContent()
  61. else if (state.config.type === 'user') this.loadUserContent()
  62. }
  63. }
  64. componentWillUnmount () {
  65. console.log('%c<AdminWorkspaceUser> will Unmount', `color: ${this.state.config.hexcolor}`)
  66. document.removeEventListener('appCustomEvent', this.customEventReducer)
  67. }
  68. loadWorkspaceContent = async () => {
  69. const { props, state } = this
  70. const fetchWorkspaceList = getWorkspaceList(state.loggedUser, state.config.apiUrl)
  71. const workspaceList = await handleFetchResult(await fetchWorkspaceList)
  72. switch (workspaceList.apiResponse.status) {
  73. case 200:
  74. const fetchWorkspaceListMemberList = await Promise.all(
  75. workspaceList.body.map(async ws =>
  76. handleFetchResult(await getWorkspaceMemberList(state.loggedUser, state.config.apiUrl, ws.workspace_id))
  77. )
  78. )
  79. this.setState(prev => ({
  80. content: {
  81. ...prev.content,
  82. workspaceList: workspaceList.body.map(ws => ({
  83. ...ws,
  84. memberList: (fetchWorkspaceListMemberList.find(fws => fws.body[0].workspace_id === ws.workspace_id) || {body: []}).body
  85. }))
  86. }
  87. }))
  88. break
  89. default: GLOBAL_dispatchEvent({
  90. type: 'addFlashMsg',
  91. data: {
  92. msg: props.t('Error while loading workspaces list'),
  93. type: 'warning',
  94. delay: undefined
  95. }
  96. })
  97. }
  98. }
  99. loadUserContent = async () => {
  100. const { props, state } = this
  101. const userList = await handleFetchResult(await getUserList(state.loggedUser, state.config.apiUrl))
  102. switch (userList.apiResponse.status) {
  103. case 200:
  104. const fetchUserDetailList = await Promise.all(
  105. userList.body.map(async u =>
  106. handleFetchResult(await getUserDetail(state.loggedUser, state.config.apiUrl, u.user_id))
  107. )
  108. )
  109. this.setState(prev => ({
  110. content: {
  111. ...prev.content,
  112. userList: fetchUserDetailList.map(fu => fu.body)
  113. }
  114. }))
  115. break
  116. default: GLOBAL_dispatchEvent({
  117. type: 'addFlashMsg',
  118. data: {
  119. msg: props.t('Error while loading users list'),
  120. type: 'warning',
  121. delay: undefined
  122. }
  123. })
  124. }
  125. }
  126. handleDeleteWorkspace = async () => {
  127. const { props, state } = this
  128. const deleteWorkspaceResponse = await handleFetchResult(await deleteWorkspace(state.loggedUser, state.config.apiUrl, state.workspaceToDelete))
  129. switch (deleteWorkspaceResponse.status) {
  130. case 204:
  131. this.loadWorkspaceContent()
  132. GLOBAL_dispatchEvent({
  133. type: 'refreshWorkspaceList',
  134. data: {}
  135. })
  136. break
  137. default: GLOBAL_dispatchEvent({
  138. type: 'addFlashMsg',
  139. data: {
  140. msg: props.t('Error while deleting workspace'),
  141. type: 'warning',
  142. delay: undefined
  143. }
  144. })
  145. }
  146. this.handleClosePopupDeleteWorkspace()
  147. }
  148. handleOpenPopupDeleteWorkspace = idWorkspace => this.setState({
  149. popupDeleteWorkspaceDisplay: true,
  150. workspaceToDelete: idWorkspace
  151. })
  152. handleClosePopupDeleteWorkspace = () => this.setState({popupDeleteWorkspaceDisplay: false})
  153. handleToggleUser = async (idUser, toggle) => {
  154. const { props, state } = this
  155. const activateOrDelete = toggle ? putUserEnable : putUserDisable
  156. const toggleUser = await handleFetchResult(await activateOrDelete(state.loggedUser, state.config.apiUrl, idUser))
  157. switch (toggleUser.status) {
  158. case 204: this.loadUserContent(); break
  159. default: GLOBAL_dispatchEvent({
  160. type: 'addFlashMsg',
  161. data: {
  162. msg: props.t('Error while enabling or disabling user'),
  163. type: 'warning',
  164. delay: undefined
  165. }
  166. })
  167. }
  168. }
  169. handleUpdateProfile = async (idUser, newProfile) => {
  170. const { props, state } = this
  171. const toggleManager = await handleFetchResult(await putUserProfile(state.loggedUser, state.config.apiUrl, idUser, newProfile))
  172. switch (toggleManager.status) {
  173. case 204: this.loadUserContent(); break
  174. default: GLOBAL_dispatchEvent({
  175. type: 'addFlashMsg',
  176. data: {
  177. msg: props.t('Error while saving new profile'),
  178. type: 'warning',
  179. delay: undefined
  180. }
  181. })
  182. }
  183. }
  184. handleClickAddUser = async (email, profile) => {
  185. const { props, state } = this
  186. const newUserResult = await handleFetchResult(await postAddUser(state.loggedUser, state.config.apiUrl, email, profile))
  187. switch (newUserResult.apiResponse.status) {
  188. case 200:
  189. this.loadUserContent()
  190. break
  191. default: GLOBAL_dispatchEvent({
  192. type: 'addFlashMsg',
  193. data: {
  194. msg: props.t('Error while saving new user'),
  195. type: 'warning',
  196. delay: undefined
  197. }
  198. })
  199. }
  200. }
  201. render () {
  202. const { props, state } = this
  203. if (!state.isVisible) return null
  204. return (
  205. <div>
  206. {state.config.type === 'workspace' &&
  207. <AdminWorkspace
  208. workspaceList={state.content.workspaceList}
  209. onClickDeleteWorkspace={this.handleOpenPopupDeleteWorkspace}
  210. />
  211. }
  212. {state.config.type === 'user' &&
  213. <AdminUser
  214. userList={state.content.userList}
  215. profile={state.content.profile}
  216. onClickToggleUserBtn={this.handleToggleUser}
  217. onChangeProfile={this.handleUpdateProfile}
  218. onClickAddUser={this.handleClickAddUser}
  219. />
  220. }
  221. {state.popupDeleteWorkspaceDisplay &&
  222. <CardPopup
  223. customClass='adminworkspaceuser__popup'
  224. customHeaderClass='primaryColorBg'
  225. onClose={this.handleClosePopupDeleteWorkspace}
  226. >
  227. <div className='adminworkspaceuser__popup__body'>
  228. <div className='adminworkspaceuser__popup__body__msg'>{props.t('Are you sure ?')}</div>
  229. <div className='adminworkspaceuser__popup__body__btn'>
  230. <button
  231. type='button'
  232. className='btn'
  233. onClick={this.handleClosePopupDeleteWorkspace}
  234. >
  235. {props.t('Cancel')}
  236. </button>
  237. <button
  238. type='button'
  239. className='btn'
  240. onClick={this.handleDeleteWorkspace}
  241. >
  242. {props.t('Delete')}
  243. </button>
  244. </div>
  245. </div>
  246. </CardPopup>
  247. }
  248. </div>
  249. )
  250. }
  251. }
  252. export default translate()(AdminWorkspaceUser)