PopupCreateWorkspace.jsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React from 'react'
  2. import { translate } from 'react-i18next'
  3. import {
  4. CardPopupCreateContent,
  5. handleFetchResult,
  6. addAllResourceI18n
  7. } from 'tracim_frontend_lib'
  8. import { postWorkspace } from '../action.async.js'
  9. import i18n from '../i18n.js'
  10. const debug = { // outdated
  11. config: {
  12. slug: 'workspace',
  13. faIcon: 'bank',
  14. hexcolor: '#7d4e24',
  15. creationLabel: 'Create a workspace',
  16. domContainer: 'appFeatureContainer',
  17. apiUrl: 'http://localhost:3001',
  18. apiHeader: {
  19. 'Accept': 'application/json',
  20. 'Content-Type': 'application/json',
  21. 'Authorization': 'Basic ' + btoa(`${'admin@admin.admin'}:${'admin@admin.admin'}`)
  22. },
  23. translation: {
  24. en: {},
  25. fr: {}
  26. }
  27. },
  28. loggedUser: {
  29. id: 5,
  30. username: 'Smoi',
  31. firstname: 'Côme',
  32. lastname: 'Stoilenom',
  33. email: 'osef@algoo.fr',
  34. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  35. },
  36. idWorkspace: 1,
  37. idFolder: null
  38. }
  39. class PopupCreateWorkspace extends React.Component {
  40. constructor (props) {
  41. super(props)
  42. this.state = {
  43. appName: 'workspace',
  44. config: props.data ? props.data.config : debug.config,
  45. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  46. newWorkspaceName: ''
  47. }
  48. // i18n has been init, add resources from frontend
  49. addAllResourceI18n(i18n, this.state.config.translation)
  50. i18n.changeLanguage(this.state.loggedUser.lang)
  51. document.addEventListener('appCustomEvent', this.customEventReducer)
  52. }
  53. customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
  54. switch (type) {
  55. case 'allApp_changeLang':
  56. console.log('%c<PopupCreateWorkspace> Custom event', 'color: #28a745', type, data)
  57. this.setState(prev => ({
  58. loggedUser: {
  59. ...prev.loggedUser,
  60. lang: data
  61. }
  62. }))
  63. i18n.changeLanguage(data)
  64. break
  65. }
  66. }
  67. handleChangeNewWorkspaceName = e => this.setState({newWorkspaceName: e.target.value})
  68. handleClose = () => GLOBAL_dispatchEvent({
  69. type: 'hide_popupCreateWorkspace', // handled by tracim_front:dist/index.html
  70. data: {
  71. name: this.state.appName
  72. }
  73. })
  74. handleValidate = async () => {
  75. const { loggedUser, config, newWorkspaceName } = this.state
  76. const fetchSaveNewWorkspace = postWorkspace(loggedUser, config.apiUrl, newWorkspaceName)
  77. handleFetchResult(await fetchSaveNewWorkspace)
  78. .then(resSave => {
  79. if (resSave.apiResponse.status === 200) {
  80. this.handleClose()
  81. GLOBAL_dispatchEvent({ type: 'refreshWorkspaceList', data: {} })
  82. GLOBAL_dispatchEvent({
  83. type: 'redirect',
  84. data: {
  85. url: `/workspaces/${resSave.body.workspace_id}/dashboard`
  86. }
  87. })
  88. }
  89. })
  90. }
  91. render () {
  92. return (
  93. <CardPopupCreateContent
  94. onClose={this.handleClose}
  95. onValidate={this.handleValidate}
  96. label={this.props.t('New workspace')} // @TODO get the lang of user
  97. customColor={this.state.config.hexcolor}
  98. faIcon={this.state.config.faIcon}
  99. contentName={this.state.newWorkspaceName}
  100. onChangeContentName={this.handleChangeNewWorkspaceName}
  101. btnValidateLabel={this.props.t('Validate and create')}
  102. inputPlaceholder={this.props.t("Workspace's name")}
  103. />
  104. )
  105. }
  106. }
  107. export default translate()(PopupCreateWorkspace)