Sidebar.jsx 3.9KB

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