Skip to content

Commit

Permalink
Merge 5e1f9d4 into 0294d22
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-glazier committed Feb 7, 2024
2 parents 0294d22 + 5e1f9d4 commit 275c4f4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
25 changes: 24 additions & 1 deletion src/hooks/useConsent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useActiveProject } from './useActiveProject';
import { Consent, Questionnaire } from 'fhir/r3';
import { useRestQuery, useRestMutation } from './rest-api';
import { useRestQuery, useRestMutation, useRestCache } from './rest-api';
import { RestAPIEndpoints } from '../types/rest-types';
import { RequestPayloadOf } from '@lifeomic/one-query';
import { useQueryClient } from '@tanstack/react-query';
import { ACTIVITIES_QUERY_KEY } from './useActivities';
import cloneDeep from 'lodash/cloneDeep';

type PatchConsentDirectives =
RestAPIEndpoints['PATCH /v1/consent/directives/me/:directiveId'];
Expand All @@ -29,6 +30,7 @@ export const createConsentPatch = (
export const useConsent = () => {
const queryClient = useQueryClient();
const { activeProject } = useActiveProject();
const cache = useRestCache();

const useConsentDirectives = () => {
return useRestQuery('GET /v1/consent/directives/me', {
Expand All @@ -54,6 +56,27 @@ export const useConsent = () => {
*/
queryClient.resetQueries({ queryKey: ACTIVITIES_QUERY_KEY });
await options?.onSuccess?.(data, variables);

/**
* Update consent status in cache following a successful mutation
*/
cache.updateCache(
'GET /v1/consent/directives/me',
{ projectId: activeProject.id, includeForm: true },
(currentData) => {
if (!currentData) {
return currentData!;
}
const newData = cloneDeep(currentData);
for (const consent of newData?.items) {
if (consent.id === variables.directiveId) {
consent.status = variables.status;
}
}

return newData!;
},
);
},
});
};
Expand Down
30 changes: 22 additions & 8 deletions src/screens/ConsentScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import { View, ScrollView, Text, Alert } from 'react-native';
import { t } from 'i18next';
import Markdown from 'react-native-markdown-display';
Expand All @@ -23,14 +23,26 @@ export const ConsentScreen = ({
const { CustomConsentScreen } = useDeveloperConfig();
const { useShouldRenderConsentScreen, useUpdateProjectConsentDirective } =
useConsent();
const { consentDirectives, isLoading: loadingDirectives } =
useShouldRenderConsentScreen();
const {
shouldRenderConsentScreen,
consentDirectives,
isLoading: loadingDirectives,
} = useShouldRenderConsentScreen();
const updateConsentDirectiveMutation = useUpdateProjectConsentDirective({
onSuccess: (_, { status }) => {
if (status === 'rejected') {
return logout({});
}
},
});

const { logout } = useOAuthFlow();
const { shouldLaunchOnboardingCourse } = useOnboardingCourse();

// Handle navigation when there
// are no consents left to present
useEffect(() => {
if (!shouldRenderConsentScreen) {
if (route.params?.noNavOnAccept) {
navigation.pop();
return;
Expand All @@ -39,12 +51,14 @@ export const ConsentScreen = ({
? 'screens/OnboardingCourseScreen'
: 'app';
navigation.replace(nextRoute);
},
});
const { logout } = useOAuthFlow();
const { shouldLaunchOnboardingCourse } = useOnboardingCourse();
}
}, [
navigation,
route.params?.noNavOnAccept,
shouldLaunchOnboardingCourse,
shouldRenderConsentScreen,
]);

// TODO: If needed, allow for accepting multiple consents in a row.
const consentToPresent = useMemo(
() => consentDirectives?.[0],
[consentDirectives],
Expand Down

0 comments on commit 275c4f4

Please sign in to comment.