PageHtml.jsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, MODE } from '../helper.js'
  13. import i18n from '../i18n.js'
  14. const debug = {
  15. config: {
  16. name: 'PageHtml',
  17. label: {
  18. fr: 'Page Html',
  19. en: 'Html page'
  20. },
  21. componentLeft: 'PageHtml',
  22. componentRight: 'Timeline',
  23. customClass: 'wsContentPageHtml',
  24. icon: 'fa fa-file-text-o',
  25. color: '#fdfdfd',
  26. domContainer: 'appContainer',
  27. apiUrl: 'http://localhost:3001'
  28. },
  29. loggedUser: {
  30. id: 5,
  31. username: 'Smoi',
  32. firstname: 'Côme',
  33. lastname: 'Stoilenom',
  34. email: 'osef@algoo.fr',
  35. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  36. },
  37. content: {
  38. id: -1,
  39. type: 'pageHtml',
  40. title: 'Test debug pageHtml',
  41. status: 'validated',
  42. version: '-1',
  43. text: 'This is the default pageHtml content for debug purpose',
  44. workspace: {
  45. id: -1,
  46. title: 'Test debug workspace',
  47. ownerId: 5
  48. }
  49. },
  50. timeline: timelineDebugData
  51. }
  52. class pageHtml extends React.Component {
  53. constructor (props) {
  54. super(props)
  55. this.state = {
  56. appName: 'PageHtml',
  57. isVisible: true,
  58. config: props.data ? props.data.config : debug.config,
  59. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  60. content: props.data ? props.data.content : debug.content,
  61. timeline: props.data ? [] : debug.timeline,
  62. mode: MODE.VIEW
  63. }
  64. document.addEventListener('appCustomEvent', this.customEventReducer)
  65. }
  66. customEventReducer = ({ detail: action }) => { // action: { type: '', data: {} }
  67. switch (action.type) {
  68. case 'PageHtml_showApp':
  69. this.setState({isVisible: true})
  70. break
  71. case 'PageHtml_hideApp':
  72. this.setState({isVisible: false})
  73. break
  74. }
  75. }
  76. async componentDidMount () {
  77. const { content, config } = this.state
  78. if (content.id === '-1') return // debug case
  79. const fetchResultPageHtml = await fetch(`${config.apiUrl}/workspace/${content.workspace.id}/content/${content.id}`, {
  80. ...FETCH_CONFIG,
  81. method: 'GET'
  82. })
  83. const fetchResultTimeline = await fetch(`${config.apiUrl}/workspace/${content.workspace.id}/content/${content.id}/timeline`, {
  84. ...FETCH_CONFIG,
  85. method: 'GET'
  86. })
  87. fetchResultPageHtml.json = await handleFetchResult(fetchResultPageHtml)
  88. fetchResultTimeline.json = await handleFetchResult(fetchResultTimeline)
  89. this.setState({
  90. content: fetchResultPageHtml.json,
  91. timeline: fetchResultTimeline.json
  92. })
  93. wysiwyg()
  94. }
  95. handleClickBtnCloseApp = () => {
  96. this.setState({ isVisible: false })
  97. }
  98. handleChangeTitle = e => console.log('new title : ', e.target.value)
  99. handleClickNewVersion = () => {
  100. this.setState({ mode: MODE.EDIT })
  101. }
  102. handleCloseNewVersion = () => {
  103. this.setState({ mode: MODE.VIEW })
  104. }
  105. render () {
  106. const { isVisible, loggedUser, content, timeline, config } = this.state
  107. if (!isVisible) return null
  108. return (
  109. <PopinFixed customClass={`${config.customClass}`}>
  110. <PopinFixedHeader
  111. customClass={`${config.customClass}`}
  112. icon={config.icon}
  113. name={content.title}
  114. onClickCloseBtn={this.handleClickBtnCloseApp}
  115. onChangeTitle={this.handleChangeTitle}
  116. />
  117. <PopinFixedOption
  118. customClass={`${config.customClass}`}
  119. onClickNewVersion={this.handleClickNewVersion}
  120. i18n={i18n}
  121. />
  122. <PopinFixedContent customClass={`${config.customClass}__contentpage`}>
  123. <PageHtmlComponent
  124. mode={this.state.mode}
  125. onClickCloseNewVersion={this.handleCloseNewVersion}
  126. version={content.version}
  127. text={content.text}
  128. key={'PageHtml'}
  129. />
  130. <Timeline
  131. customClass={`${config.customClass}__contentpage`}
  132. loggedUser={loggedUser}
  133. timelineData={timeline}
  134. />
  135. </PopinFixedContent>
  136. </PopinFixed>
  137. )
  138. }
  139. }
  140. export default pageHtml