Sidebar.jsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 appFactory from '../appFactory.js'
  7. import WorkspaceListItem from '../component/Sidebar/WorkspaceListItem.jsx'
  8. import {
  9. setWorkspaceListIsOpenInSidebar
  10. } from '../action-creator.sync.js'
  11. import { PAGE, workspaceConfig } 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. workspaceListLoaded: false,
  19. workspaceIdInUrl: props.match && props.match.params.idws ? parseInt(props.match.params.idws) : null
  20. }
  21. document.addEventListener('appCustomEvent', this.customEventReducer)
  22. }
  23. customEventReducer = async ({ detail: { type, data } }) => {
  24. // switch (type) {
  25. // default:
  26. // return
  27. // }
  28. }
  29. componentDidUpdate (prevProps, prevState) {
  30. const { props } = this
  31. if (!this.shouldDisplaySidebar()) return
  32. // console.log('%c<Sidebar> Did Update', 'color: #c17838')
  33. if (props.match && props.match.params.idws !== undefined && !isNaN(props.match.params.idws)) {
  34. const newWorkspaceId = parseInt(props.match.params.idws)
  35. if (prevState.workspaceIdInUrl !== newWorkspaceId) this.setState({workspaceIdInUrl: newWorkspaceId})
  36. }
  37. }
  38. shouldDisplaySidebar = () => {
  39. const pageWithoutSidebar = [PAGE.LOGIN]
  40. return !pageWithoutSidebar.includes(this.props.location.pathname)
  41. }
  42. handleClickWorkspace = (idWs, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(idWs, newIsOpenInSidebar))
  43. handleClickAllContent = idWs => this.props.history.push(PAGE.WORKSPACE.CONTENT_LIST(idWs))
  44. handleClickToggleSidebar = () => this.setState(prev => ({sidebarClose: !prev.sidebarClose}))
  45. handleClickNewWorkspace = () => this.props.renderAppPopupCreation(workspaceConfig, this.props.user, null, null)
  46. render () {
  47. const { sidebarClose, workspaceIdInUrl } = this.state
  48. const { activeLang, workspaceList, t } = this.props
  49. if (!this.shouldDisplaySidebar()) return null
  50. return (
  51. <div className={classnames('sidebar primaryColorBgDarken', {'sidebarclose': sidebarClose})}>
  52. <div className='sidebar__expand primaryColorBg' onClick={this.handleClickToggleSidebar}>
  53. <i className={classnames('fa fa-chevron-left', {'fa-chevron-right': sidebarClose, 'fa-chevron-left': !sidebarClose})} />
  54. </div>
  55. <div className='sidebar__content'>
  56. <nav className={classnames('sidebar__content__navigation', {'sidebarclose': sidebarClose})}>
  57. <ul className='sidebar__content__navigation__workspace'>
  58. { workspaceList.map(ws =>
  59. <WorkspaceListItem
  60. idWs={ws.id}
  61. label={ws.label}
  62. allowedApp={ws.sidebarEntry}
  63. lang={activeLang}
  64. activeFilterList={ws.id === workspaceIdInUrl ? [qs.parse(this.props.location.search).type] : []}
  65. isOpenInSidebar={ws.isOpenInSidebar}
  66. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpenInSidebar)}
  67. onClickAllContent={this.handleClickAllContent}
  68. key={ws.id}
  69. />
  70. )}
  71. </ul>
  72. </nav>
  73. <div className='sidebar__content__btnnewworkspace'>
  74. <button
  75. className='sidebar__content__btnnewworkspace__btn btn btn-primary primaryColorBg primaryColorBorder primaryColorBorderDarkenHover mb-5'
  76. onClick={this.handleClickNewWorkspace}
  77. >
  78. {t('Create a workspace')}
  79. </button>
  80. </div>
  81. </div>
  82. <div className='sidebar__footer mb-2'>
  83. <div className='sidebar__footer__text whiteFontColor d-flex align-items-end justify-content-center'>
  84. Copyright - 2013 - 2018
  85. <div className='sidebar__footer__text__link'>
  86. <a href='http://www.tracim.fr/' target='_blank' className='ml-3'>tracim.fr</a>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. )
  92. }
  93. }
  94. const mapStateToProps = ({ lang, user, workspaceList }) => ({
  95. activeLang: lang.find(l => l.active) || {id: 'en'},
  96. user,
  97. workspaceList
  98. })
  99. export default withRouter(connect(mapStateToProps)(appFactory(translate()(Sidebar))))