Sidebar.jsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 {
  8. setWorkspaceListIsOpenInSidebar,
  9. updateWorkspaceFilter
  10. } from '../action-creator.sync.js'
  11. import { PAGE } from '../helper.js'
  12. class Sidebar extends React.Component {
  13. constructor (props) {
  14. super(props)
  15. this.state = {
  16. sidebarClose: false,
  17. workspaceIdInUrl: props.match.params.idws ? parseInt(props.match.params.idws) : null
  18. }
  19. }
  20. componentDidUpdate (prevProps, prevState) {
  21. if (this.props.match.params.idws === undefined) return
  22. const newWorkspaceId = parseInt(this.props.match.params.idws)
  23. if (prevState.workspaceIdInUrl !== newWorkspaceId) this.setState({workspaceIdInUrl: newWorkspaceId})
  24. }
  25. handleClickWorkspace = (idWs, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(idWs, newIsOpenInSidebar))
  26. handleClickAllContent = idWs => {
  27. this.props.dispatch(updateWorkspaceFilter([]))
  28. this.props.history.push(PAGE.WORKSPACE.CONTENT_LIST(idWs))
  29. }
  30. handleClickContentFilter = (idWs, filter) => {
  31. const { workspace, history, dispatch } = this.props
  32. const newFilter = workspace.filter.includes(filter) ? [] : [filter] // use an array to allow multiple filters (NYI)
  33. dispatch(updateWorkspaceFilter(newFilter))
  34. history.push(`${PAGE.WORKSPACE.CONTENT_LIST(idWs)}?type=${newFilter.join(';')}`) // workspace.filter gets updated on react redraw from match.params
  35. }
  36. handleClickToggleSidebar = () => this.setState(prev => ({sidebarClose: !prev.sidebarClose}))
  37. render () {
  38. const { sidebarClose, workspaceIdInUrl } = this.state
  39. const { activeLang, workspace, workspaceList, app, t } = this.props
  40. return (
  41. <div className={classnames('sidebar', {'sidebarclose': sidebarClose})}>
  42. <div className='sidebarSticky'>
  43. <div className='sidebar__expand' onClick={this.handleClickToggleSidebar}>
  44. <i className={classnames('fa fa-chevron-left', {'fa-chevron-right': sidebarClose, 'fa-chevron-left': !sidebarClose})} />
  45. </div>
  46. <nav className='sidebar__navigation'>
  47. <ul className='sidebar__navigation__workspace'>
  48. { workspaceList.map((ws, i) =>
  49. <WorkspaceListItem
  50. number={++i}
  51. idWs={ws.id}
  52. name={ws.title}
  53. app={app}
  54. lang={activeLang}
  55. activeFilterList={ws.id === workspaceIdInUrl ? workspace.filter : []}
  56. isOpenInSidebar={ws.isOpenInSidebar}
  57. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpenInSidebar)}
  58. onClickAllContent={this.handleClickAllContent}
  59. onClickContentFilter={this.handleClickContentFilter}
  60. key={ws.id}
  61. />
  62. )}
  63. </ul>
  64. </nav>
  65. <div className='sidebar__btnnewworkspace'>
  66. <button className='sidebar__btnnewworkspace__btn btn btn-primary'>
  67. {t('Sidebar.create_new_workspace')}
  68. </button>
  69. </div>
  70. </div>
  71. </div>
  72. )
  73. }
  74. }
  75. const mapStateToProps = ({ lang, user, workspace, workspaceList, app }) => ({
  76. activeLang: lang.find(l => l.active) || {id: 'en'},
  77. user,
  78. workspace,
  79. workspaceList,
  80. app
  81. })
  82. export default withRouter(connect(mapStateToProps)(translate()(Sidebar)))