Sidebar.jsx 5.7KB

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