Sidebar.jsx 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const qs = require('query-string')
  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. componentDidUpdate (prevProps, prevState) {
  22. if (this.props.match.params.idws === undefined) return
  23. const newWorkspaceId = parseInt(this.props.match.params.idws)
  24. if (prevState.workspaceIdInUrl !== newWorkspaceId) this.setState({workspaceIdInUrl: newWorkspaceId})
  25. }
  26. handleClickWorkspace = (idWs, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(idWs, newIsOpenInSidebar))
  27. handleClickAllContent = idWs => {
  28. this.props.dispatch(updateWorkspaceFilter([]))
  29. this.props.history.push(PAGE.WORKSPACE.CONTENT_LIST(idWs))
  30. }
  31. handleClickContentFilter = (idWs, filter) => {
  32. const { workspace, history } = this.props
  33. const newFilter = workspace.filter.includes(filter) ? [] : [filter] // use an array to allow multiple filters (NYI)
  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, workspaceList, 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. idWs={ws.id}
  51. label={ws.label}
  52. allowedApp={ws.sidebarEntry}
  53. lang={activeLang}
  54. activeFilterList={ws.id === workspaceIdInUrl ? [qs.parse(this.props.location.search).type] : []}
  55. isOpenInSidebar={ws.isOpenInSidebar}
  56. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpenInSidebar)}
  57. onClickAllContent={this.handleClickAllContent}
  58. onClickContentFilter={this.handleClickContentFilter}
  59. key={ws.id}
  60. />
  61. )}
  62. </ul>
  63. </nav>
  64. <div className='sidebar__btnnewworkspace'>
  65. <button className='sidebar__btnnewworkspace__btn btn btn-primary mb-5'>
  66. {t('Sidebar.create_new_workspace')}
  67. </button>
  68. </div>
  69. </div>
  70. </div>
  71. )
  72. }
  73. }
  74. const mapStateToProps = ({ lang, user, workspace, workspaceList, app }) => ({
  75. activeLang: lang.find(l => l.active) || {id: 'en'},
  76. user,
  77. workspace,
  78. workspaceList,
  79. app
  80. })
  81. export default withRouter(connect(mapStateToProps)(translate()(Sidebar)))