2017-06-13 00:40:18 +05:30
|
|
|
// @flow
|
|
|
|
import React, { Component } from 'react';
|
2017-05-26 00:41:57 +05:30
|
|
|
import { Redirect } from 'react-router-dom';
|
|
|
|
|
|
|
|
import authFlow from 'services/authFlow';
|
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
type ComponentProps = {
|
|
|
|
component: any,
|
|
|
|
routerProps: Object
|
|
|
|
};
|
|
|
|
|
2017-05-26 00:41:57 +05:30
|
|
|
export default class AuthFlowRouteContents extends Component {
|
2017-06-13 00:40:18 +05:30
|
|
|
props: ComponentProps;
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
state: {
|
|
|
|
component: any
|
|
|
|
} = {
|
2017-05-26 00:41:57 +05:30
|
|
|
component: null
|
|
|
|
};
|
|
|
|
|
2017-06-13 00:44:02 +05:30
|
|
|
_isMounted = false;
|
|
|
|
|
2017-05-26 00:41:57 +05:30
|
|
|
componentDidMount() {
|
|
|
|
this.handleProps(this.props);
|
2017-06-13 00:44:02 +05:30
|
|
|
this._isMounted = true;
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
componentWillReceiveProps(nextProps: ComponentProps) {
|
2017-05-26 00:41:57 +05:30
|
|
|
this.handleProps(nextProps);
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:44:02 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:41:57 +05:30
|
|
|
render() {
|
|
|
|
return this.state.component;
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
handleProps(props: ComponentProps) {
|
2017-05-26 00:41:57 +05:30
|
|
|
const {routerProps} = props;
|
|
|
|
|
|
|
|
authFlow.handleRequest({
|
|
|
|
path: routerProps.location.pathname,
|
|
|
|
params: routerProps.match.params,
|
|
|
|
query: routerProps.location.query
|
|
|
|
}, this.onRedirect.bind(this), this.onRouteAllowed.bind(this, props));
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
onRedirect(path: string) {
|
2017-06-13 00:44:02 +05:30
|
|
|
if (!this._isMounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:41:57 +05:30
|
|
|
this.setState({
|
|
|
|
component: <Redirect to={path} />
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
onRouteAllowed(props: ComponentProps) {
|
2017-05-26 00:41:57 +05:30
|
|
|
const {component: Component} = props;
|
|
|
|
|
2017-06-13 00:44:02 +05:30
|
|
|
if (!this._isMounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 00:41:57 +05:30
|
|
|
this.setState({
|
|
|
|
component: <Component {...props.routerProps} />
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|