2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2019-12-10 13:17:32 +05:30
|
|
|
import { Redirect, RouteComponentProps } from 'react-router-dom';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import authFlow from 'app/services/authFlow';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
interface Props {
|
|
|
|
component:
|
|
|
|
| React.ComponentType<RouteComponentProps<any>>
|
|
|
|
| React.ComponentType<any>;
|
|
|
|
routerProps: RouteComponentProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
access: null | 'rejected' | 'allowed';
|
|
|
|
component: React.ReactElement | null;
|
|
|
|
}
|
2017-06-13 00:40:18 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default class AuthFlowRouteContents extends React.Component<
|
2019-12-10 13:17:32 +05:30
|
|
|
Props,
|
|
|
|
State
|
2019-11-27 14:33:32 +05:30
|
|
|
> {
|
2019-12-10 13:17:32 +05:30
|
|
|
state: State = {
|
|
|
|
access: null,
|
2019-11-27 14:33:32 +05:30
|
|
|
component: null,
|
|
|
|
};
|
2017-06-13 00:44:02 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
mounted = false;
|
|
|
|
|
|
|
|
shouldComponentUpdate(
|
|
|
|
{ routerProps: nextRoute, component: nextComponent }: Props,
|
|
|
|
state: State,
|
|
|
|
) {
|
|
|
|
const { component: prevComponent, routerProps: prevRoute } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
prevRoute.location.pathname !== nextRoute.location.pathname ||
|
|
|
|
prevRoute.location.search !== nextRoute.location.search ||
|
|
|
|
prevComponent !== nextComponent ||
|
|
|
|
this.state.access !== state.access
|
|
|
|
);
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentDidMount() {
|
2019-12-10 13:17:32 +05:30
|
|
|
this.mounted = true;
|
2019-11-27 14:33:32 +05:30
|
|
|
this.handleProps(this.props);
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
componentDidUpdate() {
|
|
|
|
this.handleProps(this.props);
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2017-06-13 00:44:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentWillUnmount() {
|
2019-12-10 13:17:32 +05:30
|
|
|
this.mounted = false;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
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-12-10 13:17:32 +05:30
|
|
|
handleProps(props: Props) {
|
2019-11-27 14:33:32 +05:30
|
|
|
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,
|
2019-12-10 13:17:32 +05:30
|
|
|
query: new URLSearchParams(routerProps.location.search),
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
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) {
|
2019-12-10 13:17:32 +05:30
|
|
|
if (!this.mounted) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return;
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({
|
2019-12-10 13:17:32 +05:30
|
|
|
access: 'rejected',
|
2019-11-27 14:33:32 +05:30
|
|
|
component: <Redirect to={path} />,
|
|
|
|
});
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
onRouteAllowed(props: Props) {
|
|
|
|
if (!this.mounted) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return;
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
const { component: Component } = props;
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({
|
2019-12-10 13:17:32 +05:30
|
|
|
access: 'allowed',
|
2019-11-27 14:33:32 +05:30
|
|
|
component: <Component {...props.routerProps} />,
|
|
|
|
});
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|