Skip to content

Commit

Permalink
(feat) O3-4087 : enable input for set-based lab tests on lab results …
Browse files Browse the repository at this point in the history
…form and handle partial set results
  • Loading branch information
donaldkibet committed Oct 15, 2024
1 parent f8937d4 commit 92d6ff6
Show file tree
Hide file tree
Showing 3 changed files with 428 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -33,9 +39,7 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({

const {
control,
register,
formState: { errors, isDirty, isSubmitting },
getValues,
formState: { isDirty, isSubmitting },
handleSubmit,
} = useForm<{ testResult: any }>({
defaultValues: {},
Expand Down Expand Up @@ -82,66 +86,15 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
);
}

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',
Expand All @@ -152,50 +105,48 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
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 (
<Form className={styles.form}>
<Form className={styles.form} onSubmit={handleSubmit(saveLabResults)}>
<div className={styles.grid}>
{concept.setMembers.length > 0 && <p className={styles.heading}>{concept.display}</p>}
{concept && (
Expand All @@ -220,13 +171,7 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
<Button className={styles.button} kind="secondary" disabled={isSubmitting} onClick={closeWorkspace}>
{t('discard', 'Discard')}
</Button>
<Button
className={styles.button}
kind="primary"
onClick={handleSubmit(saveLabResults)}
disabled={isSubmitting}
type="submit"
>
<Button className={styles.button} kind="primary" disabled={isSubmitting} type="submit">
{isSubmitting ? (
<InlineLoading description={t('saving', 'Saving') + '...'} />
) : (
Expand Down
Loading

0 comments on commit 92d6ff6

Please sign in to comment.