Sidebar.jsx 3.5KB

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