Sidebar.jsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. updateWorkspaceListData
  11. } from '../action-creator.sync.js'
  12. import {
  13. getWorkspaceList
  14. } from '../action-creator.async.js'
  15. import { PAGE } from '../helper.js'
  16. const qs = require('query-string')
  17. class Sidebar extends React.Component {
  18. constructor (props) {
  19. super(props)
  20. this.state = {
  21. sidebarClose: false,
  22. workspaceIdInUrl: props.match.params.idws ? parseInt(props.match.params.idws) : null
  23. }
  24. }
  25. async componentDidMount () {
  26. const { workspaceIdInUrl } = this.state
  27. const { user, workspaceList, dispatch } = this.props
  28. if (user.user_id !== -1 && workspaceList.length === 0) {
  29. const fetchGetWorkspaceList = await dispatch(getWorkspaceList(user))
  30. if (fetchGetWorkspaceList.status === 200) {
  31. dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
  32. dispatch(setWorkspaceListIsOpenInSidebar(workspaceIdInUrl || fetchGetWorkspaceList.json[0].workspace_id, true))
  33. }
  34. }
  35. }
  36. componentDidUpdate (prevProps, prevState) {
  37. // console.log('%c<Sidebar> Did Update', 'color: #c17838')
  38. if (this.props.match.params.idws === undefined || isNaN(this.props.match.params.idws)) return
  39. const newWorkspaceId = parseInt(this.props.match.params.idws)
  40. if (prevState.workspaceIdInUrl !== newWorkspaceId) this.setState({workspaceIdInUrl: newWorkspaceId})
  41. }
  42. handleClickWorkspace = (idWs, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(idWs, newIsOpenInSidebar))
  43. handleClickAllContent = idWs => {
  44. this.props.dispatch(updateWorkspaceFilter([]))
  45. this.props.history.push(PAGE.WORKSPACE.CONTENT_LIST(idWs))
  46. }
  47. // not used, right now, link on sidebar filters is a <Link>
  48. handleClickContentFilter = (idWs, filter) => {
  49. const { workspace, history } = this.props
  50. const newFilter = workspace.filter.includes(filter) ? [] : [filter] // use an array to allow multiple filters (NYI)
  51. history.push(`${PAGE.WORKSPACE.CONTENT_LIST(idWs)}?type=${newFilter.join(';')}`) // workspace.filter gets updated on react redraw from match.params
  52. // obviously, it's ugly to use custom event to tell WorkspaceContent to refresh, but since WorkspaceContent
  53. // will end up being an App, it'll have to be that way. So it's fine
  54. GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
  55. }
  56. handleClickToggleSidebar = () => this.setState(prev => ({sidebarClose: !prev.sidebarClose}))
  57. render () {
  58. const { sidebarClose, workspaceIdInUrl } = this.state
  59. const { activeLang, workspaceList, t } = this.props
  60. return (
  61. <div className={classnames('sidebar primaryColorBgDarken', {'sidebarclose': sidebarClose})}>
  62. <div className='sidebarSticky'>
  63. <div className='sidebar__expand primaryColorBg whiteColorBorder' onClick={this.handleClickToggleSidebar}>
  64. <i className={classnames('fa fa-chevron-left', {'fa-chevron-right': sidebarClose, 'fa-chevron-left': !sidebarClose})} />
  65. </div>
  66. <div className='sidebar__wrapper'>
  67. <nav className='sidebar__navigation'>
  68. <ul className='sidebar__navigation__workspace'>
  69. { workspaceList.map(ws =>
  70. <WorkspaceListItem
  71. idWs={ws.id}
  72. label={ws.label}
  73. allowedApp={ws.sidebarEntry}
  74. lang={activeLang}
  75. activeFilterList={ws.id === workspaceIdInUrl ? [qs.parse(this.props.location.search).type] : []}
  76. isOpenInSidebar={ws.isOpenInSidebar}
  77. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpenInSidebar)}
  78. onClickAllContent={this.handleClickAllContent}
  79. // onClickContentFilter={this.handleClickContentFilter}
  80. key={ws.id}
  81. />
  82. )}
  83. </ul>
  84. </nav>
  85. <div className='sidebar__btnnewworkspace'>
  86. <button className='sidebar__btnnewworkspace__btn btn btn-primary primaryColorBg primaryColorBorder primaryColorBorderDarkenHover mb-5'>
  87. {t('Create a workspace')}
  88. </button>
  89. </div>
  90. </div>
  91. <div className='sidebar__footer mb-2'>
  92. <div className='sidebar__footer__text whiteFontColor d-flex align-items-end justify-content-center'>
  93. Copyright - 2013 - 2018
  94. <div className='sidebar__footer__text__link'>
  95. <a href='http://www.tracim.fr/' target='_blank' className='ml-3'>tracim.fr</a>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. )
  102. }
  103. }
  104. const mapStateToProps = ({ lang, user, workspace, workspaceList, app }) => ({
  105. activeLang: lang.find(l => l.active) || {id: 'en'},
  106. user,
  107. workspace,
  108. workspaceList,
  109. app
  110. })
  111. export default withRouter(connect(mapStateToProps)(translate()(Sidebar)))