PopupCreateThread.jsx 3.9KB

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