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 4632389dd..879b72487 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'; @@ -33,9 +39,7 @@ const LabResultsForm: React.FC = ({ const { control, - register, - formState: { errors, isDirty, isSubmitting }, - getValues, + formState: { isDirty, isSubmitting }, handleSubmit, } = useForm<{ testResult: any }>({ defaultValues: {}, @@ -82,66 +86,15 @@ const LabResultsForm: React.FC = ({ ); } - const saveLabResults = () => { - const formValues = getValues(); - + const saveLabResults = async (formValues) => { 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 }; - + const obsPayload = createObservationPayload(concept, order, formValues); const orderDiscontinuationPayload = { previousOrder: order.uuid, type: 'testorder', @@ -152,50 +105,48 @@ const LabResultsForm: React.FC = ({ concept: order.concept.uuid, orderer: order.orderer, }; - const resultsStatusPayload = { fulfillerStatus: 'COMPLETED', fulfillerComment: 'Test Results Entered', }; - updateOrderResult( - order.uuid, - order.encounter.uuid, - obsPayload, - resultsStatusPayload, - orderDiscontinuationPayload, - abortController, - ).then( - () => { - closeWorkspaceWithSavedChanges(); - mutateLabOrders(); - mutate( - (key) => typeof key === 'string' && key.startsWith(`${restBaseUrl}/order?patient=${order.patient.uuid}`), - undefined, - { revalidate: true }, - ); - showSnackbar({ - title: t('saveLabResults', 'Save lab results'), - kind: 'success', - subtitle: t('successfullySavedLabResults', 'Lab results for {{orderNumber}} have been successfully updated', { - orderNumber: order?.orderNumber, - }), - }); - }, - (err) => { - showSnackbar({ - title: t('errorSavingLabResults', 'Error saving lab results'), - kind: 'error', - subtitle: err?.message, - }); - }, - ); - - setShowEmptyFormErrorNotification(false); + try { + await updateOrderResult( + order.uuid, + order.encounter.uuid, + obsPayload, + resultsStatusPayload, + orderDiscontinuationPayload, + abortController, + ); + + closeWorkspaceWithSavedChanges(); + mutateLabOrders(); + mutate( + (key) => typeof key === 'string' && key.startsWith(`${restBaseUrl}/order?patient=${order.patient.uuid}`), + undefined, + { revalidate: true }, + ); + showSnackbar({ + title: t('saveLabResults', 'Save lab results'), + kind: 'success', + subtitle: t('successfullySavedLabResults', 'Lab results for {{orderNumber}} have been successfully updated', { + orderNumber: order?.orderNumber, + }), + }); + } catch (err) { + showSnackbar({ + title: t('errorSavingLabResults', 'Error saving lab results'), + kind: 'error', + subtitle: err?.message, + }); + } finally { + setShowEmptyFormErrorNotification(false); + } }; return ( -
+
{concept.setMembers.length > 0 &&

{concept.display}

} {concept && ( @@ -220,13 +171,7 @@ const LabResultsForm: React.FC = ({ -