accounts-frontend/packages/app/containers/AuthFlowRouteContents.tsx

78 lines
1.4 KiB
TypeScript
Raw Normal View History

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