diff --git a/packages/esm-patient-labs-app/src/config-schema.ts b/packages/esm-patient-labs-app/src/config-schema.ts index 425a489c70..6313eee878 100644 --- a/packages/esm-patient-labs-app/src/config-schema.ts +++ b/packages/esm-patient-labs-app/src/config-schema.ts @@ -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', @@ -81,7 +74,7 @@ export interface LabTestReason { } export interface OrderReason { labTestUuid: string; - orderReasons: Array; + orderReasons: Array; } export interface ConfigObject { concepts: Array; diff --git a/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/lab-order-form.component.tsx b/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/lab-order-form.component.tsx index 29e819c66a..c63ad08d1b 100644 --- a/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/lab-order-form.component.tsx +++ b/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/lab-order-form.component.tsx @@ -1,8 +1,8 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, 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, @@ -73,9 +73,10 @@ export function LabOrderForm({ initialOrder, closeWorkspace }: LabOrderFormProps }, }); const config = useConfig(); - const orderReasons = + const orderReasonUuids = (config.labTestsWithOrderReasons?.find((c) => c.labTestUuid === defaultValues?.testType?.conceptUuid) || {}) .orderReasons || []; + const { orderReasons } = useOrderReasons(orderReasonUuids); const handleFormSubmission = useCallback( (data: LabOrderBasketItem) => { @@ -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 || '')} /> )} /> diff --git a/packages/esm-patient-labs-app/src/lab-orders/api.ts b/packages/esm-patient-labs-app/src/lab-orders/api.ts index a5ed4360e5..15550a2186 100644 --- a/packages/esm-patient-labs-app/src/lab-orders/api.ts +++ b/packages/esm-patient-labs-app/src/lab-orders/api.ts @@ -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'; /** @@ -42,6 +43,47 @@ export function usePatientLabOrders(patientUuid: string, status: 'ACTIVE' | 'any }; } +export function useConceptAnswers(conceptUuid: string): { data: Array; isLoading: boolean } { + const shouldFetch = typeof conceptUuid === 'string' && conceptUuid !== ''; + const { data, error, isLoading } = useSWRImmutable, 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) { + const shouldFetch = conceptUuids && conceptUuids.length > 0; + const url = shouldFetch ? getConceptReferenceUrls(conceptUuids) : null; + const { data, error, isLoading } = useSWRImmutable, 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; @@ -66,9 +108,33 @@ export function prepLabOrderPostData(order: LabOrderBasketItem, patientUuid: str orderReason: order.orderReason, }; } +const chunkSize = 10; +export function getConceptReferenceUrls(conceptUuids: Array) { + 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; + setMembers: Array; +}