WorkspaceContent.jsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import Folder from '../component/Workspace/Folder.jsx'
  4. import FileItem from '../component/Workspace/FileItem.jsx'
  5. import FileItemHeader from '../component/Workspace/FileItemHeader.jsx'
  6. import PageWrapper from '../component/common/layout/PageWrapper.jsx'
  7. import PageTitle from '../component/common/layout/PageTitle.jsx'
  8. import PageContent from '../component/common/layout/PageContent.jsx'
  9. import DropdownCreateButton from '../component/common/Input/DropdownCreateButton.jsx'
  10. import { FETCH_CONFIG } from '../helper.js'
  11. import { setActiveFileContentActive } from '../action-creator.sync.js'
  12. import {
  13. getAppList,
  14. getWorkspaceContent
  15. } from '../action-creator.async.js'
  16. class WorkspaceContent extends React.Component {
  17. constructor (props) {
  18. super(props)
  19. this.state = {
  20. activeFileType: ''
  21. }
  22. }
  23. componentDidMount () {
  24. const { workspaceList, app, match, dispatch } = this.props
  25. if (match.params.idws !== undefined) dispatch(getWorkspaceContent(match.params.idws))
  26. else if (workspaceList.length > 0) dispatch(getWorkspaceContent(workspaceList[0].id)) // load first ws if none specified
  27. Object.keys(app).length === 0 && dispatch(getAppList())
  28. }
  29. componentDidUpdate (prevProps) {
  30. const { workspace, workspaceList, match, dispatch } = this.props
  31. // if a workspace is already loaded and the idws in url hasn't changed, do nothing
  32. if (workspace.id !== -1 && prevProps.match.params.idws === match.params.idws) return
  33. // if the idws in url has changed, load the new workspace
  34. if (match.params.idws !== undefined) dispatch(getWorkspaceContent(match.params.idws))
  35. // else bellow is for loading url PAGE_NAME.HOME (without an idws), when workspaceList is loaded, load the first workspace
  36. else if (match.params.idws === undefined && workspace.id === -1 && workspaceList.length > 0) dispatch(getWorkspaceContent(workspaceList[0].id))
  37. }
  38. handleClickContentItem = content => {
  39. const { user, workspace, dispatch } = this.props
  40. GLOBAL_renderApp({
  41. workspace: {
  42. id: workspace.id,
  43. title: workspace.title
  44. },
  45. appConfig: {
  46. ...this.props.app[content.type],
  47. apiUrl: FETCH_CONFIG.apiUrl
  48. },
  49. loggedUser: user.isLoggedIn ? user : {},
  50. content
  51. })
  52. dispatch(setActiveFileContentActive(content))
  53. }
  54. render () {
  55. const { workspace, app } = this.props
  56. return (
  57. <PageWrapper customeClass='workspace'>
  58. <PageTitle
  59. parentClass='workspace__header'
  60. customClass='justify-content-between'
  61. title={workspace.title}
  62. >
  63. <DropdownCreateButton parentClass='workspace__header__btnaddworkspace' />
  64. </PageTitle>
  65. <PageContent parentClass='workspace__content'>
  66. <div className='workspace__content__fileandfolder folder__content active'>
  67. <FileItemHeader />
  68. { workspace.content.map((c, i) => c.type === 'folder'
  69. ? <Folder app={app} folderData={c} key={c.id} isLast={i === workspace.content.length - 1} />
  70. : (
  71. <FileItem
  72. name={c.title}
  73. type={c.type}
  74. icon={(app[c.type] || {icon: ''}).icon}
  75. status={c.status}
  76. onClickItem={() => this.handleClickContentItem(c)}
  77. isLast={i === workspace.content.length - 1}
  78. key={c.id}
  79. />
  80. )
  81. )}
  82. </div>
  83. <DropdownCreateButton customClass='workspace__content__button mb-5' />
  84. <div id='appContainer' />
  85. </PageContent>
  86. </PageWrapper>
  87. )
  88. }
  89. }
  90. const mapStateToProps = ({ user, workspace, workspaceList, app }) => ({ user, workspace, workspaceList, app })
  91. export default connect(mapStateToProps)(WorkspaceContent)