diff --git a/web/src/containers/Main/context.ts b/web/src/containers/Main/context.ts index 8f81abf3..c91f4178 100644 --- a/web/src/containers/Main/context.ts +++ b/web/src/containers/Main/context.ts @@ -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> +type PopulateService = (launchContext:Parameters) => Promise> + +interface SDCContextType { + assemble: AssembleService + populate: PopulateService +} export const FormRenderContext = createContext(QRFormWrapper); +export const SDCContext = createContext({ assemble, populate }) diff --git a/web/src/containers/Main/useMain.ts b/web/src/containers/Main/useMain.ts index 26435146..109b90c4 100644 --- a/web/src/containers/Main/useMain.ts +++ b/web/src/containers/Main/useMain.ts @@ -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'; @@ -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({ @@ -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>>(notAsked); @@ -104,14 +105,9 @@ export function useMain(questionnaireId: string) { return response; }, [questionnaireId]); - const [assembledQuestionnaireRD, assembledQuestionnaireRDManager] = useService(async () => { - const response = await service({ - method: 'GET', - url: `Questionnaire/${questionnaireId}/$assemble`, - }); - - return response; - }, [questionnaireId]); + const [assembledQuestionnaireRD, assembledQuestionnaireRDManager] = useService( + () => assemble(questionnaireId), + [questionnaireId, assemble]); const reloadQuestionnaire = useCallback(async () => { originalQuestionnaireRDManager.reload(); @@ -180,15 +176,9 @@ export function useMain(questionnaireId: string) { [saveQuestionnaire, navigate], ); - const [questionnaireResponseRD, questionnaireResponseRDManager] = useService(async () => { - const response = await service({ - method: 'POST', - url: '/Questionnaire/$populate', - data: launchContext, - }); - - return response; - }, [launchContext]); + const [questionnaireResponseRD, questionnaireResponseRDManager] = useService( + () => populate(launchContext), + [launchContext, populate]); const createMapping = useCallback( async (mapping: Mapping) => { diff --git a/web/src/services/sdc.ts b/web/src/services/sdc.ts new file mode 100644 index 00000000..934f9b2e --- /dev/null +++ b/web/src/services/sdc.ts @@ -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({ + method: 'POST', + url: '/Questionnaire/$populate', + data: launchContext, + }); + return response; +} + +export async function assemble(questionnaireId:string){ + const response = await service({ + method: 'GET', + url: `Questionnaire/${questionnaireId}/$assemble`, + }); + + return response; +}