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.
|
|
|
|
*/
|
|
|
|
|
2017-09-26 16:36:28 +02:00
|
|
|
import React from 'react';
|
2017-09-20 11:12:04 +02:00
|
|
|
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';
|
2017-12-05 13:49:31 +01:00
|
|
|
import { get, includes, isFunction, map, omit } from 'lodash';
|
2017-08-21 15:12:53 +02:00
|
|
|
|
2017-12-01 15:45:11 +01:00
|
|
|
import { pluginLoaded, updatePlugin } from 'containers/App/actions';
|
2017-12-05 13:49:31 +01:00
|
|
|
import { selectHasUserPlugin, selectPlugins } from 'containers/App/selectors';
|
2017-11-14 15:19:48 +01:00
|
|
|
import { hideNotification } from 'containers/NotificationProvider/actions';
|
|
|
|
|
|
|
|
// Design
|
2017-08-21 15:12:53 +02:00
|
|
|
import ComingSoonPage from 'containers/ComingSoonPage';
|
2017-11-28 13:24:26 +01:00
|
|
|
import Content from 'containers/Content';
|
|
|
|
import Header from 'components/Header/index';
|
|
|
|
import HomePage from 'containers/HomePage';
|
2017-12-04 17:32:45 +01:00
|
|
|
// import InstallPluginPage from 'containers/InstallPluginPage';
|
2017-08-21 15:12:53 +02:00
|
|
|
import LeftMenu from 'containers/LeftMenu';
|
2017-11-02 11:36:38 +01:00
|
|
|
import ListPluginsPage from 'containers/ListPluginsPage';
|
2017-11-28 13:24:26 +01:00
|
|
|
import Logout from 'components/Logout';
|
2017-08-21 15:12:53 +02:00
|
|
|
import NotFoundPage from 'containers/NotFoundPage';
|
2017-11-28 13:24:26 +01:00
|
|
|
import PluginPage from 'containers/PluginPage';
|
2017-08-21 15:12:53 +02:00
|
|
|
|
2017-11-14 15:19:48 +01:00
|
|
|
import auth from 'utils/auth';
|
|
|
|
|
2017-09-14 11:10:05 +02:00
|
|
|
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
|
2017-12-01 15:45:11 +01:00
|
|
|
state = { hasAlreadyRegistereOtherPlugins: false };
|
|
|
|
|
2017-10-10 12:25:40 +02:00
|
|
|
getChildContext = () => (
|
|
|
|
{
|
|
|
|
plugins: this.props.plugins,
|
|
|
|
updatePlugin: this.props.updatePlugin,
|
|
|
|
}
|
2017-11-14 15:19:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-11-20 14:45:27 +01:00
|
|
|
this.checkLogin(this.props);
|
2017-11-14 15:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.location.pathname !== this.props.location.pathname) {
|
2017-12-04 17:32:45 +01:00
|
|
|
|
2017-11-20 14:45:27 +01:00
|
|
|
this.checkLogin(nextProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkLogin = (props) => {
|
2017-12-05 13:49:31 +01:00
|
|
|
if (this.props.hasUserPlugin && this.isUrlProtected(props) && !auth.getToken()) {
|
2017-11-20 14:45:27 +01:00
|
|
|
const endPoint = this.hasAdminUser() ? 'login': 'register';
|
|
|
|
this.props.history.push(`/plugins/users-permissions/auth/${endPoint}`);
|
2017-11-14 15:19:48 +01:00
|
|
|
}
|
2017-11-28 15:04:02 +01:00
|
|
|
|
2017-12-04 15:50:46 +01:00
|
|
|
// if (!this.isUrlProtected(props) && includes(props.location.pathname, 'login') && !this.hasAdminUser()) {
|
|
|
|
// this.props.history.push('/plugins/users-permissions/auth/register');
|
|
|
|
// }
|
2017-11-28 15:04:02 +01:00
|
|
|
|
|
|
|
if (!this.isUrlProtected(props) && includes(props.location.pathname, 'register') && this.hasAdminUser()) {
|
|
|
|
this.props.history.push('/plugins/users-permissions/auth/login');
|
|
|
|
}
|
2017-12-01 15:45:11 +01:00
|
|
|
|
2017-12-05 13:49:31 +01:00
|
|
|
if (!this.props.hasUserPlugin || auth.getToken() && !this.state.hasAlreadyRegistereOtherPlugins) {
|
2017-12-01 15:45:11 +01:00
|
|
|
map(omit(this.props.plugins.toJS(), ['users-permissions', 'email']), plugin => {
|
2017-12-05 13:49:31 +01:00
|
|
|
switch (true) {
|
|
|
|
case isFunction(plugin.bootstrap) && isFunction(plugin.pluginRequirements):
|
|
|
|
plugin.pluginRequirements(plugin)
|
|
|
|
.then(plugin => {
|
|
|
|
return plugin.bootstrap(plugin);
|
|
|
|
})
|
|
|
|
.then(plugin => this.props.pluginLoaded(plugin));
|
|
|
|
break;
|
|
|
|
case isFunction(plugin.pluginRequirements):
|
|
|
|
plugin.pluginRequirements(plugin).then(plugin => this.props.pluginLoaded(plugin));
|
|
|
|
break;
|
|
|
|
case isFunction(plugin.bootstrap):
|
|
|
|
plugin.bootstrap(plugin).then(plugin => this.props.pluginLoaded(plugin));
|
|
|
|
break;
|
|
|
|
default:
|
2017-12-01 15:45:11 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({ hasAlreadyRegistereOtherPlugins: true });
|
|
|
|
}
|
2017-11-14 15:19:48 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 16:05:06 +01:00
|
|
|
hasAdminUser = () => get(this.props.plugins.toJS(), ['users-permissions', 'hasAdminUser']);
|
|
|
|
|
2017-11-14 16:42:08 +01:00
|
|
|
isUrlProtected = (props) => !includes(props.location.pathname, get(this.props.plugins.toJS(), ['users-permissions', 'nonProtectedUrl']));
|
2017-10-10 12:25:40 +02:00
|
|
|
|
2017-11-14 16:42:08 +01:00
|
|
|
showLeftMenu = () => !includes(this.props.location.pathname, get(this.props.plugins.toJS(), ['users-permissions', 'nonProtectedUrl']));
|
2017-11-09 17:36:07 +01:00
|
|
|
|
2017-08-21 15:12:53 +02:00
|
|
|
render() {
|
2017-11-09 17:36:07 +01:00
|
|
|
const leftMenu = this.showLeftMenu() ? <LeftMenu plugins={this.props.plugins} /> : '';
|
|
|
|
const header = this.showLeftMenu() ? <Header /> : '';
|
|
|
|
const style = this.showLeftMenu() ? {} : { width: '100%' };
|
2017-11-28 13:24:26 +01:00
|
|
|
|
2017-08-21 15:12:53 +02:00
|
|
|
return (
|
|
|
|
<div className={styles.adminPage}>
|
2017-11-09 17:36:07 +01:00
|
|
|
{leftMenu}
|
2017-12-05 13:49:31 +01:00
|
|
|
{ auth.getToken() && this.props.hasUserPlugin && this.isUrlProtected(this.props) ? (
|
2017-11-28 13:24:26 +01:00
|
|
|
<Logout />
|
|
|
|
) : ''}
|
2017-11-09 17:36:07 +01:00
|
|
|
<div className={styles.adminPageRightWrapper} style={style}>
|
|
|
|
{header}
|
|
|
|
<Content {...this.props} showLeftMenu={this.showLeftMenu()}>
|
2017-08-21 15:12:53 +02:00
|
|
|
<Switch>
|
2017-08-22 10:51:53 +02:00
|
|
|
<Route path="/" component={HomePage} exact />
|
2017-08-22 17:57:54 +02:00
|
|
|
<Route path="/plugins/:pluginId" component={PluginPage} />
|
|
|
|
<Route path="/plugins" component={ComingSoonPage} />
|
2017-11-02 11:36:38 +01:00
|
|
|
<Route path="/list-plugins" component={ListPluginsPage} exact />
|
2017-12-04 17:32:45 +01:00
|
|
|
<Route path="/install-plugin" component={ComingSoonPage} exact />
|
2017-08-22 10:51:53 +02:00
|
|
|
<Route path="/configuration" component={ComingSoonPage} exact />
|
2017-08-21 15:12:53 +02:00
|
|
|
<Route path="" component={NotFoundPage} />
|
|
|
|
</Switch>
|
|
|
|
</Content>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 12:25:40 +02:00
|
|
|
AdminPage.childContextTypes = {
|
|
|
|
plugins: PropTypes.object,
|
|
|
|
updatePlugin: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2017-08-21 15:12:53 +02:00
|
|
|
AdminPage.contextTypes = {
|
2017-09-20 11:12:04 +02:00
|
|
|
router: PropTypes.object.isRequired,
|
2017-08-21 15:12:53 +02:00
|
|
|
};
|
|
|
|
|
2017-12-05 13:49:31 +01:00
|
|
|
AdminPage.defaultProps = {
|
|
|
|
hasUserPlugin: true,
|
|
|
|
};
|
|
|
|
|
2017-08-21 15:12:53 +02:00
|
|
|
AdminPage.propTypes = {
|
2017-12-05 13:49:31 +01:00
|
|
|
hasUserPlugin: PropTypes.bool,
|
2017-11-14 15:19:48 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
2017-11-09 17:36:07 +01:00
|
|
|
location: PropTypes.object.isRequired,
|
2017-12-01 15:45:11 +01:00
|
|
|
pluginLoaded: PropTypes.func.isRequired,
|
2017-09-20 11:12:04 +02:00
|
|
|
plugins: PropTypes.object.isRequired,
|
2017-10-10 12:25:40 +02:00
|
|
|
updatePlugin: PropTypes.func.isRequired,
|
2017-08-21 15:12:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = createStructuredSelector({
|
2017-12-05 13:49:31 +01:00
|
|
|
hasUserPlugin: selectHasUserPlugin(),
|
2017-08-21 15:12:53 +02:00
|
|
|
plugins: selectPlugins(),
|
|
|
|
});
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return {
|
|
|
|
onHideNotification: (id) => { dispatch(hideNotification(id)); },
|
2017-10-10 12:25:40 +02:00
|
|
|
updatePlugin: (pluginId, updatedKey, updatedValue) => { dispatch(updatePlugin(pluginId, updatedKey, updatedValue)); },
|
2017-12-01 15:45:11 +01:00
|
|
|
pluginLoaded: (plugin) => { dispatch(pluginLoaded(plugin)); },
|
2017-08-21 15:12:53 +02:00
|
|
|
dispatch,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(AdminPage);
|