diff --git a/jest.config.js b/jest.config.js index 5d7f4b40b6..0d2ef7d519 100644 --- a/jest.config.js +++ b/jest.config.js @@ -14,6 +14,7 @@ module.exports = { '^@carbon/charts-react$': path.resolve(__dirname, '__mocks__', '@carbon__charts-react.ts'), '^dexie$': require.resolve('dexie'), '^lodash-es/(.*)$': 'lodash/$1', + '^lodash-es$': 'lodash', '^react-i18next$': path.resolve(__dirname, '__mocks__', 'react-i18next.js'), '^uuid$': path.resolve(__dirname, 'node_modules', 'uuid', 'dist', 'index.js'), }, 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 2e7cde645e..a8eb1aed9c 100644 --- a/packages/esm-patient-labs-app/src/lab-orders/api.ts +++ b/packages/esm-patient-labs-app/src/lab-orders/api.ts @@ -1,9 +1,10 @@ import useSWR, { mutate } from 'swr'; -import { type FetchResponse, openmrsFetch, useConfig, restBaseUrl, showSnackbar } from '@openmrs/esm-framework'; -import { type ConfigObject } from '../config-schema'; import { useCallback, useMemo } from 'react'; -import type { OrderPost, PatientOrderFetchResponse, LabOrderBasketItem } from '@openmrs/esm-patient-common-lib'; import useSWRImmutable from 'swr/immutable'; +import { chunk } from 'lodash-es'; +import { type FetchResponse, openmrsFetch, useConfig, restBaseUrl, showSnackbar } from '@openmrs/esm-framework'; +import type { OrderPost, PatientOrderFetchResponse, LabOrderBasketItem } from '@openmrs/esm-patient-common-lib'; +import { type ConfigObject } from '../config-schema'; export const careSettingUuid = '6f0c9a92-6f24-11e3-af88-005056821db0'; /** @@ -44,16 +45,14 @@ export function usePatientLabOrders(patientUuid: string, status: 'ACTIVE' | 'any } export function useOrderReasons(conceptUuids: Array) { - const shouldFetch = conceptUuids && conceptUuids.length > 0; - const url = shouldFetch ? getConceptReferenceUrls(conceptUuids) : null; - const { data, error, isLoading } = useSWRImmutable, Error>( - shouldFetch ? `${restBaseUrl}/${url[0]}` : null, - openmrsFetch, + const { data, error, isLoading } = useSWRImmutable>, Error>( + conceptUuids && conceptUuids.length > 0 ? getConceptReferenceUrls(conceptUuids) : null, + (key: Array) => Promise.all(key.map((url) => openmrsFetch(url))), ); - const ob = data?.data; + const ob: ConceptReferenceResponse = data?.reduce((acc, response) => ({ ...acc, ...response.data }), {}); const orderReasons = ob - ? Object.entries(ob).map(([key, value]) => ({ + ? Object.values(ob).map((value) => ({ uuid: value.uuid, display: value.display, })) @@ -70,6 +69,12 @@ export function useOrderReasons(conceptUuids: Array) { return { orderReasons: orderReasons, isLoading }; } +function getConceptReferenceUrls(conceptUuids: Array) { + return chunk(conceptUuids, 10).map( + (partition) => `${restBaseUrl}/conceptreferences?references=${partition.join(',')}&v=custom:(uuid,display)`, + ); +} + export function prepLabOrderPostData(order: LabOrderBasketItem, patientUuid: string, encounterUuid: string): OrderPost { if (order.action === 'NEW' || order.action === 'RENEW') { return { @@ -112,15 +117,6 @@ export function prepLabOrderPostData(order: LabOrderBasketItem, patientUuid: str throw new Error(`Unknown order action: ${order.action}.`); } } -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, @@ -132,13 +128,16 @@ export interface ConceptAnswers { display: string; uuid: string; } -export interface ConceptResponse { - uuid: string; - display: string; - datatype: { + +export interface ConceptReferenceResponse { + [key: string]: { uuid: string; display: string; + datatype: { + uuid: string; + display: string; + }; + answers: Array; + setMembers: Array; }; - answers: Array; - setMembers: Array; }