Skip to content

Commit

Permalink
fix: 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 d907942
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
79 changes: 41 additions & 38 deletions src/hooks/useConsent.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
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', {
projectId: activeProject.id,
includeForm: true,
});
};

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
29 changes: 26 additions & 3 deletions 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 { 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,35 @@ export type RestAPIEndpoints = FhirAPIEndpoints & {
'GET /v1/consent/directives/me': {
Request: {
projectId: string;
includeForm: boolean;
includeForm: true;
};
Response: {
items: ConsentDirective[];
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 d907942

Please sign in to comment.