import React from 'react' import { connect } from 'react-redux' import { withRouter } from 'react-router' import { translate } from 'react-i18next' import WorkspaceListItem from '../component/Sidebar/WorkspaceListItem.jsx' import { getWorkspaceList } from '../action-creator.async.js' import { setWorkspaceListIsOpenInSidebar, updateWorkspaceFilter } from '../action-creator.sync.js' import { PAGE_NAME } from '../helper.js' class Sidebar extends React.Component { constructor (props) { super(props) this.state = { firstWsOpen: false, workspaceIdInUrl: parseInt(props.match.params.idws) } } componentDidMount () { const { workspaceIdInUrl } = this.state const { user, workspaceList, dispatch } = this.props user.id !== -1 && workspaceList.length === 0 && dispatch(getWorkspaceList(user.id, workspaceIdInUrl)) } componentDidUpdate (prevProps, prevState) { const { user, match, dispatch } = this.props const newWorkspaceId = parseInt(match.params.idws) prevState.workspaceIdInUrl !== newWorkspaceId && this.setState({workspaceIdInUrl: newWorkspaceId}) user.id !== -1 && prevProps.user.id !== user.id && dispatch(getWorkspaceList(user.id, newWorkspaceId)) } handleClickWorkspace = (wsId, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(wsId, newIsOpenInSidebar)) handleClickAllContent = wsId => { this.props.history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}`) } handleClickContentFilter = (wsId, filter) => { const { workspaceIdInUrl } = this.state const { workspace, history, dispatch } = this.props const filterList = (() => { if (wsId !== workspaceIdInUrl) return [filter] // load a different workspace => reset filters if (workspace.filter.includes(filter)) return workspace.filter.filter(f => f !== filter) // remove the filter else return [...workspace.filter, filter] // add the filter })() dispatch(updateWorkspaceFilter(filterList)) history.push(`${PAGE_NAME.WS_CONTENT}/${wsId}/${filterList.join(';')}`) // workspace.filter gets updated on react redraw from match.params } render () { const { workspaceIdInUrl } = this.state const { activeLang, workspace, workspaceList, app, t } = this.props return (