2024-12-14 17:46:29 +05:30
|
|
|
import React, { FC, ReactElement, ComponentType, useEffect, useState } from 'react';
|
|
|
|
import { RouteComponentProps } from 'react-router-dom';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2024-12-14 17:46:29 +05:30
|
|
|
import { useIsMounted } from 'app/hooks';
|
2019-12-08 00:32:00 +05:30
|
|
|
import authFlow from 'app/services/authFlow';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2024-12-14 17:46:29 +05:30
|
|
|
interface Props extends RouteComponentProps {
|
|
|
|
component: ComponentType<RouteComponentProps>;
|
2019-12-10 13:17:32 +05:30
|
|
|
}
|
|
|
|
|
2024-12-14 17:46:29 +05:30
|
|
|
const AuthFlowRouteContents: FC<Props> = ({ component: WantedComponent, location, match, history }) => {
|
|
|
|
const isMounted = useIsMounted();
|
|
|
|
const [component, setComponent] = useState<ReactElement | null>(null);
|
2020-05-24 04:38:24 +05:30
|
|
|
|
2024-12-14 17:46:29 +05:30
|
|
|
useEffect(() => {
|
2024-12-18 03:41:39 +05:30
|
|
|
// Promise that will be resolved after handleRequest might contain already non-actual component to render,
|
|
|
|
// so set it to false in the effect's clear function to prevent unwanted UI state
|
|
|
|
let isActual = true;
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
authFlow.handleRequest(
|
|
|
|
{
|
2024-12-14 17:46:29 +05:30
|
|
|
path: location.pathname,
|
|
|
|
params: match.params,
|
|
|
|
query: new URLSearchParams(location.search),
|
|
|
|
},
|
|
|
|
history.push,
|
|
|
|
() => {
|
2024-12-18 03:41:39 +05:30
|
|
|
if (isActual && isMounted()) {
|
2024-12-14 17:46:29 +05:30
|
|
|
setComponent(<WantedComponent history={history} location={location} match={match} />);
|
|
|
|
}
|
2020-05-24 04:38:24 +05:30
|
|
|
},
|
|
|
|
);
|
2024-12-18 03:41:39 +05:30
|
|
|
|
|
|
|
return () => {
|
|
|
|
isActual = false;
|
|
|
|
};
|
2024-12-14 17:46:29 +05:30
|
|
|
}, [location.pathname, location.search]);
|
2020-05-24 04:38:24 +05:30
|
|
|
|
2024-12-14 17:46:29 +05:30
|
|
|
return component;
|
|
|
|
};
|
2020-05-24 04:38:24 +05:30
|
|
|
|
2024-12-14 17:46:29 +05:30
|
|
|
export default AuthFlowRouteContents;
|