241 lines
8.5 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';
2018-03-19 17:45:33 +01:00
import ReactGA from 'react-ga';
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';
2018-03-08 10:32:01 +01:00
import { get, includes, isFunction, map, omit } from 'lodash';
import { compose } from 'redux';
2017-08-21 15:12:53 +02:00
// Actions required for disabling and enabling the OverlayBlocker
import {
disableGlobalOverlayBlocker,
enableGlobalOverlayBlocker,
} from 'actions/overlayBlocker';
import { pluginLoaded, updatePlugin } from 'containers/App/actions';
import {
makeSelectBlockApp,
makeSelectShowGlobalAppBlocker,
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';
2018-04-03 17:56:19 +02:00
import HomePage from 'containers/HomePage/Loadable';
import InstallPluginPage from 'containers/InstallPluginPage/Loadable';
2017-08-21 15:12:53 +02:00
import LeftMenu from 'containers/LeftMenu';
2018-04-03 17:56:19 +02:00
import ListPluginsPage from 'containers/ListPluginsPage/Loadable';
2017-11-28 13:24:26 +01:00
import Logout from 'components/Logout';
2018-04-03 17:56:19 +02:00
import NotFoundPage from 'containers/NotFoundPage/Loadable';
import OverlayBlocker from 'components/OverlayBlocker';
2018-04-03 17:56:19 +02:00
import PluginPage from 'containers/PluginPage/Loadable';
2017-08-21 15:12:53 +02:00
// Utils
2017-11-14 15:19:48 +01:00
import auth from 'utils/auth';
import injectReducer from 'utils/injectReducer';
import injectSaga from 'utils/injectSaga';
2018-04-05 11:13:49 +02:00
import { getGaStatus, getLayout } from './actions';
import reducer from './reducer';
import saga from './saga';
import selectAdminPage from './selectors';
2017-11-14 15:19:48 +01: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
state = { hasAlreadyRegistereOtherPlugins: false };
getChildContext = () => (
{
disableGlobalOverlayBlocker: this.props.disableGlobalOverlayBlocker,
enableGlobalOverlayBlocker: this.props.enableGlobalOverlayBlocker,
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);
2018-03-23 12:44:17 +01:00
this.props.getGaStatus();
2018-04-05 11:13:49 +02:00
this.props.getLayout();
2018-03-19 17:45:33 +01:00
ReactGA.initialize('UA-54313258-9');
2017-11-14 15:19:48 +01:00
}
componentWillReceiveProps(nextProps) {
if (nextProps.location.pathname !== this.props.location.pathname) {
2017-12-05 15:16:33 +01:00
this.checkLogin(nextProps);
2018-03-19 17:45:33 +01:00
2018-03-23 12:44:17 +01:00
if (nextProps.adminPage.allowGa) {
ReactGA.pageview(nextProps.location.pathname);
}
2017-12-05 15:16:33 +01:00
}
2017-12-04 17:32:45 +01:00
2017-12-05 15:16:33 +01:00
if (get(nextProps.plugins.toJS(), ['users-permissions', 'hasAdminUser']) !== get(this.props.plugins.toJS(), ['users-permissions', 'hasAdminUser'])) {
2017-11-20 14:45:27 +01:00
this.checkLogin(nextProps);
}
}
checkLogin = (props) => {
2017-12-06 11:08:49 +01:00
if (props.hasUserPlugin && this.isUrlProtected(props) && !auth.getToken()) {
2017-12-05 15:16:33 +01:00
const endPoint = this.hasAdminUser(props) ? 'login': 'register';
2017-11-20 14:45:27 +01:00
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
if (!this.isUrlProtected(props) && includes(props.location.pathname, 'auth/register') && this.hasAdminUser(props)) {
this.props.history.push('/plugins/users-permissions/auth/login');
2017-11-28 15:04:02 +01:00
}
2017-12-12 15:48:09 +01:00
if (props.hasUserPlugin && !this.isUrlProtected(props) && !includes(props.location.pathname, 'auth/register') && !this.hasAdminUser(props)) {
this.props.history.push('/plugins/users-permissions/auth/register');
}
2017-12-06 11:08:49 +01:00
if (!props.hasUserPlugin || auth.getToken() && !this.state.hasAlreadyRegistereOtherPlugins) {
map(omit(this.props.plugins.toJS(), ['users-permissions', 'email']), plugin => {
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:
}
});
this.setState({ hasAlreadyRegistereOtherPlugins: true });
}
2017-11-14 15:19:48 +01:00
}
2017-12-05 15:16:33 +01:00
hasAdminUser = (props) => get(props.plugins.toJS(), ['users-permissions', 'hasAdminUser']);
2017-12-06 11:08:49 +01:00
isUrlProtected = (props) => !includes(props.location.pathname, get(props.plugins.toJS(), ['users-permissions', 'nonProtectedUrl']));
2017-12-05 15:16:33 +01:00
showLeftMenu = () => !includes(this.props.location.pathname, 'users-permissions/auth/');
2017-08-21 15:12:53 +02:00
render() {
2018-04-12 23:45:17 +02:00
const { adminPage } = this.props;
const header = this.showLeftMenu() ? <Header /> : '';
const style = this.showLeftMenu() ? {} : { width: '100%' };
2017-08-21 15:12:53 +02:00
return (
<div className={styles.adminPage}>
2018-04-12 23:45:17 +02:00
{this.showLeftMenu() && (
<LeftMenu
plugins={this.props.plugins}
layout={adminPage.layout}
version={adminPage.strapiVersion}
/>
)}
{ auth.getToken() && this.props.hasUserPlugin && this.isUrlProtected(this.props) ? (
2017-11-28 13:24:26 +01:00
<Logout />
) : ''}
<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 />
2017-12-11 19:04:27 +01:00
<Route path="/install-plugin" component={InstallPluginPage} exact />
<Route path="/configuration" component={ComingSoonPage} exact />
2017-08-21 15:12:53 +02:00
<Route path="" component={NotFoundPage} />
2017-12-06 16:40:32 +01:00
<Route path="404" component={NotFoundPage} />
2017-08-21 15:12:53 +02:00
</Switch>
</Content>
</div>
<OverlayBlocker isOpen={this.props.blockApp && this.props.showGlobalAppBlocker} />
2017-08-21 15:12:53 +02:00
</div>
);
}
}
AdminPage.childContextTypes = {
disableGlobalOverlayBlocker: PropTypes.func,
enableGlobalOverlayBlocker: PropTypes.func,
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.defaultProps = {
adminPage: {},
hasUserPlugin: true,
};
2017-08-21 15:12:53 +02:00
AdminPage.propTypes = {
adminPage: PropTypes.object,
blockApp: PropTypes.bool.isRequired,
disableGlobalOverlayBlocker: PropTypes.func.isRequired,
enableGlobalOverlayBlocker: PropTypes.func.isRequired,
2018-03-23 12:44:17 +01:00
getGaStatus: PropTypes.func.isRequired,
2018-04-05 11:13:49 +02:00
getLayout: PropTypes.func.isRequired,
hasUserPlugin: PropTypes.bool,
2017-11-14 15:19:48 +01:00
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
pluginLoaded: PropTypes.func.isRequired,
plugins: PropTypes.object.isRequired,
showGlobalAppBlocker: PropTypes.bool.isRequired,
updatePlugin: PropTypes.func.isRequired,
2017-08-21 15:12:53 +02:00
};
const mapStateToProps = createStructuredSelector({
adminPage: selectAdminPage(),
blockApp: makeSelectBlockApp(),
hasUserPlugin: selectHasUserPlugin(),
2017-08-21 15:12:53 +02:00
plugins: selectPlugins(),
showGlobalAppBlocker: makeSelectShowGlobalAppBlocker(),
2017-08-21 15:12:53 +02:00
});
function mapDispatchToProps(dispatch) {
return {
disableGlobalOverlayBlocker: () => { dispatch(disableGlobalOverlayBlocker()); },
enableGlobalOverlayBlocker: () => { dispatch(enableGlobalOverlayBlocker()); },
2018-03-23 12:44:17 +01:00
getGaStatus: () => { dispatch(getGaStatus()); },
2018-04-05 11:13:49 +02:00
getLayout: () => { dispatch(getLayout()); },
2017-08-21 15:12:53 +02:00
onHideNotification: (id) => { dispatch(hideNotification(id)); },
pluginLoaded: (plugin) => { dispatch(pluginLoaded(plugin)); },
updatePlugin: (pluginId, updatedKey, updatedValue) => { dispatch(updatePlugin(pluginId, updatedKey, updatedValue)); },
2017-08-21 15:12:53 +02:00
dispatch,
};
}
const withConnect = connect(mapStateToProps, mapDispatchToProps);
const withReducer = injectReducer({ key: 'adminPage', reducer });
const withSaga = injectSaga({ key: 'adminPage', saga });
export default compose(
withReducer,
withSaga,
withConnect,
)(AdminPage);
// export default connect(mapStateToProps, mapDispatchToProps)(AdminPage);