Thread.jsx 3.2KB

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