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 {
|
2020-05-24 04:38:24 +05:30
|
|
|
component: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
|
|
|
|
routerProps: RouteComponentProps;
|
2019-12-10 13:17:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2020-05-24 04:38:24 +05:30
|
|
|
access: null | 'rejected' | 'allowed';
|
|
|
|
component: React.ReactElement | null;
|
2019-12-10 13:17:32 +05:30
|
|
|
}
|
2017-06-13 00:40:18 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export default class AuthFlowRouteContents extends React.Component<Props, State> {
|
|
|
|
state: State = {
|
|
|
|
access: null,
|
|
|
|
component: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.mounted = true;
|
|
|
|
this.handleProps(this.props);
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
componentDidUpdate() {
|
|
|
|
this.handleProps(this.props);
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
this.mounted = false;
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
render() {
|
|
|
|
return this.state.component;
|
|
|
|
}
|
2019-12-10 13:17:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
handleProps(props: Props) {
|
|
|
|
const { routerProps } = props;
|
|
|
|
|
|
|
|
authFlow.handleRequest(
|
|
|
|
{
|
|
|
|
path: routerProps.location.pathname,
|
|
|
|
params: routerProps.match.params,
|
|
|
|
query: new URLSearchParams(routerProps.location.search),
|
|
|
|
},
|
|
|
|
this.onRedirect.bind(this),
|
|
|
|
this.onRouteAllowed.bind(this, props),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onRedirect(path: string) {
|
|
|
|
if (!this.mounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
access: 'rejected',
|
|
|
|
component: <Redirect to={path} />,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onRouteAllowed(props: Props) {
|
|
|
|
if (!this.mounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { component: Component } = props;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
access: 'allowed',
|
|
|
|
component: <Component {...props.routerProps} />,
|
|
|
|
});
|
|
|
|
}
|
2017-05-26 00:41:57 +05:30
|
|
|
}
|