PopupCreateHtmlDocument.jsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React from 'react'
  2. import {
  3. CardPopupCreateContent, handleFetchResult
  4. } from 'tracim_frontend_lib'
  5. import { postHtmlDocContent } from '../action.async.js'
  6. import {addAllResourceForI18n, addAllResourceI18n} from '../../../frontend_lib'
  7. import i18n from '../i18n.js'
  8. const debug = { // outdated
  9. config: {
  10. label: 'Text Document',
  11. slug: 'html-documents',
  12. faIcon: 'file-text-o',
  13. hexcolor: '#3f52e3',
  14. creationLabel: 'Write a document',
  15. domContainer: 'appContainer',
  16. apiUrl: 'http://localhost:3001',
  17. apiHeader: {
  18. 'Accept': 'application/json',
  19. 'Content-Type': 'application/json',
  20. 'Authorization': 'Basic ' + btoa(`${'admin@admin.admin'}:${'admin@admin.admin'}`)
  21. }
  22. },
  23. loggedUser: {
  24. id: 5,
  25. username: 'Smoi',
  26. firstname: 'Côme',
  27. lastname: 'Stoilenom',
  28. email: 'osef@algoo.fr',
  29. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  30. },
  31. idWorkspace: 1,
  32. idFolder: null
  33. }
  34. class PopupCreateHtmlDocument extends React.Component {
  35. constructor (props) {
  36. super(props)
  37. this.state = {
  38. appName: 'html-documents',
  39. config: props.data ? props.data.config : debug.config,
  40. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  41. idWorkspace: props.data ? props.data.idWorkspace : debug.idWorkspace,
  42. idFolder: props.data ? props.data.idFolder : debug.idFolder,
  43. newContentName: ''
  44. }
  45. // i18n has been init, add resources from frontend
  46. addAllResourceI18n(i18n, props.data ? props.data.config.translation : debug.config.translation)
  47. }
  48. handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
  49. handleClose = () => GLOBAL_dispatchEvent({
  50. type: 'hide_popupCreateContent', // handled by tracim_front:dist/index.html
  51. data: {
  52. name: this.state.appName
  53. }
  54. })
  55. handleValidate = async () => {
  56. const { loggedUser, config, appName, idWorkspace, idFolder, newContentName } = this.state
  57. const fetchSaveNewHtmlDoc = postHtmlDocContent(loggedUser, config.apiUrl, idWorkspace, idFolder, config.slug, newContentName)
  58. handleFetchResult(await fetchSaveNewHtmlDoc)
  59. .then(resSave => {
  60. if (resSave.apiResponse.status === 200) {
  61. this.handleClose()
  62. GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
  63. GLOBAL_dispatchEvent({
  64. type: 'openContentUrl', // handled by tracim_front:src/container/WorkspaceContent.jsx
  65. data: {
  66. idWorkspace: resSave.body.workspace_id,
  67. contentType: appName,
  68. idContent: resSave.body.content_id
  69. // will be open in edit mode because revision.length === 1
  70. }
  71. })
  72. }
  73. })
  74. }
  75. render () {
  76. return (
  77. <CardPopupCreateContent
  78. onClose={this.handleClose}
  79. onValidate={this.handleValidate}
  80. label={this.state.config.label} // @TODO get the lang of user
  81. customColor={this.state.config.hexcolor}
  82. faIcon={this.state.config.faIcon}
  83. contentName={this.state.newContentName}
  84. onChangeContentName={this.handleChangeNewContentName}
  85. btnValidateLabel='Valider et créer'
  86. />
  87. )
  88. }
  89. }
  90. export default PopupCreateHtmlDocument