Thread.jsx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React from 'react'
  2. import i18n from '../i18n.js'
  3. import { debug } from '../helper.js'
  4. import {
  5. handleFetchResult,
  6. PopinFixed,
  7. PopinFixedHeader,
  8. PopinFixedOption,
  9. PopinFixedContent,
  10. Timeline,
  11. SelectStatus,
  12. ArchiveDeleteContent
  13. } from 'tracim_lib'
  14. import {
  15. getThreadContent,
  16. getThreadComment,
  17. postThreadNewComment,
  18. putThreadStatus,
  19. putThreadContent
  20. } from '../action.async.js'
  21. class Thread extends React.Component {
  22. constructor (props) {
  23. super(props)
  24. this.state = {
  25. appName: 'thread',
  26. isVisible: true,
  27. config: props.data ? props.data.config : debug.config,
  28. loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
  29. content: props.data ? props.data.content : debug.content,
  30. listMessage: props.data ? [] : [], // debug.listMessage,
  31. newComment: '',
  32. timelineWysiwyg: false
  33. }
  34. document.addEventListener('appCustomEvent', this.customEventReducer)
  35. }
  36. customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
  37. switch (type) {
  38. case 'thread_showApp':
  39. this.setState({isVisible: true})
  40. break
  41. case 'thread_hideApp':
  42. this.setState({isVisible: false})
  43. break
  44. case 'thread_reloadContent':
  45. this.setState(prev => ({content: {...prev.content, ...data}, isVisible: true}))
  46. }
  47. }
  48. componentDidMount () {
  49. console.log('Thread did Mount')
  50. this.loadContent()
  51. }
  52. componentDidUpdate (prevProps, prevState) {
  53. const { state } = this
  54. console.log('Thread did Update', prevState, state)
  55. if (!prevState.content || !state.content) return
  56. if (prevState.content.content_id !== state.content.content_id) this.loadContent()
  57. if (!prevState.timelineWysiwyg && state.timelineWysiwyg) wysiwyg('#wysiwygTimelineComment', this.handleChangeNewComment)
  58. else if (prevState.timelineWysiwyg && !state.timelineWysiwyg) tinymce.remove('#wysiwygTimelineComment')
  59. }
  60. loadContent = async () => {
  61. const { content, config } = this.state
  62. if (content.content_id === '-1') return // debug case
  63. const fetchResultThread = getThreadContent(config.apiUrl, content.workspace_id, content.content_id)
  64. const fetchResultThreadComment = getThreadComment(config.apiUrl, content.workspace_id, content.content_id)
  65. Promise.all([
  66. handleFetchResult(await fetchResultThread),
  67. handleFetchResult(await fetchResultThreadComment)
  68. ])
  69. .then(([resThread, resComment]) => this.setState({
  70. content: resThread.body,
  71. listMessage: resComment.body.map(c => ({
  72. ...c,
  73. timelineType: 'comment',
  74. created: (new Date(c.created)).toLocaleString()
  75. }))
  76. }))
  77. .catch(e => console.log('Error loading Thread data.', e))
  78. }
  79. handleClickBtnCloseApp = () => {
  80. this.setState({ isVisible: false })
  81. GLOBAL_dispatchEvent({type: 'appClosed', data: {}}) // handled by tracim_front::src/container/WorkspaceContent.jsx
  82. }
  83. handleSaveEditTitle = async newTitle => {
  84. const { config, content } = this.state
  85. const fetchResultSaveThread = putThreadContent(config.apiUrl, content.workspace_id, content.content_id, newTitle)
  86. handleFetchResult(await fetchResultSaveThread)
  87. .then(resSave => {
  88. if (resSave.apiResponse.status === 200) this.loadContent()
  89. else console.warn('Error saving threads. Result:', resSave, 'content:', content, 'config:', config)
  90. })
  91. }
  92. handleChangeNewComment = e => {
  93. const newComment = e.target.value
  94. this.setState({newComment})
  95. }
  96. handleClickValidateNewCommentBtn = async () => {
  97. const { config, content, newComment } = this.state
  98. const fetchResultSaveNewComment = await postThreadNewComment(config.apiUrl, content.workspace_id, content.content_id, newComment)
  99. handleFetchResult(await fetchResultSaveNewComment)
  100. .then(resSave => {
  101. if (resSave.apiResponse.status === 200) {
  102. this.setState({newComment: ''})
  103. if (this.state.timelineWysiwyg) tinymce.get('wysiwygTimelineComment').setContent('')
  104. this.loadContent()
  105. } else {
  106. console.warn('Error saving thread comment. Result:', resSave, 'content:', content, 'config:', config)
  107. }
  108. })
  109. }
  110. handleToggleWysiwyg = () => this.setState(prev => ({timelineWysiwyg: !prev.timelineWysiwyg}))
  111. handleChangeStatus = async newStatus => {
  112. const { config, content } = this.state
  113. const fetchResultSaveEditStatus = putThreadStatus(config.apiUrl, content.workspace_id, content.content_id, newStatus)
  114. handleFetchResult(await fetchResultSaveEditStatus)
  115. .then(resSave => {
  116. if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
  117. console.warn('Error saving thread comment. Result:', resSave, 'content:', content, 'config:', config)
  118. } else {
  119. this.loadContent()
  120. }
  121. })
  122. }
  123. handleClickArchive = () => console.log('archive nyi')
  124. handleClickDelete = () => console.log('delete nyi')
  125. render () {
  126. const { config, isVisible, loggedUser, content, listMessage, newComment, timelineWysiwyg } = this.state
  127. if (!isVisible) return null
  128. return (
  129. <PopinFixed customClass={`wsContentThread`}>
  130. <PopinFixedHeader
  131. customClass={`wsContentThread`}
  132. faIcon={config.faIcon}
  133. title={content.label}
  134. onClickCloseBtn={this.handleClickBtnCloseApp}
  135. onValidateChangeTitle={this.handleSaveEditTitle}
  136. />
  137. <PopinFixedOption customClass={`wsContentThread`} i18n={i18n}>
  138. <div className='justify-content-end'>
  139. <SelectStatus
  140. selectedStatus={config.availableStatuses.find(s => s.slug === content.status)}
  141. availableStatus={config.availableStatuses}
  142. onChangeStatus={this.handleChangeStatus}
  143. disabled={false}
  144. />
  145. <ArchiveDeleteContent
  146. onClickArchiveBtn={this.handleClickArchive}
  147. onClickDeleteBtn={this.handleClickDelete}
  148. disabled={false}
  149. />
  150. </div>
  151. </PopinFixedOption>
  152. <PopinFixedContent customClass={`${config.customClass}__contentpage`}>
  153. <Timeline
  154. customClass={`${config.slug}__contentpage`}
  155. loggedUser={loggedUser}
  156. timelineData={listMessage}
  157. newComment={newComment}
  158. disableComment={false}
  159. wysiwyg={timelineWysiwyg}
  160. onChangeNewComment={this.handleChangeNewComment}
  161. onClickValidateNewCommentBtn={this.handleClickValidateNewCommentBtn}
  162. onClickWysiwygBtn={this.handleToggleWysiwyg}
  163. onClickRevisionBtn={() => {}}
  164. shouldScrollToBottom={false}
  165. showHeader={false}
  166. />
  167. </PopinFixedContent>
  168. </PopinFixed>
  169. )
  170. }
  171. }
  172. export default Thread