PopupCreateWorkspace.jsx 3.4KB

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