PopupCreateHtmlDocument.jsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { postHtmlDocContent } from '../action.async.js'
  9. import i18n from '../i18n.js'
  10. const debug = { // outdated
  11. config: {
  12. label: 'Text Document',
  13. slug: 'html-document',
  14. faIcon: 'file-text-o',
  15. hexcolor: '#3f52e3',
  16. creationLabel: 'Write a document',
  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. },
  25. loggedUser: {
  26. id: 5,
  27. username: 'Smoi',
  28. firstname: 'Côme',
  29. lastname: 'Stoilenom',
  30. email: 'osef@algoo.fr',
  31. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  32. },
  33. idWorkspace: 1,
  34. idFolder: null
  35. }
  36. class PopupCreateHtmlDocument extends React.Component {
  37. constructor (props) {
  38. super(props)
  39. this.state = {
  40. appName: 'html-document', // must remain 'html-document' because it is the name of the react built app (which contains HtmlDocument and PopupCreateHtmlDocument)
  41. config: props.data ? props.data.config : debug.config,
  42. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  43. idWorkspace: props.data ? props.data.idWorkspace : debug.idWorkspace,
  44. idFolder: props.data ? props.data.idFolder : debug.idFolder,
  45. newContentName: ''
  46. }
  47. // i18n has been init, add resources from frontend
  48. addAllResourceI18n(i18n, this.state.config.translation)
  49. i18n.changeLanguage(this.state.loggedUser.lang)
  50. document.addEventListener('appCustomEvent', this.customEventReducer)
  51. }
  52. customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
  53. switch (type) {
  54. case 'allApp_changeLang':
  55. console.log('%c<PopupCreateHtmlDocument> Custom event', 'color: #28a745', type, data)
  56. this.setState(prev => ({
  57. loggedUser: {
  58. ...prev.loggedUser,
  59. lang: data
  60. }
  61. }))
  62. i18n.changeLanguage(data)
  63. break
  64. }
  65. }
  66. handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
  67. handleClose = () => GLOBAL_dispatchEvent({
  68. type: 'hide_popupCreateContent', // handled by tracim_front:dist/index.html
  69. data: {
  70. name: this.state.appName
  71. }
  72. })
  73. handleValidate = async () => {
  74. const { loggedUser, config, appName, idWorkspace, idFolder, newContentName } = this.state
  75. const fetchSaveNewHtmlDoc = postHtmlDocContent(loggedUser, config.apiUrl, idWorkspace, idFolder, config.slug, newContentName)
  76. handleFetchResult(await fetchSaveNewHtmlDoc)
  77. .then(resSave => {
  78. if (resSave.apiResponse.status === 200) {
  79. this.handleClose()
  80. GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
  81. GLOBAL_dispatchEvent({
  82. type: 'openContentUrl', // handled by tracim_front:src/container/WorkspaceContent.jsx
  83. data: {
  84. idWorkspace: resSave.body.workspace_id,
  85. contentType: appName,
  86. idContent: resSave.body.content_id
  87. // will be open in edit mode because revision.length === 1
  88. }
  89. })
  90. }
  91. })
  92. }
  93. render () {
  94. return (
  95. <CardPopupCreateContent
  96. onClose={this.handleClose}
  97. onValidate={this.handleValidate}
  98. label={this.state.config.label} // @TODO get the lang of user
  99. customColor={this.state.config.hexcolor}
  100. faIcon={this.state.config.faIcon}
  101. contentName={this.state.newContentName}
  102. onChangeContentName={this.handleChangeNewContentName}
  103. btnValidateLabel={this.props.t('Validate and create')}
  104. inputPlaceholder={this.props.t("Document's title")}
  105. />
  106. )
  107. }
  108. }
  109. export default translate()(PopupCreateHtmlDocument)