Sidebar.jsx 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { withRouter } from 'react-router'
  4. import WorkspaceListItem from '../component/Sidebar/WorkspaceListItem.jsx'
  5. import { getWorkspaceList } from '../action-creator.async.js'
  6. import { updateWorkspaceListIsOpen } from '../action-creator.sync.js'
  7. import { PAGE_NAME } from '../helper.js'
  8. class Sidebar extends React.Component {
  9. constructor (props) {
  10. super(props)
  11. this.state = {
  12. firstWsOpen: false
  13. }
  14. }
  15. componentDidMount () {
  16. const { user, workspaceList, dispatch } = this.props
  17. user.id !== 0 && workspaceList.length === 0 && dispatch(getWorkspaceList(user.id))
  18. }
  19. componentDidUpdate (prevProps) {
  20. const { user, dispatch } = this.props
  21. user.id !== 0 && prevProps.user.id !== user.id && dispatch(getWorkspaceList(user.id))
  22. }
  23. handleClickWorkspace = (wsId, newIsOpen) => this.props.dispatch(updateWorkspaceListIsOpen(wsId, newIsOpen))
  24. handleClickAllContent = wsId => {
  25. this.props.history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}`)
  26. }
  27. render () {
  28. const { workspaceList } = this.props
  29. return (
  30. <div className='sidebar d-none d-lg-table-cell'>
  31. <nav className='sidebar__navigation'>
  32. <ul className='sidebar__navigation__workspace'>
  33. { workspaceList.map((ws, i) =>
  34. <WorkspaceListItem
  35. number={++i}
  36. wsId={ws.id}
  37. name={ws.title}
  38. isOpen={ws.isOpen}
  39. onClickTitle={() => this.handleClickWorkspace(ws.id, !ws.isOpen)}
  40. onClickAllContent={this.handleClickAllContent}
  41. key={ws.id}
  42. />
  43. )}
  44. </ul>
  45. </nav>
  46. <div className='sidebar__btnnewworkspace'>
  47. <button className='sidebar__btnnewworkspace__btn btn btn-success'>
  48. Créer un workspace
  49. </button>
  50. </div>
  51. </div>
  52. )
  53. }
  54. }
  55. const mapStateToProps = ({ user, workspaceList }) => ({ user, workspaceList })
  56. export default withRouter(connect(mapStateToProps)(Sidebar))