fleet/frontend/components/AuthenticatedRoutes/AuthenticatedRoutes.jsx
Gabe Hernandez efb35b537a
add prettier and have it format all fleet application code (#625)
* add prettier and have it format all js code except website:
:

* trying running prettier check in CI

* fix runs on in CI

* change CI job name

* fix prettier erros and fix CI
2021-04-12 14:32:25 +01:00

87 lines
2.1 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";
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}</div>;
}
}
const mapStateToProps = (state) => {
const { loading, user } = state.auth;
const { locationBeforeTransitions } = state.routing;
return { loading, locationBeforeTransitions, user };
};
export default connect(mapStateToProps)(AuthenticatedRoutes);