Skip to content

Commit

Permalink
(feat) O3-3768 Prepopulate program states field in different forms fo…
Browse files Browse the repository at this point in the history
…r questions with the same program
  • Loading branch information
CynthiaKamau committed Aug 28, 2024
1 parent 8e30a8e commit 3568f24
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/adapters/program-state-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ export const ProgramStateAdapter: FormFieldValueAdapter = {
sourceObject: OpenmrsResource,
context: FormProcessorContextProps,
): Promise<any> {
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) {
Expand Down
10 changes: 10 additions & 0 deletions src/components/renderer/field/form-field-renderer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/usePatientPrograms.ts
Original file line number Diff line number Diff line change
@@ -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<Array<PatientProgram>>([]);
Expand Down

0 comments on commit 3568f24

Please sign in to comment.