PageHtml.jsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react'
  2. import PageHtmlComponent from '../component/PageHtml.jsx'
  3. import {
  4. handleFetchResult,
  5. PopinFixed,
  6. PopinFixedHeader,
  7. PopinFixedOption,
  8. PopinFixedContent,
  9. Timeline
  10. } from 'tracim_lib'
  11. import { timelineDebugData } from '../timelineDebugData.js'
  12. import { FETCH_CONFIG } from '../helper.js'
  13. const debug = {
  14. workspace: {
  15. id: -1,
  16. title: 'Test debug workspace'
  17. },
  18. appConfig: {
  19. name: 'PageHtml',
  20. customClass: 'wsFilePageHtml',
  21. icon: 'fa fa-file-word-o',
  22. apiUrl: 'http://localhost:3001'
  23. },
  24. loggedUser: {
  25. id: 5,
  26. username: 'Smoi',
  27. firstname: 'Côme',
  28. lastname: 'Stoilenom',
  29. email: 'osef@algoo.fr',
  30. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  31. },
  32. content: {
  33. id: -1,
  34. type: 'pageHtml',
  35. title: 'Test debug pageHtml',
  36. status: 'validated',
  37. version: '-1',
  38. text: 'This is the default pageHtml content for debug purpose'
  39. },
  40. timeline: timelineDebugData
  41. }
  42. class pageHtml extends React.Component {
  43. constructor (props) {
  44. super(props)
  45. this.state = {
  46. appName: 'PageHtml',
  47. isVisible: true,
  48. workspace: props.data ? props.data.workspace : debug.workspace,
  49. appConfig: props.data ? props.data.appConfig : debug.appConfig,
  50. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  51. content: props.data ? props.data.content : debug.content,
  52. timeline: props.data ? [] : debug.timeline
  53. }
  54. document.addEventListener('appCustomEvent', this.customEventReducer)
  55. }
  56. customEventReducer = ({ detail: action }) => { // action: { type: '', data: {} }
  57. switch (action.type) {
  58. case 'PageHtml_showApp':
  59. this.setState({isVisible: true})
  60. break
  61. case 'PageHtml_hideApp':
  62. this.setState({isVisible: false})
  63. break
  64. }
  65. }
  66. async componentDidMount () {
  67. const { workspace, content, appConfig } = this.state
  68. if (content.id === '-1') return // debug case
  69. const fetchResultPageHtml = await fetch(`${appConfig.apiUrl}/workspace/${workspace.id}/content/${content.id}`, {
  70. ...FETCH_CONFIG,
  71. method: 'GET'
  72. })
  73. const fetchResultTimeline = await fetch(`${appConfig.apiUrl}/workspace/${workspace.id}/content/${content.id}/timeline`, {
  74. ...FETCH_CONFIG,
  75. method: 'GET'
  76. })
  77. fetchResultPageHtml.json = await handleFetchResult(fetchResultPageHtml)
  78. fetchResultTimeline.json = await handleFetchResult(fetchResultTimeline)
  79. this.setState({
  80. content: fetchResultPageHtml.json,
  81. timeline: fetchResultTimeline.json
  82. })
  83. }
  84. handleClickBtnCloseApp = () => {
  85. this.setState({ isVisible: false })
  86. }
  87. render () {
  88. const { isVisible, loggedUser, content, timeline, appConfig } = this.state
  89. if (!isVisible) return null
  90. return (
  91. <PopinFixed customClass={`${appConfig.customClass}`}>
  92. <PopinFixedHeader
  93. customClass={`${appConfig.customClass}`}
  94. icon={appConfig.icon}
  95. name={content.title}
  96. onClickCloseBtn={this.handleClickBtnCloseApp}
  97. />
  98. <PopinFixedOption customClass={`${appConfig.customClass}`} />
  99. <PopinFixedContent customClass={`${appConfig.customClass}__contentpage`}>
  100. <PageHtmlComponent
  101. version={content.version}
  102. text={content.text}
  103. key={'PageHtml'}
  104. />
  105. <Timeline
  106. customClass={`${appConfig.customClass}__contentpage`}
  107. loggedUser={loggedUser}
  108. timelineData={timeline}
  109. />
  110. </PopinFixedContent>
  111. </PopinFixed>
  112. )
  113. }
  114. }
  115. export default pageHtml