Thread.jsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React from 'react'
  2. import ThreadComponent from '../component/Thread.jsx'
  3. import {
  4. handleFetchResult,
  5. PopinFixed,
  6. PopinFixedHeader,
  7. PopinFixedOption,
  8. PopinFixedContent
  9. } from 'tracim_lib'
  10. import { listMessageDebugData } from '../listMessageDebugData.js'
  11. import { FETCH_CONFIG } from '../helper.js'
  12. const debug = {
  13. loggedUser: {
  14. id: 5,
  15. username: 'Stoi',
  16. firstname: 'John',
  17. lastname: 'Doe',
  18. email: 'osef@algoo.fr',
  19. avatar: 'https://avatars3.githubusercontent.com/u/11177014?s=460&v=4'
  20. },
  21. workspace: {
  22. id: 1,
  23. title: 'Test debug workspace'
  24. },
  25. content: {
  26. id: 2,
  27. type: 'thread',
  28. status: 'validated',
  29. title: 'test debug title'
  30. },
  31. listMessage: listMessageDebugData,
  32. appConfig: {
  33. name: 'Thread',
  34. customClass: 'wsContentThread',
  35. icon: 'fa fa-comments-o',
  36. apiUrl: 'http://localhost:3001'
  37. }
  38. }
  39. class Thread extends React.Component {
  40. constructor (props) {
  41. super(props)
  42. this.state = {
  43. appName: 'Thread',
  44. isVisible: true,
  45. loggedUser: props.app ? props.app.loggedUser : debug.loggedUser,
  46. workspace: props.app ? props.app.workspace : debug.workspace,
  47. content: props.app ? props.app.content : debug.content,
  48. listMessage: props.app ? props.app.content.message_list : debug.listMessage,
  49. appConfig: props.app ? props.app.appConfig : debug.appConfig
  50. }
  51. document.addEventListener('appCustomEvent', this.customEventReducer)
  52. }
  53. async componentDidMount () {
  54. const { workspace, content, appConfig } = this.state
  55. if (content.id === '-1') return // debug case
  56. const fetchResultThread = await fetch(`${appConfig.apiUrl}/workspace/${workspace.id}/content/${content.id}`, {
  57. ...FETCH_CONFIG,
  58. method: 'GET'
  59. })
  60. fetchResultThread.json = await handleFetchResult(fetchResultThread)
  61. this.setState({
  62. content: fetchResultThread.json
  63. })
  64. }
  65. customEventReducer = ({detail}) => {
  66. switch (detail.type) {
  67. case 'Thread_showMsg': // unused for now, for testing purpose
  68. this.setState({inputText: detail.content})
  69. break
  70. }
  71. }
  72. handleClickBtnCloseApp = () => {
  73. this.setState({ isVisible: false })
  74. }
  75. render () {
  76. const { isVisible, loggedUser, content, listMessage, appConfig } = this.state
  77. if (!isVisible) return null
  78. return (
  79. <PopinFixed customClass={`${appConfig.customClass}`}>
  80. <PopinFixedHeader
  81. customClass={`${appConfig.customClass}`}
  82. icon={appConfig.icon}
  83. name={content.title}
  84. onClickCloseBtn={this.handleClickBtnCloseApp}
  85. />
  86. <PopinFixedOption customClass={`${appConfig.customClass}`} />
  87. <PopinFixedContent customClass={`${appConfig.customClass}__contentpage`}>
  88. <ThreadComponent
  89. title={content.title}
  90. listMessage={listMessage}
  91. loggedUser={loggedUser}
  92. />
  93. </PopinFixedContent>
  94. </PopinFixed>
  95. )
  96. }
  97. }
  98. export default Thread