Skip to content

feat: Add config option to require an active consent #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading