121 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-08-21 15:12:53 +02:00
/*
* AdminPage
*
* This is the first thing users see of our AdminPage, at the '/' route
*
* NOTE: while this component should technically be a stateless functional
* component (SFC), hot reloading does not currently support SFCs. If hot
* reloading is not a neccessity for you then you can refactor it and remove
* the linting exception.
*/
import React from 'react';
import PropTypes from 'prop-types';
2017-08-21 15:12:53 +02:00
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { Switch, Route } from 'react-router-dom';
import { get, includes, isUndefined } from 'lodash';
2017-08-21 15:12:53 +02:00
2017-11-14 15:19:48 +01:00
import { updatePlugin } from 'containers/App/actions';
import { selectPlugins } from 'containers/App/selectors';
import { hideNotification } from 'containers/NotificationProvider/actions';
// Design
2017-08-21 15:12:53 +02:00
import HomePage from 'containers/HomePage';
import PluginPage from 'containers/PluginPage';
import ComingSoonPage from 'containers/ComingSoonPage';
import LeftMenu from 'containers/LeftMenu';
import ListPluginsPage from 'containers/ListPluginsPage';
2017-08-21 15:12:53 +02:00
import Content from 'containers/Content';
import NotFoundPage from 'containers/NotFoundPage';
import Header from 'components/Header/index';
2017-11-14 15:19:48 +01:00
import auth from 'utils/auth';
import styles from './styles.scss';
2017-08-21 15:12:53 +02:00
export class AdminPage extends React.Component { // eslint-disable-line react/prefer-stateless-function
getChildContext = () => (
{
plugins: this.props.plugins,
updatePlugin: this.props.updatePlugin,
}
2017-11-14 15:19:48 +01:00
);
componentDidMount() {
if (this.hasUsersPlugin() && this.isUrlProtected(this.props) && !auth.getToken()) {
this.props.history.push('/plugins/users-permissions/auth/login');
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.location.pathname !== this.props.location.pathname) {
if (this.hasUsersPlugin() && this.isUrlProtected(nextProps) && !auth.getToken()) {
this.props.history.push('/plugins/users-permissions/auth/login');
}
}
}
hasUsersPlugin = () => !isUndefined(get(this.props.plugins.toJS(), 'users-permissions'));
isUrlProtected = (props) => !includes(props.location.pathname, '/plugins/users-permissions/auth');
2017-11-14 15:26:12 +01:00
showLeftMenu = () => !includes(this.props.location.pathname, '/plugins/users-permissions/auth');
2017-08-21 15:12:53 +02:00
render() {
const leftMenu = this.showLeftMenu() ? <LeftMenu plugins={this.props.plugins} /> : '';
const header = this.showLeftMenu() ? <Header /> : '';
const style = this.showLeftMenu() ? {} : { width: '100%' };
2017-08-21 15:12:53 +02:00
return (
<div className={styles.adminPage}>
{leftMenu}
<div className={styles.adminPageRightWrapper} style={style}>
{header}
<Content {...this.props} showLeftMenu={this.showLeftMenu()}>
2017-08-21 15:12:53 +02:00
<Switch>
<Route path="/" component={HomePage} exact />
<Route path="/plugins/:pluginId" component={PluginPage} />
<Route path="/plugins" component={ComingSoonPage} />
<Route path="/list-plugins" component={ListPluginsPage} exact />
<Route path="/install-plugin" component={ComingSoonPage} exact />
<Route path="/configuration" component={ComingSoonPage} exact />
2017-08-21 15:12:53 +02:00
<Route path="" component={NotFoundPage} />
</Switch>
</Content>
</div>
</div>
);
}
}
AdminPage.childContextTypes = {
plugins: PropTypes.object,
updatePlugin: PropTypes.func,
};
2017-08-21 15:12:53 +02:00
AdminPage.contextTypes = {
router: PropTypes.object.isRequired,
2017-08-21 15:12:53 +02:00
};
AdminPage.propTypes = {
2017-11-14 15:19:48 +01:00
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
plugins: PropTypes.object.isRequired,
updatePlugin: PropTypes.func.isRequired,
2017-08-21 15:12:53 +02:00
};
const mapStateToProps = createStructuredSelector({
plugins: selectPlugins(),
});
function mapDispatchToProps(dispatch) {
return {
onHideNotification: (id) => { dispatch(hideNotification(id)); },
updatePlugin: (pluginId, updatedKey, updatedValue) => { dispatch(updatePlugin(pluginId, updatedKey, updatedValue)); },
2017-08-21 15:12:53 +02:00
dispatch,
};
}
export default connect(mapStateToProps, mapDispatchToProps)(AdminPage);