WorkspaceContent.jsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. } 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, match.params.filter))
  26. else if (workspaceList.length > 0) dispatch(getWorkspaceContent(workspaceList[0].id, match.params.filter)) // load first ws if none specified
  27. if (Object.keys(app).length === 0) dispatch(getAppList())
  28. }
  29. componentDidUpdate (prevProps) {
  30. const { workspace, workspaceList, match, dispatch } = this.props
  31. console.log('workspaceContent update', prevProps, this.props)
  32. // if a workspace is already loaded and the idws in url hasn't changed, do nothing
  33. if (workspace.id !== -1 && prevProps.match.params.idws === match.params.idws) return
  34. // if the idws in url has changed, load the new workspace
  35. if (match.params.idws !== undefined) dispatch(getWorkspaceContent(match.params.idws, match.params.filter))
  36. // else bellow is for loading url PAGE_NAME.HOME (without an idws), when workspaceList is loaded, load the first workspace
  37. else if (workspace.id === -1 && workspaceList.length > 0) dispatch(getWorkspaceContent(workspaceList[0].id))
  38. }
  39. handleClickContentItem = content => {
  40. this.props.renderApp(this.props.user, this.props.workspace, this.props.app, content)
  41. // 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
  42. // dispatch(setActiveFileContentActive(content))
  43. }
  44. filterWorkspaceContent = (contentList, filter) => filter.length === 0
  45. ? contentList
  46. : contentList.filter(c => c.type === 'folder' || filter.includes(c.type)) // keep unfiltered files and folders
  47. .map(c => c.type !== 'folder' ? c : {...c, content: this.filterWorkspaceContent(c.content, filter)}) // recursively filter folder content
  48. .filter(c => c.type !== 'folder' || c.content.length > 0) // remove empty folder
  49. render () {
  50. const { workspace, app } = this.props
  51. const filteredWorkspaceContent = this.filterWorkspaceContent(workspace.content, workspace.filter)
  52. return (
  53. <div className='sidebarpagecontainer'>
  54. <Sidebar />
  55. <PageWrapper customeClass='workspace'>
  56. <PageTitle
  57. parentClass='workspace__header'
  58. customClass='justify-content-between'
  59. title={workspace.title}
  60. >
  61. <DropdownCreateButton parentClass='workspace__header__btnaddworkspace' />
  62. </PageTitle>
  63. <PageContent parentClass='workspace__content'>
  64. <div className='workspace__content__fileandfolder folder__content active'>
  65. <FileItemHeader />
  66. { filteredWorkspaceContent.map((c, i) => c.type === 'folder'
  67. ? (
  68. <Folder
  69. app={app}
  70. folderData={c}
  71. onClickItem={this.handleClickContentItem}
  72. isLast={i === filteredWorkspaceContent.length - 1}
  73. key={c.id}
  74. />
  75. )
  76. : (
  77. <FileItem
  78. name={c.title}
  79. type={c.type}
  80. icon={(app[c.type] || {icon: ''}).icon}
  81. status={c.status}
  82. onClickItem={() => this.handleClickContentItem(c)}
  83. isLast={i === filteredWorkspaceContent.length - 1}
  84. key={c.id}
  85. />
  86. )
  87. )}
  88. </div>
  89. <DropdownCreateButton customClass='workspace__content__button mb-5' />
  90. <div id='appContainer' />
  91. </PageContent>
  92. </PageWrapper>
  93. </div>
  94. )
  95. }
  96. }
  97. const mapStateToProps = ({ user, workspace, workspaceList, app }) => ({ user, workspace, workspaceList, app })
  98. export default connect(mapStateToProps)(appFactory(WorkspaceContent))