Sidebar.jsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { withRouter } from 'react-router'
  4. import { translate } from 'react-i18next'
  5. import WorkspaceListItem from '../component/Sidebar/WorkspaceListItem.jsx'
  6. import { getWorkspaceList } from '../action-creator.async.js'
  7. import {
  8. setWorkspaceListIsOpenInSidebar,
  9. updateWorkspaceFilter
  10. } from '../action-creator.sync.js'
  11. import { PAGE_NAME } from '../helper.js'
  12. class Sidebar extends React.Component {
  13. constructor (props) {
  14. super(props)
  15. this.state = {
  16. firstWsOpen: false,
  17. workspaceIdInUrl: parseInt(props.match.params.idws)
  18. }
  19. }
  20. componentDidMount () {
  21. const { workspaceIdInUrl } = this.state
  22. const { user, workspaceList, dispatch } = this.props
  23. user.id !== -1 && workspaceList.length === 0 && dispatch(getWorkspaceList(user.id, workspaceIdInUrl))
  24. }
  25. componentDidUpdate (prevProps, prevState) {
  26. const { user, match, dispatch } = this.props
  27. const newWorkspaceId = parseInt(match.params.idws)
  28. prevState.workspaceIdInUrl !== newWorkspaceId && this.setState({workspaceIdInUrl: newWorkspaceId})
  29. user.id !== -1 && prevProps.user.id !== user.id && dispatch(getWorkspaceList(user.id, newWorkspaceId))
  30. }
  31. handleClickWorkspace = (wsId, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(wsId, newIsOpenInSidebar))
  32. handleClickAllContent = wsId => {
  33. this.props.history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}`)
  34. }
  35. handleClickContentFilter = (wsId, filter) => {
  36. const { workspaceIdInUrl } = this.state
  37. const { workspace, history, dispatch } = this.props
  38. const filterList = (() => {
  39. if (wsId !== workspaceIdInUrl) return [filter] // load a different workspace => reset filters
  40. if (workspace.filter.includes(filter)) return workspace.filter.filter(f => f !== filter) // remove the filter
  41. else return [...workspace.filter, filter] // add the filter
  42. })()
  43. dispatch(updateWorkspaceFilter(filterList))
  44. history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}/${filterList.join(';')}`) // workspace.filter gets updated on react redraw from match.params
  45. }
  46. render () {
  47. const { workspaceIdInUrl } = this.state
  48. const { activeLang, workspace, workspaceList, app, t } = this.props
  49. return (
  50. <div className='sidebar d-none d-lg-block'>
  51. <nav className='sidebar__navigation'>
  52. <ul className='sidebar__navigation__workspace'>
  53. { workspaceList.map((ws, i) =>
  54. <WorkspaceListItem
  55. number={++i}
  56. wsId={ws.id}
  57. name={ws.title}
  58. app={app}
  59. lang={activeLang}
  60. activeFilterList={ws.id === workspaceIdInUrl ? workspace.filter : []}
  61. isOpenInSidebar={ws.isOpenInSidebar}
  62. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpenInSidebar)}
  63. onClickAllContent={this.handleClickAllContent}
  64. onClickContentFilter={this.handleClickContentFilter}
  65. key={ws.id}
  66. />
  67. )}
  68. </ul>
  69. </nav>
  70. <div className='sidebar__btnnewworkspace'>
  71. <button className='sidebar__btnnewworkspace__btn btn btn-success'>
  72. {t('Sidebar.create_new_workspace')}
  73. </button>
  74. </div>
  75. </div>
  76. )
  77. }
  78. }
  79. const mapStateToProps = ({ lang, user, workspace, workspaceList, app }) => ({
  80. activeLang: lang.find(l => l.active) || {id: 'en'},
  81. user,
  82. workspace,
  83. workspaceList,
  84. app
  85. })
  86. export default withRouter(connect(mapStateToProps)(translate()(Sidebar)))