Skip to content

Commit

Permalink
(feat) O3-4087: Enable input for set-based lab tests on lab results f…
Browse files Browse the repository at this point in the history
…orm 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
  • Loading branch information
donaldkibet authored Oct 16, 2024
1 parent 0c438fe commit c49d112
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 61 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';
import { useLabResultsFormSchema } from './useLabResultsFormSchema';
Expand Down Expand Up @@ -37,9 +43,8 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
const {
control,
formState: { errors, isDirty, isSubmitting },
getValues,
handleSubmit,
} = useForm<{ testResult: any }>({
} = useForm<{ testResult: Record<string, unknown> }>({
defaultValues: {},
resolver: zodResolver(schema),
mode: 'all',
Expand Down Expand Up @@ -94,66 +99,16 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
);
}

const saveLabResults = async (formData) => {
const formValues = getValues();

const saveLabResults = async (formValues: Record<string, unknown>) => {
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',
Expand All @@ -164,7 +119,6 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
concept: order.concept.uuid,
orderer: order.orderer,
};

const resultsStatusPayload = {
fulfillerStatus: 'COMPLETED',
fulfillerComment: 'Test Results Entered',
Expand All @@ -179,7 +133,6 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
orderDiscontinuationPayload,
abortController,
);

closeWorkspaceWithSavedChanges();
mutateLabOrders();
mutateOrderData();
Expand Down Expand Up @@ -227,7 +180,12 @@ 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" disabled={isSubmitting} type="submit">
<Button
className={styles.button}
kind="primary"
disabled={isSubmitting || Object.keys(errors).length > 0}
type="submit"
>
{isSubmitting ? (
<InlineLoading description={t('saving', 'Saving') + '...'} />
) : (
Expand Down
Loading

0 comments on commit c49d112

Please sign in to comment.