PopupCreateThread.jsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React from 'react'
  2. import {
  3. addAllResourceI18n,
  4. CardPopupCreateContent,
  5. handleFetchResult
  6. } from 'tracim_frontend_lib'
  7. import { postThreadContent } from '../action.async.js'
  8. const debug = { // outdated
  9. config: {
  10. label: 'Thread',
  11. slug: 'thread',
  12. faIcon: 'file-text-o',
  13. hexcolor: '#ad4cf9',
  14. creationLabel: 'Write a thread',
  15. domContainer: 'appContainer',
  16. apiUrl: 'http://localhost:3001',
  17. mockApiUrl: 'http://localhost:8071',
  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. translation: {}
  26. },
  27. fr: {
  28. translation: {}
  29. }
  30. }
  31. },
  32. loggedUser: {
  33. id: 1,
  34. username: 'Smoi',
  35. firstname: 'Côme',
  36. lastname: 'Stoilenom',
  37. email: 'osef@algoo.fr',
  38. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  39. },
  40. idWorkspace: 1,
  41. idFolder: null
  42. }
  43. class PopupCreateHtmlDocument extends React.Component {
  44. constructor (props) {
  45. super(props)
  46. this.state = {
  47. appName: 'thread',
  48. config: props.data ? props.data.config : debug.config,
  49. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  50. idWorkspace: props.data ? props.data.idWorkspace : debug.idWorkspace,
  51. idFolder: props.data ? props.data.idFolder : debug.idFolder,
  52. newContentName: ''
  53. }
  54. // i18n has been init, add resources from frontend
  55. addAllResourceI18n(i18n, props.data ? props.data.config.translation : debug.config.translation)
  56. }
  57. handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
  58. handleClose = () => GLOBAL_dispatchEvent({
  59. type: 'hide_popupCreateContent', // handled by tracim_front:dist/index.html
  60. data: {
  61. name: this.state.appName
  62. }
  63. })
  64. handleValidate = async () => {
  65. const { config, appName, idWorkspace, idFolder, newContentName } = this.state
  66. const fetchSaveThreadDoc = postThreadContent(config.apiUrl, idWorkspace, idFolder, config.slug, newContentName)
  67. handleFetchResult(await fetchSaveThreadDoc)
  68. .then(resSave => {
  69. if (resSave.apiResponse.status === 200) {
  70. this.handleClose()
  71. GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
  72. GLOBAL_dispatchEvent({
  73. type: 'openContentUrl', // handled by tracim_front:src/container/WorkspaceContent.jsx
  74. data: {
  75. idWorkspace: resSave.body.workspace_id,
  76. contentType: appName,
  77. idContent: resSave.body.content_id
  78. }
  79. })
  80. }
  81. })
  82. }
  83. render () {
  84. return (
  85. <CardPopupCreateContent
  86. onClose={this.handleClose}
  87. onValidate={this.handleValidate}
  88. label={this.state.config.label} // @TODO get the lang of user
  89. customColor={this.state.config.hexcolor}
  90. faIcon={this.state.config.faIcon}
  91. contentName={this.state.newContentName}
  92. onChangeContentName={this.handleChangeNewContentName}
  93. btnValidateLabel='Valider et créer'
  94. />
  95. )
  96. }
  97. }
  98. export default PopupCreateHtmlDocument