WorkspaceContent.jsx 4.7KB

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