296 lines
9.7 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';
2018-09-25 17:11:14 +02:00
import { bindActionCreators, compose } from 'redux';
// Actions required for disabling and enabling the OverlayBlocker
2018-05-18 14:22:24 +02:00
import { disableGlobalOverlayBlocker, enableGlobalOverlayBlocker } from 'actions/overlayBlocker';
import { pluginLoaded, updatePlugin } from 'containers/App/actions';
import {
2018-09-26 14:43:10 +02:00
makeSelectAppPlugins,
makeSelectBlockApp,
2018-09-25 17:11:14 +02:00
makeSelectIsAppLoading,
makeSelectShowGlobalAppBlocker,
selectHasUserPlugin,
selectPlugins,
} from 'containers/App/selectors';
2017-11-14 15:19:48 +01:00
// 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';
2018-05-14 18:22:37 +02:00
import LocaleToggle from 'containers/LocaleToggle';
2018-05-16 09:18:30 +02:00
import CTAWrapper from 'components/CtaWrapper';
2017-11-28 13:24:26 +01:00
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';
2018-09-25 17:11:14 +02:00
import LoadingIndicatorPage from 'components/LoadingIndicatorPage';
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-09-07 13:09:35 +02:00
import PluginPage from 'containers/PluginPage';
// 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-09-25 17:11:14 +02:00
import { getAdminData } from './actions';
import reducer from './reducer';
import saga from './saga';
import selectAdminPage from './selectors';
import styles from './styles.scss';
2017-08-21 15:12:53 +02:00
2018-04-26 11:24:47 +02:00
const PLUGINS_TO_BLOCK_PRODUCTION = ['content-type-builder', 'settings-manager'];
2018-05-18 14:22:24 +02:00
export class AdminPage extends React.Component {
// eslint-disable-line react/prefer-stateless-function
state = { hasAlreadyRegistereOtherPlugins: false };
2018-05-18 14:22:24 +02:00
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() {
2018-09-25 17:11:14 +02:00
this.props.getAdminData();
2017-11-20 14:45:27 +01:00
this.checkLogin(this.props);
2018-03-19 17:45:33 +01:00
ReactGA.initialize('UA-54313258-9');
2017-11-14 15:19:48 +01:00
}
2018-09-25 17:11:14 +02:00
componentDidUpdate(prevProps) {
const { adminPage: { allowGa }, location: { pathname }, plugins } = this.props;
if (prevProps.location.pathname !== pathname) {
this.checkLogin(this.props);
2018-03-19 17:45:33 +01:00
2018-09-25 17:11:14 +02:00
if (allowGa) {
ReactGA.pageview(pathname);
2018-03-23 12:44:17 +01:00
}
2017-12-05 15:16:33 +01:00
}
2017-12-04 17:32:45 +01:00
2018-09-25 17:11:14 +02:00
const hasAdminPath = ['users-permissions', 'hasAdminUser'];
if (get(prevProps.plugins.toJS(), hasAdminPath) !== get(plugins.toJS(), hasAdminPath)) {
this.checkLogin(this.props, true);
2017-11-20 14:45:27 +01:00
}
2018-09-25 17:11:14 +02:00
if (!this.hasUserPluginLoaded(prevProps) && this.hasUserPluginLoaded(this.props)) {
this.checkLogin(this.props);
}
2017-11-20 14:45:27 +01:00
}
2018-05-17 19:06:52 +02:00
checkLogin = (props, skipAction = false) => {
2017-12-06 11:08:49 +01:00
if (props.hasUserPlugin && this.isUrlProtected(props) && !auth.getToken()) {
if (!this.hasUserPluginLoaded(props)) {
return;
}
2018-05-18 14:22:24 +02: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
2018-05-18 14:22:24 +02:00
if (
!this.isUrlProtected(props) &&
includes(props.location.pathname, 'auth/register') &&
this.hasAdminUser(props) &&
!skipAction
) {
this.props.history.push('/plugins/users-permissions/auth/login');
2017-11-28 15:04:02 +01:00
}
2018-05-18 14:22:24 +02: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');
}
2018-05-18 14:22:24 +02: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):
2018-05-18 14:22:24 +02:00
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 });
}
2018-05-18 14:22:24 +02:00
};
2017-11-14 15:19:48 +01:00
2018-09-26 14:43:10 +02:00
hasUserPluginInstalled = () => {
const { appPlugins } = this.props;
return appPlugins.indexOf('users-permissions') !== -1;
}
2018-05-18 14:22:24 +02:00
hasUserPluginLoaded = props =>
typeof get(props.plugins.toJS(), ['users-permissions', 'hasAdminUser']) !== 'undefined';
2018-05-18 14:22:24 +02:00
hasAdminUser = props => get(props.plugins.toJS(), ['users-permissions', 'hasAdminUser']);
2018-05-18 14:22:24 +02:00
isUrlProtected = props =>
!includes(props.location.pathname, get(props.plugins.toJS(), ['users-permissions', 'nonProtectedUrl']));
2018-05-14 18:22:37 +02:00
shouldDisplayLogout = () => auth.getToken() && this.props.hasUserPlugin && this.isUrlProtected(this.props);
2017-12-05 15:16:33 +01:00
showLeftMenu = () => !includes(this.props.location.pathname, 'users-permissions/auth/');
2018-09-25 17:11:14 +02:00
showLoading = () => {
const { isAppLoading, adminPage: { isLoading } } = this.props;
2018-09-26 14:43:10 +02:00
return isAppLoading || isLoading || (this.hasUserPluginInstalled() && !this.hasUserPluginLoaded(this.props));
2018-09-25 17:11:14 +02:00
}
2018-04-26 11:24:47 +02:00
retrievePlugins = () => {
2018-05-18 14:22:24 +02:00
const {
adminPage: { currentEnvironment },
plugins,
} = this.props;
2018-04-26 11:24:47 +02:00
if (currentEnvironment === 'production') {
let pluginsToDisplay = plugins;
2018-05-18 14:22:24 +02:00
PLUGINS_TO_BLOCK_PRODUCTION.map(plugin => (pluginsToDisplay = pluginsToDisplay.delete(plugin)));
2018-04-26 11:24:47 +02:00
return pluginsToDisplay;
}
return plugins;
2018-05-18 14:22:24 +02:00
};
2018-04-26 11:24:47 +02:00
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%' };
2018-09-25 17:11:14 +02:00
if (this.showLoading()) {
return <LoadingIndicatorPage />;
2018-06-14 11:35:40 +02:00
}
2017-08-21 15:12:53 +02:00
return (
<div className={styles.adminPage}>
2018-04-12 23:45:17 +02:00
{this.showLeftMenu() && (
<LeftMenu
2018-04-26 11:24:47 +02:00
plugins={this.retrievePlugins()}
2018-04-12 23:45:17 +02:00
layout={adminPage.layout}
version={adminPage.strapiVersion}
/>
)}
2018-05-15 14:47:19 +02:00
<CTAWrapper>
2018-05-14 18:22:37 +02:00
{this.shouldDisplayLogout() && <Logout />}
2018-05-17 13:37:39 +02:00
<LocaleToggle isLogged={this.shouldDisplayLogout() === true} />
2018-05-15 14:47:19 +02:00
</CTAWrapper>
<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: {},
2018-09-26 15:07:53 +02:00
appPlugins: [],
hasUserPlugin: true,
2018-09-26 15:07:53 +02:00
isAppLoading: true,
};
2017-08-21 15:12:53 +02:00
AdminPage.propTypes = {
adminPage: PropTypes.object,
2018-09-26 15:07:53 +02:00
appPlugins: PropTypes.array,
blockApp: PropTypes.bool.isRequired,
disableGlobalOverlayBlocker: PropTypes.func.isRequired,
enableGlobalOverlayBlocker: PropTypes.func.isRequired,
hasUserPlugin: PropTypes.bool,
2017-11-14 15:19:48 +01:00
history: PropTypes.object.isRequired,
2018-09-26 15:07:53 +02:00
isAppLoading: PropTypes.bool,
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(),
2018-09-26 14:43:10 +02:00
appPlugins: makeSelectAppPlugins(),
blockApp: makeSelectBlockApp(),
hasUserPlugin: selectHasUserPlugin(),
2018-09-25 17:11:14 +02:00
isAppLoading: makeSelectIsAppLoading(),
2017-08-21 15:12:53 +02:00
plugins: selectPlugins(),
showGlobalAppBlocker: makeSelectShowGlobalAppBlocker(),
2017-08-21 15:12:53 +02:00
});
function mapDispatchToProps(dispatch) {
2018-09-25 17:11:14 +02:00
return bindActionCreators(
{
disableGlobalOverlayBlocker,
enableGlobalOverlayBlocker,
getAdminData,
pluginLoaded,
updatePlugin,
2018-05-18 14:22:24 +02:00
},
2017-08-21 15:12:53 +02:00
dispatch,
2018-09-25 17:11:14 +02:00
);
2017-08-21 15:12:53 +02:00
}
const withConnect = connect(mapStateToProps, mapDispatchToProps);
const withReducer = injectReducer({ key: 'adminPage', reducer });
const withSaga = injectSaga({ key: 'adminPage', saga });
2018-05-18 14:22:24 +02:00
export default compose(withReducer, withSaga, withConnect)(AdminPage);