2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2017-05-26 00:41:57 +05:30
|
|
|
import { Redirect } from 'react-router-dom';
|
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import authFlow from 'app/services/authFlow';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2017-06-13 00:40:18 +05:30
|
|
|
type ComponentProps = {
|
2019-12-07 16:58:52 +05:30
|
|
|
component: any;
|
|
|
|
routerProps: { [key: string]: any };
|
2017-06-13 00:40:18 +05:30
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default class AuthFlowRouteContents extends React.Component<
|
2019-11-27 14:33:32 +05:30
|
|
|
ComponentProps,
|
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
component: any;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
> {
|
|
|
|
state: {
|
2019-12-07 16:58:52 +05:30
|
|
|
component: any;
|
2019-11-27 14:33:32 +05:30
|
|
|
} = {
|
|
|
|
component: null,
|
|
|
|
};
|
2017-06-13 00:44:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
_isMounted = false;
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentDidMount() {
|
|
|
|
this._isMounted = true;
|
|
|
|
this.handleProps(this.props);
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentWillReceiveProps(nextProps: ComponentProps) {
|
|
|
|
this.handleProps(nextProps);
|
|
|
|
}
|
2017-06-13 00:44:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
render() {
|
|
|
|
return this.state.component;
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
handleProps(props: ComponentProps) {
|
|
|
|
const { routerProps } = props;
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
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:44:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onRedirect(path: string) {
|
|
|
|
if (!this._isMounted) {
|
|
|
|
return;
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({
|
|
|
|
component: <Redirect to={path} />,
|
|
|
|
});
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onRouteAllowed(props: ComponentProps) {
|
|
|
|
const { component: Component } = props;
|
2017-06-13 00:44:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!this._isMounted) {
|
|
|
|
return;
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
this.setState({
|
|
|
|
component: <Component {...props.routerProps} />,
|
|
|
|
});
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|