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. 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. console.log('<Sidebar> Did Update')
  23. if (this.props.match.params.idws === undefined || isNaN(this.props.match.params.idws)) return
  24. const newWorkspaceId = parseInt(this.props.match.params.idws)
  25. if (prevState.workspaceIdInUrl !== newWorkspaceId) this.setState({workspaceIdInUrl: newWorkspaceId})
  26. }
  27. handleClickWorkspace = (idWs, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(idWs, newIsOpenInSidebar))
  28. handleClickAllContent = idWs => {
  29. this.props.dispatch(updateWorkspaceFilter([]))
  30. this.props.history.push(PAGE.WORKSPACE.CONTENT_LIST(idWs))
  31. }
  32. handleClickContentFilter = (idWs, filter) => {
  33. const { workspace, history } = this.props
  34. const newFilter = workspace.filter.includes(filter) ? [] : [filter] // use an array to allow multiple filters (NYI)
  35. history.push(`${PAGE.WORKSPACE.CONTENT_LIST(idWs)}?type=${newFilter.join(';')}`) // workspace.filter gets updated on react redraw from match.params
  36. }
  37. handleClickToggleSidebar = () => this.setState(prev => ({sidebarClose: !prev.sidebarClose}))
  38. render () {
  39. const { sidebarClose, workspaceIdInUrl } = this.state
  40. const { activeLang, workspaceList, t } = this.props
  41. return (
  42. <div className={classnames('sidebar', {'sidebarclose': sidebarClose})}>
  43. <div className='sidebarSticky'>
  44. <div className='sidebar__expand' onClick={this.handleClickToggleSidebar}>
  45. <i className={classnames('fa fa-chevron-left', {'fa-chevron-right': sidebarClose, 'fa-chevron-left': !sidebarClose})} />
  46. </div>
  47. <nav className='sidebar__navigation'>
  48. <ul className='sidebar__navigation__workspace'>
  49. { workspaceList.map(ws =>
  50. <WorkspaceListItem
  51. idWs={ws.id}
  52. label={ws.label}
  53. allowedApp={ws.sidebarEntry}
  54. lang={activeLang}
  55. activeFilterList={ws.id === workspaceIdInUrl ? [qs.parse(this.props.location.search).type] : []}
  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 mb-5'>
  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)))