/*
* 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';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { Switch, Route } from 'react-router-dom';
import { get, includes, isUndefined } from 'lodash';
import { updatePlugin } from 'containers/App/actions';
import { selectPlugins } from 'containers/App/selectors';
import { hideNotification } from 'containers/NotificationProvider/actions';
// Design
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';
import Content from 'containers/Content';
import NotFoundPage from 'containers/NotFoundPage';
import Header from 'components/Header/index';
import auth from 'utils/auth';
import styles from './styles.scss';
export class AdminPage extends React.Component { // eslint-disable-line react/prefer-stateless-function
getChildContext = () => (
{
plugins: this.props.plugins,
updatePlugin: this.props.updatePlugin,
}
);
componentDidMount() {
this.checkLogin(this.props);
}
componentWillReceiveProps(nextProps) {
if (nextProps.location.pathname !== this.props.location.pathname) {
this.checkLogin(nextProps);
}
}
checkLogin = (props) => {
if (this.hasUsersPlugin() && this.isUrlProtected(props) && !auth.getToken()) {
const endPoint = this.hasAdminUser() ? 'login': 'register';
this.props.history.push(`/plugins/users-permissions/auth/${endPoint}`);
}
}
hasUsersPlugin = () => !isUndefined(get(this.props.plugins.toJS(), 'users-permissions'));
hasAdminUser = () => get(this.props.plugins.toJS(), ['users-permissions', 'hasAdminUser']);
isUrlProtected = (props) => !includes(props.location.pathname, get(this.props.plugins.toJS(), ['users-permissions', 'nonProtectedUrl']));
showLeftMenu = () => !includes(this.props.location.pathname, get(this.props.plugins.toJS(), ['users-permissions', 'nonProtectedUrl']));
render() {
const leftMenu = this.showLeftMenu() ?