From 3568f248f554edc18bb5bbe078231952e8e1d920 Mon Sep 17 00:00:00 2001 From: CynthiaKamau Date: Thu, 8 Aug 2024 13:18:40 +0300 Subject: [PATCH] (feat) O3-3768 Prepopulate program states field in different forms for questions with the same program --- src/adapters/program-state-adapter.ts | 12 +++++++++++- .../renderer/field/form-field-renderer.component.tsx | 10 ++++++++++ src/hooks/usePatientPrograms.ts | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/adapters/program-state-adapter.ts b/src/adapters/program-state-adapter.ts index e7a8132a1..971e55685 100644 --- a/src/adapters/program-state-adapter.ts +++ b/src/adapters/program-state-adapter.ts @@ -38,7 +38,17 @@ export const ProgramStateAdapter: FormFieldValueAdapter = { sourceObject: OpenmrsResource, context: FormProcessorContextProps, ): Promise { - return null; + const program = context.customDependencies.patientPrograms?.find( + (program) => program.program.uuid === field.questionOptions.programUuid, + ); + if (program?.states?.length > 0) { + const currentState = program.states + .filter((state) => !state.endDate) + .find((state) => state.state.programWorkflow?.uuid === field.questionOptions.workflowUuid)?.state; + field.meta = { ...(field.meta || {}), previousValue: currentState }; + return Promise.resolve({ value : currentState.uuid, display: currentState.concept.display }); + } + return Promise.resolve(null); }, getDisplayValue: function (field: FormField, value: any) { if (value?.display) { diff --git a/src/components/renderer/field/form-field-renderer.component.tsx b/src/components/renderer/field/form-field-renderer.component.tsx index f6db4e218..efeb1744d 100644 --- a/src/components/renderer/field/form-field-renderer.component.tsx +++ b/src/components/renderer/field/form-field-renderer.component.tsx @@ -76,6 +76,16 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi console.error(error); } } + + if (field.type === 'programState' && field.questionOptions.enablePreviousValue) { + try { + context.processor.getHistoricalValue(field, context).then((value) => { + setHistoricalValue({value: value.value, display: value.display}); + }); + } catch (error) { + console.error(error); + } + } }, []); useEffect(() => { diff --git a/src/hooks/usePatientPrograms.ts b/src/hooks/usePatientPrograms.ts index 45ce5dbe3..b94f93e56 100644 --- a/src/hooks/usePatientPrograms.ts +++ b/src/hooks/usePatientPrograms.ts @@ -1,7 +1,7 @@ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; import { useEffect, useState } from 'react'; import { type FormSchema, type PatientProgram } from '../types'; -const customRepresentation = `custom:(uuid,display,program:(uuid,name,allWorkflows),dateEnrolled,dateCompleted,location:(uuid,display),states:(startDate,endDate,state:(uuid,name,retired,concept:(uuid),programWorkflow:(uuid)))`; +const customRepresentation = `custom:(uuid,display,program:(uuid,name,allWorkflows),dateEnrolled,dateCompleted,location:(uuid,display),states:(startDate,endDate,state:(uuid,name,retired,concept:(uuid,display),programWorkflow:(uuid)))`; export const usePatientPrograms = (patientUuid: string, formJson: FormSchema) => { const [patientPrograms, setPatientPrograms] = useState>([]);