Thread.jsx 7.9KB

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