Skip to content

Commit

Permalink
Merge pull request #566 from lifeomic/add-require-consent-flag
Browse files Browse the repository at this point in the history
feat: Add config option to require an active consent
  • Loading branch information
bruce-glazier authored Jun 17, 2024
2 parents 72da60e + 32dc615 commit aaa79ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/DeveloperConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
29 changes: 27 additions & 2 deletions src/hooks/useConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -142,6 +165,8 @@ export const useConsent = () => {
isLoading: boolean;
consentDirectives: ConsentAndForm[] | undefined;
shouldRenderConsentScreen: boolean;
hasActiveConsent: boolean;
refetchDirectives: () => void;
};
};
};
Expand Down

0 comments on commit aaa79ac

Please sign in to comment.