Skip to content

Commit

Permalink
refactor: change use consent hook to be powered by api hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jkdowdle committed Dec 1, 2023
1 parent 8fd889f commit 31fdf61
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/hooks/todayTile/useTodayTasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ const useConsentTasks = () => {
const { activeProject } = useActiveProject();

return useRestQuery(
'GET /v1/consent/directives/me',
'GET /v1/consent/directives/me?includeForm=true',
{
projectId: activeProject?.id!,
includeForm: true,
},
{
select: (data) => data.items,
Expand Down
20 changes: 15 additions & 5 deletions src/hooks/useConsent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('useConsentDirectives', () => {
};

const responseMock = { items: [directive] };
axiosMock.onGet(`/v1/consent/directives/me`).reply(200, responseMock);
axiosMock
.onGet(`/v1/consent/directives/me?includeForm=true`)
.reply(200, responseMock);

const useTestHook = () => {
const { useConsentDirectives } = useConsent();
Expand All @@ -66,7 +68,9 @@ describe('useConsentDirectives', () => {

const { result } = renderHookInContext(useTestHook);

expect(axiosMock.history.get[0].url).toBe(`/v1/consent/directives/me`);
expect(axiosMock.history.get[0].url).toBe(
`/v1/consent/directives/me?includeForm=true`,
);

await waitFor(() => {
expect(result.current.data).toEqual(responseMock);
Expand Down Expand Up @@ -164,7 +168,9 @@ describe('useShouldRenderConsentScreen', () => {
};
const responseMock = { items: [directive] };

axiosMock.onGet(`/v1/consent/directives/me`).reply(200, responseMock);
axiosMock
.onGet(`/v1/consent/directives/me?includeForm=true`)
.reply(200, responseMock);

const useTestHook = () => {
const { useShouldRenderConsentScreen } = useConsent();
Expand All @@ -186,7 +192,9 @@ describe('useShouldRenderConsentScreen', () => {
status: 'proposed',
};
const responseMock = { items: [directive] };
axiosMock.onGet(`/v1/consent/directives/me`).reply(200, responseMock);
axiosMock
.onGet(`/v1/consent/directives/me?includeForm=true`)
.reply(200, responseMock);

const useTestHook = () => {
const { useShouldRenderConsentScreen } = useConsent();
Expand Down Expand Up @@ -220,7 +228,9 @@ describe('useShouldRenderConsentScreen', () => {
};
const responseMock = { items: [directive] };

axiosMock.onGet(`/v1/consent/directives/me`).reply(200, responseMock);
axiosMock
.onGet(`/v1/consent/directives/me?includeForm=true`)
.reply(200, responseMock);

const useTestHook = () => {
const { useShouldRenderConsentScreen } = useConsent();
Expand Down
78 changes: 40 additions & 38 deletions src/hooks/useConsent.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
import { useQuery, useMutation } from '@tanstack/react-query';
import { useHttpClient } from './useHttpClient';
import { useActiveProject } from './useActiveProject';
import { Consent, Questionnaire } from 'fhir/r3';
import { useRestQuery, useRestMutation } from './rest-api';
import { RestAPIEndpoints } from '../types/rest-types';

export const useConsent = () => {
const { activeProject } = useActiveProject();
const { httpClient } = useHttpClient();

const useConsentDirectives = () => {
return useQuery(
[
'/v1/consent/directives/me',
{
projectId: activeProject.id,
},
],
() =>
httpClient
.get<{ items: ConsentAndForm[] }>('/v1/consent/directives/me', {
params: { projectId: activeProject.id, includeForm: true },
})
.then((res) => res.data),
);
return useRestQuery('GET /v1/consent/directives/me?includeForm=true', {
projectId: activeProject.id,
});
};

const useUpdateProjectConsentDirective = () => {
return useMutation({
mutationFn: async (params: ConsentPatch) =>
httpClient.patch(`/v1/consent/directives/me/${params.directiveId}`, {
status: params.accept ? 'active' : 'rejected',
response: {
item: [
{
linkId: 'terms',
},
{
linkId: 'acceptance',
answer: [
{
valueBoolean: params.accept,
},
],
},
],
},
}),
});
const mutation = useRestMutation(
'PATCH /v1/consent/directives/me/:directiveId',
);

const getInput = ({ directiveId, accept }: ConsentPatch) => {
const status: 'active' | 'rejected' = accept ? 'active' : 'rejected';
return {
directiveId,
status,
response: {
item: [
{
linkId: 'terms',
},
{
linkId: 'acceptance',
answer: [
{
valueBoolean: accept,
},
],
},
],
},
} as RestAPIEndpoints['PATCH /v1/consent/directives/me/:directiveId']['Request'] & {
directiveId: string;
};
};

return {
...mutation,
mutate: (values: ConsentPatch) => mutation.mutate(getInput(values)),
mutateAsync: (values: ConsentPatch) =>
mutation.mutateAsync(getInput(values)),
};
};

const useShouldRenderConsentScreen = () => {
Expand Down
33 changes: 32 additions & 1 deletion src/types/rest-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConsentDirective, SurveyResponse } from '../hooks/todayTile/types';
import type { ConsentAndForm } from '../hooks/useConsent';
import { AppConfig } from '../hooks/useAppConfig';
import { ProjectInvite, User } from '../types';
import { FhirAPIEndpoints } from './fhir-api-types';
Expand Down Expand Up @@ -85,13 +86,43 @@ export type RestAPIEndpoints = FhirAPIEndpoints & {
'GET /v1/consent/directives/me': {
Request: {
projectId: string;
includeForm: boolean;
};
Response: {
items: ConsentDirective[];
};
};

'GET /v1/consent/directives/me?includeForm=true': {
Request: {
projectId: string;
};
Response: {
items: ConsentAndForm[];
};
};

'PATCH /v1/consent/directives/me/:directiveId': {
Request: {
status: 'active' | 'rejected';
response: {
item: [
{
linkId: 'terms';
},
{
linkId: 'acceptance';
answer: [
{
valueBoolean: boolean;
},
];
},
];
};
};
Response: {};
};

'GET /v1/life-research/projects/:projectId/app-config': {
Request: { projectId: string };
Response: AppConfig;
Expand Down

0 comments on commit 31fdf61

Please sign in to comment.