Skip to content

Commit

Permalink
Move assemble and populate into services provided by context
Browse files Browse the repository at this point in the history
  • Loading branch information
ir4y committed Jun 18, 2024
1 parent 9324fa2 commit 4a51eea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
12 changes: 12 additions & 0 deletions web/src/containers/Main/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { createContext } from 'react';
import { QRFormWrapper } from 'web/src/components/QRFormWrapper';
import { RemoteDataResult } from '@beda.software/remote-data';

import { Parameters, Questionnaire, QuestionnaireResponse } from "fhir/r4b";
import { QRFWrapper } from './types';
import { assemble, populate } from 'src/services/sdc';

type AssembleService = (qId:string) => Promise<RemoteDataResult<Questionnaire>>
type PopulateService = (launchContext:Parameters) => Promise<RemoteDataResult<QuestionnaireResponse>>

interface SDCContextType {
assemble: AssembleService
populate: PopulateService
}

export const FormRenderContext = createContext<QRFWrapper>(QRFormWrapper);
export const SDCContext = createContext<SDCContextType>({ assemble, populate })
28 changes: 9 additions & 19 deletions web/src/containers/Main/useMain.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
Questionnaire,
QuestionnaireResponse,
Parameters,
ParametersParameter,
Bundle,
FhirResource,
} from 'fhir/r4b';
import { useCallback, useState } from 'react';
import { useCallback, useContext, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import { generateMappingService, generateQuestionnaireService } from 'web/src/services/builder';
Expand Down Expand Up @@ -36,6 +35,7 @@ import { formatError } from '@beda.software/fhir-react';
import { Mapping } from 'shared/src/contrib/aidbox';

import { getMappings, makeMappingExtension } from './utils';
import { SDCContext } from './context';

export function useLaunchContext() {
const [launchContext, setLaunchContext] = useState<Parameters>({
Expand Down Expand Up @@ -65,6 +65,7 @@ export function useLaunchContext() {

export function useMain(questionnaireId: string) {
const navigate = useNavigate();
const {assemble, populate} = useContext(SDCContext)
const { launchContext, setLaunchContext, clearLaunchContext } = useLaunchContext();

const [mappingRD, setMappingRD] = useState<RemoteData<WithId<Mapping>>>(notAsked);
Expand Down Expand Up @@ -104,14 +105,9 @@ export function useMain(questionnaireId: string) {
return response;
}, [questionnaireId]);

const [assembledQuestionnaireRD, assembledQuestionnaireRDManager] = useService(async () => {
const response = await service<Questionnaire>({
method: 'GET',
url: `Questionnaire/${questionnaireId}/$assemble`,
});

return response;
}, [questionnaireId]);
const [assembledQuestionnaireRD, assembledQuestionnaireRDManager] = useService(
() => assemble(questionnaireId),
[questionnaireId, assemble]);

const reloadQuestionnaire = useCallback(async () => {
originalQuestionnaireRDManager.reload();
Expand Down Expand Up @@ -180,15 +176,9 @@ export function useMain(questionnaireId: string) {
[saveQuestionnaire, navigate],
);

const [questionnaireResponseRD, questionnaireResponseRDManager] = useService(async () => {
const response = await service<QuestionnaireResponse>({
method: 'POST',
url: '/Questionnaire/$populate',
data: launchContext,
});

return response;
}, [launchContext]);
const [questionnaireResponseRD, questionnaireResponseRDManager] = useService(
() => populate(launchContext),
[launchContext, populate]);

const createMapping = useCallback(
async (mapping: Mapping) => {
Expand Down
20 changes: 20 additions & 0 deletions web/src/services/sdc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Parameters, Questionnaire, QuestionnaireResponse } from "fhir/r4b";
import { service } from "./fhir";

export async function populate(launchContext:Parameters) {
const response = await service<QuestionnaireResponse>({
method: 'POST',
url: '/Questionnaire/$populate',
data: launchContext,
});
return response;
}

export async function assemble(questionnaireId:string){
const response = await service<Questionnaire>({
method: 'GET',
url: `Questionnaire/${questionnaireId}/$assemble`,
});

return response;
}

0 comments on commit 4a51eea

Please sign in to comment.