PopupCreatePageHtml.jsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React from 'react'
  2. import {
  3. CardPopupCreateContent,
  4. handleFetchResult
  5. } from 'tracim_lib'
  6. import { FETCH_CONFIG } from '../helper.js'
  7. const debug = {
  8. config: {
  9. label: 'Text Document',
  10. slug: 'page',
  11. faIcon: 'file-text-o',
  12. hexcolor: '#3f52e3',
  13. creationLabel: 'Write a document',
  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: 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 PopupCreatePageHtml extends React.Component {
  35. constructor (props) {
  36. super(props)
  37. this.state = {
  38. appName: 'page',
  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. this.handleValidate = this.handleValidate.bind(this)
  46. }
  47. handleChangeNewContentName = e => this.setState({newContentName: e.target.value})
  48. handleClose = () => GLOBAL_dispatchEvent({
  49. type: 'hide_popupCreateContent',
  50. data: {
  51. name: this.state.appName
  52. }
  53. })
  54. async handleValidate () {
  55. const fetchSaveNewHtmlDoc = await fetch(`${this.state.config.apiUrl}/workspaces/${this.state.idWorkspace}/contents`, {
  56. ...FETCH_CONFIG,
  57. method: 'POST',
  58. body: JSON.stringify({
  59. parent_id: this.state.idFolder,
  60. content_type_slug: this.state.config.slug,
  61. label: this.state.newContentName
  62. })
  63. })
  64. if (fetchSaveNewHtmlDoc.status === 200) {
  65. const jsonSaveNewHtmlDoc = await fetchSaveNewHtmlDoc.json()
  66. console.log(jsonSaveNewHtmlDoc)
  67. this.handleClose()
  68. GLOBAL_dispatchEvent({
  69. type: 'openContentUrl',
  70. data: {
  71. idWorkspace: jsonSaveNewHtmlDoc.workspace_id,
  72. contentType: this.state.appName,
  73. idContent: jsonSaveNewHtmlDoc.id
  74. }
  75. })
  76. }
  77. }
  78. render () {
  79. return (
  80. <CardPopupCreateContent
  81. onClose={this.handleClose}
  82. onValidate={this.handleValidate}
  83. label={this.state.config.label} // @TODO get the lang of user
  84. hexcolor={this.state.config.hexcolor}
  85. faIcon={this.state.config.faIcon}
  86. contentName={this.state.newContentName}
  87. onChangeContentName={this.handleChangeNewContentName}
  88. btnValidateLabel='Valider et créer'
  89. />
  90. )
  91. }
  92. }
  93. export default PopupCreatePageHtml