PageHtml.jsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React from 'react'
  2. import PageHtmlComponent from '../component/PageHtml.jsx'
  3. import {
  4. PopinFixed,
  5. PopinFixedHeader,
  6. PopinFixedOption,
  7. PopinFixedContent,
  8. Timeline
  9. } from 'tracim_lib'
  10. import { FETCH_CONFIG } from '../helper.js'
  11. const debug = {
  12. workspace: {
  13. id: '-1',
  14. title: 'Test debugg workspace'
  15. },
  16. content: {
  17. id: '-1',
  18. type: 'pageHtml',
  19. title: 'Test debugg pageHtml',
  20. status: 'validated',
  21. version: '-1',
  22. text: 'This is the default pageHtml content for debug purpose'
  23. },
  24. appConfig: {
  25. name: 'PageHtml',
  26. customClass: 'wsFilePageHtml',
  27. icon: 'fa fa-file-word-o',
  28. apiUrl: 'http://localhost:3001'
  29. }
  30. }
  31. class pageHtml extends React.Component {
  32. constructor (props) {
  33. super(props)
  34. this.state = {
  35. appName: 'PageHtml',
  36. isVisible: true,
  37. workspace: props.app ? props.app.workspace : debug.workspace,
  38. content: props.app ? props.app.content : debug.content,
  39. appConfig: props.app ? props.app.appConfig : debug.appConfig
  40. }
  41. document.addEventListener('appCustomEvent', this.customEventReducer)
  42. }
  43. async componentDidMount () {
  44. const { workspace, content, appConfig } = this.state
  45. if (content.id === '-1') return
  46. const fetchResult = await fetch(`${appConfig.apiUrl}/workspace/${workspace.id}/content/${content.id}`, {
  47. ...FETCH_CONFIG,
  48. method: 'GET'
  49. })
  50. fetchResult.json = await (async () => {
  51. switch (fetchResult.status) {
  52. case 200:
  53. case 304:
  54. return fetchResult.json()
  55. case 204:
  56. case 400:
  57. case 404:
  58. case 409:
  59. case 500:
  60. case 501:
  61. case 502:
  62. case 503:
  63. case 504:
  64. return `Error: ${fetchResult.status}` // @TODO : handle errors from api result
  65. }
  66. })()
  67. this.setState({content: fetchResult.json})
  68. }
  69. customEventReducer = action => { // action: { type: '', data: {} }
  70. switch (action.type) {
  71. case 'PageHtml_dummyTest':
  72. this.setState({dummy: true})
  73. break
  74. }
  75. }
  76. handleClickBtnCloseApp = () => {
  77. // GLOBAL_unmountApp(this.state.appName)
  78. this.setState({ isVisible: false })
  79. }
  80. render () {
  81. const { isVisible, content, appConfig } = this.state
  82. if (!isVisible) return null
  83. return (
  84. <PopinFixed customClass={`${appConfig.customClass}`}>
  85. <PopinFixedHeader
  86. customClass={`${appConfig.customClass}`}
  87. icon={appConfig.icon}
  88. name={content.title}
  89. onClickCloseBtn={this.handleClickBtnCloseApp}
  90. />
  91. <PopinFixedOption customClass={`${appConfig.customClass}`} />
  92. <PopinFixedContent customClass={`${appConfig.customClass}__contentpage`}>
  93. <PageHtmlComponent
  94. version={content.version}
  95. text={content.text}
  96. key={'PageHtml'}
  97. />
  98. <Timeline
  99. customClass={`${appConfig.customClass}__contentpage`}
  100. key={'pageHtml__timeline'}
  101. />
  102. </PopinFixedContent>
  103. </PopinFixed>
  104. )
  105. }
  106. }
  107. export default pageHtml