From 4b6152896502266a9d821bc27c41ac1989ea6d5b Mon Sep 17 00:00:00 2001 From: Donald Kibet Date: Wed, 16 Oct 2024 13:57:30 +0300 Subject: [PATCH] (feat) O3-4087: Enable input for set-based lab tests on lab results form and handle partial set results (#2062) * (feat) O3-4087 : enable input for set-based lab tests on lab results form and handle partial set results * code reviews changes * additional code review changes --- .../lab-results-form.component.tsx | 76 ++--- .../src/lab-results/lab-results-form.test.tsx | 281 +++++++++++++++++- .../src/lab-results/lab-results.resource.ts | 69 +++++ 3 files changed, 365 insertions(+), 61 deletions(-) diff --git a/packages/esm-patient-orders-app/src/lab-results/lab-results-form.component.tsx b/packages/esm-patient-orders-app/src/lab-results/lab-results-form.component.tsx index c663316da..1745cfed1 100644 --- a/packages/esm-patient-orders-app/src/lab-results/lab-results-form.component.tsx +++ b/packages/esm-patient-orders-app/src/lab-results/lab-results-form.component.tsx @@ -5,7 +5,13 @@ import { mutate } from 'swr'; import { Button, ButtonSet, Form, InlineLoading, InlineNotification, Stack } from '@carbon/react'; import { type DefaultPatientWorkspaceProps, type Order } from '@openmrs/esm-patient-common-lib'; import { restBaseUrl, showSnackbar, useAbortController, useLayoutType } from '@openmrs/esm-framework'; -import { useOrderConceptByUuid, updateOrderResult, useLabEncounter, useObservation } from './lab-results.resource'; +import { + useOrderConceptByUuid, + updateOrderResult, + useLabEncounter, + useObservation, + createObservationPayload, +} from './lab-results.resource'; import ResultFormField from './lab-results-form-field.component'; import styles from './lab-results-form.scss'; import { useLabResultsFormSchema } from './useLabResultsFormSchema'; @@ -37,9 +43,8 @@ const LabResultsForm: React.FC = ({ const { control, formState: { errors, isDirty, isSubmitting }, - getValues, handleSubmit, - } = useForm<{ testResult: any }>({ + } = useForm<{ testResult: Record }>({ defaultValues: {}, resolver: zodResolver(schema), mode: 'all', @@ -94,66 +99,16 @@ const LabResultsForm: React.FC = ({ ); } - const saveLabResults = async (formData) => { - const formValues = getValues(); - + const saveLabResults = async (formValues: Record) => { const isEmptyForm = Object.values(formValues).every( (value) => value === '' || value === null || value === undefined, ); - if (isEmptyForm) { setShowEmptyFormErrorNotification(true); return; } - - let obsValue = []; - - if (concept.set && concept.setMembers.length > 0) { - let groupMembers = []; - concept.setMembers.forEach((member) => { - let value; - if (member.datatype.display === 'Numeric' || member.datatype.display === 'Text') { - value = getValues()[`${member.uuid}`]; - } else if (member.datatype.display === 'Coded') { - value = { - uuid: getValues()[`${member.uuid}`], - }; - } - const groupMember = { - concept: { uuid: member.uuid }, - value: value, - status: 'FINAL', - order: { uuid: order.uuid }, - }; - groupMembers.push(groupMember); - }); - - obsValue.push({ - concept: { uuid: order.concept.uuid }, - status: 'FINAL', - order: { uuid: order.uuid }, - groupMembers: groupMembers, - }); - } else if (!concept.set && concept.setMembers.length === 0) { - let value; - if (concept.datatype.display === 'Numeric' || concept.datatype.display === 'Text') { - value = getValues()[`${concept.uuid}`]; - } else if (concept.datatype.display === 'Coded') { - value = { - uuid: getValues()[`${concept.uuid}`], - }; - } - - obsValue.push({ - concept: { uuid: order.concept.uuid }, - status: 'FINAL', - order: { uuid: order.uuid }, - value: value, - }); - } - - const obsPayload = { obs: obsValue }; - + // Set the observation status to 'FINAL' as we're not capturing it in the form + const obsPayload = createObservationPayload(concept, order, formValues, 'FINAL'); const orderDiscontinuationPayload = { previousOrder: order.uuid, type: 'testorder', @@ -164,7 +119,6 @@ const LabResultsForm: React.FC = ({ concept: order.concept.uuid, orderer: order.orderer, }; - const resultsStatusPayload = { fulfillerStatus: 'COMPLETED', fulfillerComment: 'Test Results Entered', @@ -179,7 +133,6 @@ const LabResultsForm: React.FC = ({ orderDiscontinuationPayload, abortController, ); - closeWorkspaceWithSavedChanges(); mutateLabOrders(); mutateOrderData(); @@ -227,7 +180,12 @@ const LabResultsForm: React.FC = ({ -