PopupCreateThread.jsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React from 'react'
  2. import {
  3. CardPopupCreateContent,
  4. handleFetchResult
  5. } from 'tracim_frontend_lib'
  6. import { postThreadContent } from '../action.async.js'
  7. const debug = { // outdated
  8. config: {
  9. label: 'Thread',
  10. slug: 'thread',
  11. faIcon: 'file-text-o',
  12. hexcolor: '#ad4cf9',
  13. creationLabel: 'Write a thread',
  14. domContainer: 'appContainer',
  15. apiUrl: 'http://localhost:3001',
  16. mockApiUrl: 'http://localhost:8071',
  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: 1,
  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: 'thread',
  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. }
  46. handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
  47. handleClose = () => GLOBAL_dispatchEvent({
  48. type: 'hide_popupCreateContent', // handled by tracim_front:dist/index.html
  49. data: {
  50. name: this.state.appName
  51. }
  52. })
  53. handleValidate = async () => {
  54. const { config, appName, idWorkspace, idFolder, newContentName } = this.state
  55. const fetchSaveThreadDoc = postThreadContent(config.apiUrl, idWorkspace, idFolder, config.slug, newContentName)
  56. handleFetchResult(await fetchSaveThreadDoc)
  57. .then(resSave => {
  58. if (resSave.apiResponse.status === 200) {
  59. this.handleClose()
  60. GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
  61. GLOBAL_dispatchEvent({
  62. type: 'openContentUrl', // handled by tracim_front:src/container/WorkspaceContent.jsx
  63. data: {
  64. idWorkspace: resSave.body.workspace_id,
  65. contentType: appName,
  66. idContent: resSave.body.content_id
  67. }
  68. })
  69. }
  70. })
  71. }
  72. render () {
  73. return (
  74. <CardPopupCreateContent
  75. onClose={this.handleClose}
  76. onValidate={this.handleValidate}
  77. label={this.state.config.label} // @TODO get the lang of user
  78. customColor={this.state.config.hexcolor}
  79. faIcon={this.state.config.faIcon}
  80. contentName={this.state.newContentName}
  81. onChangeContentName={this.handleChangeNewContentName}
  82. btnValidateLabel='Valider et créer'
  83. />
  84. )
  85. }
  86. }
  87. export default PopupCreateHtmlDocument