Sidebar.jsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: props.match.params.idws ? parseInt(props.match.params.idws) : null
  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. if (this.state.workspaceIdInUrl === null) return
  29. const newWorkspaceId = parseInt(match.params.idws)
  30. prevState.workspaceIdInUrl !== newWorkspaceId && this.setState({workspaceIdInUrl: newWorkspaceId})
  31. user.id !== -1 && prevProps.user.id !== user.id && dispatch(getWorkspaceList(user.id, newWorkspaceId))
  32. }
  33. handleClickWorkspace = (wsId, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(wsId, newIsOpenInSidebar))
  34. handleClickAllContent = wsId => {
  35. this.props.dispatch(updateWorkspaceFilter([]))
  36. this.props.history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}`)
  37. }
  38. handleClickContentFilter = (wsId, filter) => {
  39. const { workspace, history, dispatch } = this.props
  40. const newFilter = workspace.filter.includes(filter) ? [] : [filter] // use an array to allow multiple filters (NYI)
  41. dispatch(updateWorkspaceFilter(newFilter))
  42. history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}/${newFilter.join(';')}`) // workspace.filter gets updated on react redraw from match.params
  43. }
  44. handleClickToggleSidebar = () => this.setState(prev => ({sidebarClose: !prev.sidebarClose}))
  45. render () {
  46. const { sidebarClose, workspaceIdInUrl } = this.state
  47. const { activeLang, workspace, workspaceList, app, t } = this.props
  48. return (
  49. <div className={classnames('sidebar', {'sidebarclose': sidebarClose})}>
  50. <div className='sidebarSticky'>
  51. <div className='sidebar__expand' onClick={this.handleClickToggleSidebar}>
  52. <i className={classnames('fa fa-chevron-left', {'fa-chevron-right': sidebarClose, 'fa-chevron-left': !sidebarClose})} />
  53. </div>
  54. <nav className='sidebar__navigation'>
  55. <ul className='sidebar__navigation__workspace'>
  56. { workspaceList.map((ws, i) =>
  57. <WorkspaceListItem
  58. number={++i}
  59. wsId={ws.id}
  60. name={ws.title}
  61. app={app}
  62. lang={activeLang}
  63. activeFilterList={ws.id === workspaceIdInUrl ? workspace.filter : []}
  64. isOpenInSidebar={ws.isOpenInSidebar}
  65. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpenInSidebar)}
  66. onClickAllContent={this.handleClickAllContent}
  67. onClickContentFilter={this.handleClickContentFilter}
  68. key={ws.id}
  69. />
  70. )}
  71. </ul>
  72. </nav>
  73. <div className='sidebar__btnnewworkspace'>
  74. <button className='sidebar__btnnewworkspace__btn btn btn-primary'>
  75. {t('Sidebar.create_new_workspace')}
  76. </button>
  77. </div>
  78. </div>
  79. </div>
  80. )
  81. }
  82. }
  83. const mapStateToProps = ({ lang, user, workspace, workspaceList, app }) => ({
  84. activeLang: lang.find(l => l.active) || {id: 'en'},
  85. user,
  86. workspace,
  87. workspaceList,
  88. app
  89. })
  90. export default withRouter(connect(mapStateToProps)(translate()(Sidebar)))