fleet/frontend/components/AuthenticatedRoutes/AuthenticatedRoutes.jsx
Zachary Wasserman dc4b97d15f
Fix React deprecation warnings (#1976)
- Refactor imports of PropTypes to use the prop-types package
- Upgrade dependencies that were setting off deprecation warnings
2019-01-06 17:25:33 -08:00

93 lines
2.2 KiB
JavaScript

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { isEqual } from 'lodash';
import { push } from 'react-router-redux';
import paths from 'router/paths';
import redirectLocationInterface from 'interfaces/redirect_location';
import { setRedirectLocation } from 'redux/nodes/redirectLocation/actions';
import userInterface from 'interfaces/user';
import Footer from 'components/Footer';
export class AuthenticatedRoutes extends Component {
static propTypes = {
children: PropTypes.element,
dispatch: PropTypes.func,
loading: PropTypes.bool.isRequired,
locationBeforeTransitions: redirectLocationInterface,
user: userInterface,
};
componentWillMount () {
const { loading, user } = this.props;
const { redirectToLogin, redirectToPasswordReset } = this;
if (!loading && !user) {
return redirectToLogin();
}
if (user && user.force_password_reset) {
return redirectToPasswordReset();
}
return false;
}
componentWillReceiveProps (nextProps) {
if (isEqual(this.props, nextProps)) return false;
const { loading, user } = nextProps;
const { redirectToLogin, redirectToPasswordReset } = this;
if (!loading && !user) {
return redirectToLogin();
}
if (user && user.force_password_reset) {
return redirectToPasswordReset();
}
return false;
}
redirectToLogin = () => {
const { dispatch, locationBeforeTransitions } = this.props;
const { LOGIN } = paths;
dispatch(setRedirectLocation(locationBeforeTransitions));
return dispatch(push(LOGIN));
}
redirectToPasswordReset = () => {
const { dispatch } = this.props;
const { RESET_PASSWORD } = paths;
return dispatch(push(RESET_PASSWORD));
}
render () {
const { children, user } = this.props;
if (!user) {
return false;
}
return (
<div>
{children}
<Footer />
</div>
);
}
}
const mapStateToProps = (state) => {
const { loading, user } = state.auth;
const { locationBeforeTransitions } = state.routing;
return { loading, locationBeforeTransitions, user };
};
export default connect(mapStateToProps)(AuthenticatedRoutes);