Sidebar.jsx 2.1KB

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