diff --git a/src/common/DeveloperConfig.ts b/src/common/DeveloperConfig.ts index a39b6e90..eb550e9a 100644 --- a/src/common/DeveloperConfig.ts +++ b/src/common/DeveloperConfig.ts @@ -145,6 +145,11 @@ export type DeveloperConfig = { * is present for a given account and we do not expect the usual invite to project flow. ***/ keepWaitingForPatientId?: boolean; + /*** + * Instead of proceeding further into the LoggedInStack show a loading indicator if + * there the user has no active consents and keep checking for either new consent assignment or active consent status. + */ + activeConsentRequired?: boolean; }; export type LogoHeaderConfig = { [key in Route]?: LogoHeaderOptions }; diff --git a/src/hooks/useConsent.ts b/src/hooks/useConsent.ts index e3aba9d1..1cd968b0 100644 --- a/src/hooks/useConsent.ts +++ b/src/hooks/useConsent.ts @@ -15,6 +15,8 @@ import { } from '@tanstack/react-query'; import { ACTIVITIES_QUERY_KEY } from './useActivities'; import cloneDeep from 'lodash/cloneDeep'; +import { useDeveloperConfig } from './useDeveloperConfig'; +import { useEffect } from 'react'; type PatchConsentDirectives = RestAPIEndpoints['PATCH /v1/consent/directives/me/:directiveId']; @@ -95,15 +97,36 @@ export const useConsent = () => { data: directivesData, isLoading: loadingDirectives, isFetched: fetchedDirectives, + refetch: refetchDirectives, + isRefetching: refetchingDirectives, } = useConsentDirectives(); - + const { activeConsentRequired } = useDeveloperConfig(); + const activeConsents = directivesData?.items?.filter( + (c) => c.status === 'active', + ); + const hasActiveConsent = !!activeConsents?.length; const consentDirectives = directivesData?.items?.filter( (c) => c.status === 'proposed' || c.status === 'rejected', ); const shouldRenderConsentScreen = !!consentDirectives?.length; + const consentCheckNotSatisfied = + activeConsentRequired && !hasActiveConsent && !shouldRenderConsentScreen; + + useEffect(() => { + const intervalId = setInterval(() => { + if (consentCheckNotSatisfied && !refetchingDirectives) { + refetchDirectives(); + } else { + clearInterval(intervalId); + } + }, 2000); + + return () => clearInterval(intervalId); + }, [consentCheckNotSatisfied, refetchDirectives, refetchingDirectives]); return { - isLoading: !fetchedDirectives || loadingDirectives, + isLoading: + !fetchedDirectives || loadingDirectives || consentCheckNotSatisfied, consentDirectives, shouldRenderConsentScreen, }; @@ -142,6 +165,8 @@ export const useConsent = () => { isLoading: boolean; consentDirectives: ConsentAndForm[] | undefined; shouldRenderConsentScreen: boolean; + hasActiveConsent: boolean; + refetchDirectives: () => void; }; }; };