Thread.jsx 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_frontend_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. console.log('%c<Thread> Custom event', 'color: #28a745', type, data)
  40. this.setState({isVisible: true})
  41. break
  42. case 'thread_hideApp':
  43. console.log('%c<Thread> Custom event', 'color: #28a745', type, data)
  44. this.setState({isVisible: false})
  45. break
  46. case 'thread_reloadContent':
  47. console.log('%c<Thread> Custom event', 'color: #28a745', type, data)
  48. this.setState(prev => ({content: {...prev.content, ...data}, isVisible: true}))
  49. }
  50. }
  51. componentDidMount () {
  52. console.log('%c<Thread> did Mount', `color: ${this.state.config.hexcolor}`)
  53. this.loadContent()
  54. }
  55. componentDidUpdate (prevProps, prevState) {
  56. const { state } = this
  57. console.log('%c<Thread> did Mount', `color: ${this.state.config.hexcolor}`, prevState, state)
  58. if (!prevState.content || !state.content) return
  59. if (prevState.content.content_id !== state.content.content_id) this.loadContent()
  60. if (!prevState.timelineWysiwyg && state.timelineWysiwyg) wysiwyg('#wysiwygTimelineComment', this.handleChangeNewComment)
  61. else if (prevState.timelineWysiwyg && !state.timelineWysiwyg) tinymce.remove('#wysiwygTimelineComment')
  62. }
  63. componentWillUnmount () {
  64. console.log('%c<Thread> will Unmount', `color: ${this.state.config.hexcolor}`)
  65. document.removeEventListener('appCustomEvent', this.customEventReducer)
  66. }
  67. loadContent = async () => {
  68. const { loggedUser, content, config } = this.state
  69. if (content.content_id === '-1') return // debug case
  70. const fetchResultThread = getThreadContent(loggedUser, config.apiUrl, content.workspace_id, content.content_id)
  71. const fetchResultThreadComment = getThreadComment(loggedUser, config.apiUrl, content.workspace_id, content.content_id)
  72. Promise.all([
  73. handleFetchResult(await fetchResultThread),
  74. handleFetchResult(await fetchResultThreadComment)
  75. ])
  76. .then(([resThread, resComment]) => this.setState({
  77. content: resThread.body,
  78. listMessage: resComment.body.map(c => ({
  79. ...c,
  80. timelineType: 'comment',
  81. created: (new Date(c.created)).toLocaleString()
  82. }))
  83. }))
  84. .catch(e => console.log('Error loading Thread data.', e))
  85. }
  86. handleClickBtnCloseApp = () => {
  87. this.setState({ isVisible: false })
  88. GLOBAL_dispatchEvent({type: 'appClosed', data: {}}) // handled by tracim_front::src/container/WorkspaceContent.jsx
  89. }
  90. handleSaveEditTitle = async newTitle => {
  91. const { loggedUser, config, content } = this.state
  92. const fetchResultSaveThread = putThreadContent(loggedUser, config.apiUrl, content.workspace_id, content.content_id, newTitle)
  93. handleFetchResult(await fetchResultSaveThread)
  94. .then(resSave => {
  95. if (resSave.apiResponse.status === 200) {
  96. this.loadContent()
  97. GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
  98. } else {
  99. console.warn('Error saving threads. Result:', resSave, 'content:', content, 'config:', config)
  100. }
  101. })
  102. }
  103. handleChangeNewComment = e => {
  104. const newComment = e.target.value
  105. this.setState({newComment})
  106. }
  107. handleClickValidateNewCommentBtn = async () => {
  108. const { loggedUser, config, content, newComment } = this.state
  109. const fetchResultSaveNewComment = await postThreadNewComment(loggedUser, config.apiUrl, content.workspace_id, content.content_id, newComment)
  110. handleFetchResult(await fetchResultSaveNewComment)
  111. .then(resSave => {
  112. if (resSave.apiResponse.status === 200) {
  113. this.setState({newComment: ''})
  114. if (this.state.timelineWysiwyg) tinymce.get('wysiwygTimelineComment').setContent('')
  115. this.loadContent()
  116. } else {
  117. console.warn('Error saving thread comment. Result:', resSave, 'content:', content, 'config:', config)
  118. }
  119. })
  120. }
  121. handleToggleWysiwyg = () => this.setState(prev => ({timelineWysiwyg: !prev.timelineWysiwyg}))
  122. handleChangeStatus = async newStatus => {
  123. const { loggedUser, config, content } = this.state
  124. const fetchResultSaveEditStatus = putThreadStatus(loggedUser, config.apiUrl, content.workspace_id, content.content_id, newStatus)
  125. handleFetchResult(await fetchResultSaveEditStatus)
  126. .then(resSave => {
  127. if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
  128. console.warn('Error saving thread comment. Result:', resSave, 'content:', content, 'config:', config)
  129. } else {
  130. this.loadContent()
  131. }
  132. })
  133. }
  134. handleClickArchive = () => console.log('archive nyi')
  135. handleClickDelete = () => console.log('delete nyi')
  136. render () {
  137. const { config, isVisible, loggedUser, content, listMessage, newComment, timelineWysiwyg } = this.state
  138. if (!isVisible) return null
  139. return (
  140. <PopinFixed
  141. customClass={config.slug}
  142. customColor={config.hexcolor}
  143. >
  144. <PopinFixedHeader
  145. customClass={`${config.slug}__contentpage`}
  146. customColor={config.hexcolor}
  147. faIcon={config.faIcon}
  148. title={content.label}
  149. onClickCloseBtn={this.handleClickBtnCloseApp}
  150. onValidateChangeTitle={this.handleSaveEditTitle}
  151. />
  152. <PopinFixedOption
  153. customClass={`${config.slug}__contentpage`}
  154. customColor={config.hexcolor}
  155. i18n={i18n}
  156. >
  157. <div className='justify-content-end'>
  158. <SelectStatus
  159. selectedStatus={config.availableStatuses.find(s => s.slug === content.status)}
  160. availableStatus={config.availableStatuses}
  161. onChangeStatus={this.handleChangeStatus}
  162. disabled={false}
  163. />
  164. <ArchiveDeleteContent
  165. customColor={config.hexcolor}
  166. onClickArchiveBtn={this.handleClickArchive}
  167. onClickDeleteBtn={this.handleClickDelete}
  168. disabled={false}
  169. />
  170. </div>
  171. </PopinFixedOption>
  172. <PopinFixedContent
  173. customClass={`${config.slug}__contentpage`}
  174. >
  175. <Timeline
  176. customClass={`${config.slug}__contentpage`}
  177. customColor={config.hexcolor}
  178. loggedUser={loggedUser}
  179. timelineData={listMessage}
  180. newComment={newComment}
  181. disableComment={false}
  182. wysiwyg={timelineWysiwyg}
  183. onChangeNewComment={this.handleChangeNewComment}
  184. onClickValidateNewCommentBtn={this.handleClickValidateNewCommentBtn}
  185. onClickWysiwygBtn={this.handleToggleWysiwyg}
  186. onClickRevisionBtn={() => {}}
  187. shouldScrollToBottom
  188. showHeader={false}
  189. />
  190. </PopinFixedContent>
  191. </PopinFixed>
  192. )
  193. }
  194. }
  195. export default Thread