PageHtml.jsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. label: 'Text Document',
  17. slug: 'page',
  18. faIcon: 'file-text-o',
  19. hexcolor: '#3f52e3',
  20. creationLabel: 'Write a document',
  21. domContainer: 'appContainer',
  22. apiUrl: 'localhost:6543/api/v2',
  23. mockApiUrl: 'localhost:3001',
  24. apiHeader: {
  25. 'Accept': 'application/json',
  26. 'Content-Type': 'application/json'
  27. // 'Authorization': 'Basic ' + btoa(`${'admin@admin.admin'}:${'admin@admin.admin'}`)
  28. }
  29. },
  30. loggedUser: {
  31. id: 5,
  32. username: 'Smoi',
  33. firstname: 'Côme',
  34. lastname: 'Stoilenom',
  35. email: 'osef@algoo.fr',
  36. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  37. },
  38. content: {
  39. author: {
  40. avatar_url: null,
  41. public_name: 'Global manager',
  42. user_id: 1
  43. },
  44. content_id: -1,
  45. content_type: 'page',
  46. created: '2018-06-18T14:59:26Z',
  47. current_revision_id: 11,
  48. is_archived: false,
  49. is_deleted: false,
  50. label: 'Current Menu',
  51. last_modifier: {
  52. avatar_url: null,
  53. public_name: 'Global manager',
  54. user_id: 1
  55. },
  56. modified: '2018-06-18T14:59:26Z',
  57. parent_id: 2,
  58. raw_content: '<div>bonjour, je suis un lapin.</div>',
  59. show_in_ui: true,
  60. slug: 'current-menu',
  61. status: 'open',
  62. sub_content_types: ['thread', 'page', 'file', 'folder'],
  63. workspace_id: 1
  64. },
  65. timeline: timelineDebugData
  66. }
  67. class pageHtml extends React.Component {
  68. constructor (props) {
  69. super(props)
  70. this.state = {
  71. appName: 'page',
  72. isVisible: true,
  73. config: props.data ? props.data.config : debug.config,
  74. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  75. content: props.data ? props.data.content : debug.content,
  76. timeline: props.data ? [] : debug.timeline,
  77. mode: MODE.VIEW
  78. }
  79. document.addEventListener('appCustomEvent', this.customEventReducer)
  80. }
  81. customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
  82. switch (type) {
  83. case 'page_showApp':
  84. this.setState({isVisible: true})
  85. break
  86. case 'page_hideApp':
  87. this.setState({isVisible: false})
  88. break
  89. case 'page_reloadContent':
  90. this.setState({content: data})
  91. }
  92. }
  93. componentDidMount () {
  94. console.log('pageHtml did mount')
  95. if (this.state.content.content_id === -1) return // debug case
  96. this.loadContent()
  97. // wysiwyg()
  98. }
  99. componentDidUpdate (prevProps, prevState) {
  100. console.log('pageHtml did update', prevState, this.state)
  101. if (!prevState.content || !this.state.content) return
  102. if (prevState.content.content_id !== this.state.content.content_id) {
  103. this.loadContent()
  104. }
  105. }
  106. loadContent = async () => {
  107. console.log('loadContent')
  108. const { content, config } = this.state
  109. const fetchResultPageHtml = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/html-documents/${content.content_id}`, {
  110. ...FETCH_CONFIG,
  111. method: 'GET'
  112. })
  113. const fetchResultTimeline = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/contents/${content.content_id}/comments`, {
  114. ...FETCH_CONFIG,
  115. method: 'GET'
  116. })
  117. // Promise.all([
  118. // handleFetchResult(fetchResultPageHtml),
  119. // handleFetchResult(fetchResultTimeline)
  120. // ])
  121. // .then(([resPageHtml, resTimeline]) => {
  122. // this.setState({
  123. // content: resPageHtml,
  124. // timeline: resTimeline
  125. // })
  126. // })
  127. handleFetchResult(fetchResultPageHtml)
  128. .then(resPageHtml => this.setState({content: resPageHtml}))
  129. .catch(e => console.log('Error loading content.', e))
  130. handleFetchResult(fetchResultTimeline)
  131. .then(resTimeline => this.setState({timeline: resTimeline}))
  132. .catch(e => {
  133. console.log('Error loading Timeline.', e)
  134. this.setState({timeline: []})
  135. })
  136. }
  137. handleClickBtnCloseApp = () => {
  138. this.setState({ isVisible: false })
  139. GLOBAL_dispatchEvent({type: 'appClosed', data: {}})
  140. }
  141. handleChangeTitle = e => console.log('new title : ', e.target.value)
  142. handleClickNewVersion = () => {
  143. this.setState({ mode: MODE.EDIT })
  144. }
  145. handleCloseNewVersion = () => {
  146. this.setState({ mode: MODE.VIEW })
  147. }
  148. render () {
  149. const { isVisible, loggedUser, content, timeline, config } = this.state
  150. if (!isVisible) return null
  151. return (
  152. <PopinFixed customClass={`${config.slug}`}>
  153. <PopinFixedHeader
  154. customClass={`${config.slug}`}
  155. icon={config.faIcon}
  156. name={content.label}
  157. onClickCloseBtn={this.handleClickBtnCloseApp}
  158. onChangeTitle={this.handleChangeTitle}
  159. />
  160. <PopinFixedOption
  161. customClass={`${config.slug}`}
  162. onClickNewVersionBtn={this.handleClickNewVersion}
  163. i18n={i18n}
  164. />
  165. <PopinFixedContent customClass={`${config.slug}__contentpage`}>
  166. <PageHtmlComponent
  167. mode={this.state.mode}
  168. onClickCloseEditMode={this.handleCloseNewVersion}
  169. version={content.current_revision_id}
  170. text={content.raw_content}
  171. key={'html-documents'}
  172. />
  173. <Timeline
  174. customClass={`${config.slug}__contentpage`}
  175. loggedUser={loggedUser}
  176. timelineData={timeline}
  177. />
  178. </PopinFixedContent>
  179. </PopinFixed>
  180. )
  181. }
  182. }
  183. export default pageHtml