Skip to content

Commit

Permalink
Remove concept label from config
Browse files Browse the repository at this point in the history
  • Loading branch information
makombe committed Feb 15, 2024
1 parent 5932ecb commit d5d4b61
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
13 changes: 3 additions & 10 deletions packages/esm-patient-labs-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,8 @@ export const configSchema = {
orderReasons: {
_type: Type.Array,
_elements: {
concept: {
_type: Type.ConceptUuid,
_description: 'Array of coded concepts that represent reasons for ordering a lab test',
},
label: {
_type: Type.String,
_default: null,
_description: 'The label for the reason for ordering concept',
},
_type: Type.ConceptUuid,
_description: 'Array of coded concepts that represent reasons for ordering a lab test',
},
_default: [],
_description: 'Coded Lab test order reason options',
Expand All @@ -81,7 +74,7 @@ export interface LabTestReason {
}
export interface OrderReason {
labTestUuid: string;
orderReasons: Array<LabTestReason>;
orderReasons: Array<string>;
}
export interface ConfigObject {
concepts: Array<ObsTreeEntry>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import classNames from 'classnames';
import { launchPatientWorkspace, promptBeforeClosing, useOrderBasket } from '@openmrs/esm-patient-common-lib';
import { translateFrom, useLayoutType, useSession, useConfig } from '@openmrs/esm-framework';
import { careSettingUuid, type LabOrderBasketItem, prepLabOrderPostData } from '../api';
import { careSettingUuid, type LabOrderBasketItem, prepLabOrderPostData, useOrderReasons } from '../api';
import {
Button,
ButtonSet,
Expand Down Expand Up @@ -73,9 +73,10 @@ export function LabOrderForm({ initialOrder, closeWorkspace }: LabOrderFormProps
},
});
const config = useConfig<ConfigObject>();
const orderReasons =
const orderReasonUuids =
(config.labTestsWithOrderReasons?.find((c) => c.labTestUuid === defaultValues?.testType?.conceptUuid) || {})
.orderReasons || [];
const { orderReasons } = useOrderReasons(orderReasonUuids);

const handleFormSubmission = useCallback(
(data: LabOrderBasketItem) => {
Expand Down Expand Up @@ -206,10 +207,10 @@ export function LabOrderForm({ initialOrder, closeWorkspace }: LabOrderFormProps
id="orderReasonInput"
titleText={t('orderReason', 'Order reason')}
selectedItem={''}
itemToString={(item) => item?.label}
itemToString={(item) => item?.display}
items={orderReasons}
onBlur={onBlur}
onChange={({ selectedItem }) => onChange(selectedItem?.concept || '')}
onChange={({ selectedItem }) => onChange(selectedItem?.uuid || '')}
/>
)}
/>
Expand Down
68 changes: 67 additions & 1 deletion packages/esm-patient-labs-app/src/lab-orders/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import useSWR, { mutate } from 'swr';
import { type FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch, useConfig, showSnackbar } from '@openmrs/esm-framework';
import { type ConfigObject } from '../config-schema';
import { useCallback, useMemo } from 'react';
import { type OrderBasketItem, type OrderPost, type PatientOrderFetchResponse } from '@openmrs/esm-patient-common-lib';
import useSWRImmutable from 'swr/immutable';

export const careSettingUuid = '6f0c9a92-6f24-11e3-af88-005056821db0';
/**
Expand Down Expand Up @@ -42,6 +43,47 @@ export function usePatientLabOrders(patientUuid: string, status: 'ACTIVE' | 'any
};
}

export function useConceptAnswers(conceptUuid: string): { data: Array<ConceptAnswers>; isLoading: boolean } {
const shouldFetch = typeof conceptUuid === 'string' && conceptUuid !== '';
const { data, error, isLoading } = useSWRImmutable<FetchResponse<ConceptResponse>, Error>(
shouldFetch ? `/ws/rest/v1/concept/${conceptUuid}` : null,
openmrsFetch,
);
if (error) {
showSnackbar({
title: error.name,
subtitle: error.message,
kind: 'error',
});
}
return { data: data?.data?.answers, isLoading };
}
export function useOrderReasons(conceptUuids: Array<string>) {
const shouldFetch = conceptUuids && conceptUuids.length > 0;
const url = shouldFetch ? getConceptReferenceUrls(conceptUuids) : null;
const { data, error, isLoading } = useSWRImmutable<FetchResponse<ConceptResponse>, Error>(
shouldFetch ? `/ws/rest/v1/${url[0]}` : null,
openmrsFetch,
);

const ob = data?.data;
const orderReasons = ob
? Object.entries(ob).map(([key, value]) => ({
uuid: value.uuid,
display: value.display,
}))
: [];

if (error) {
showSnackbar({
title: error.name,
subtitle: error.message,
kind: 'error',
});
}

return { orderReasons: orderReasons, isLoading };
}
export interface LabOrderBasketItem extends OrderBasketItem {
testType?: {
label: string;
Expand All @@ -66,9 +108,33 @@ export function prepLabOrderPostData(order: LabOrderBasketItem, patientUuid: str
orderReason: order.orderReason,
};
}
const chunkSize = 10;
export function getConceptReferenceUrls(conceptUuids: Array<string>) {
const accumulator = [];
for (let i = 0; i < conceptUuids.length; i += chunkSize) {
accumulator.push(conceptUuids.slice(i, i + chunkSize));
}

return accumulator.map((partition) => `conceptreferences?references=${partition.join(',')}&v=custom:(uuid,display)`);
}

export type PostDataPrepLabOrderFunction = (
order: LabOrderBasketItem,
patientUuid: string,
encounterUuid: string,
) => OrderPost;

export interface ConceptAnswers {
display: string;
uuid: string;
}
export interface ConceptResponse {
uuid: string;
display: string;
datatype: {
uuid: string;
display: string;
};
answers: Array<ConceptAnswers>;
setMembers: Array<ConceptAnswers>;
}

0 comments on commit d5d4b61

Please sign in to comment.