@epcare/esm-referal-laboratory-app
Version:
Laboratory microfrontend for OpenMRS 3.x
191 lines (172 loc) • 4.72 kB
text/typescript
import { FetchResponse, openmrsFetch, restBaseUrl, useConfig } from '@openmrs/esm-framework';
import { useMemo } from 'react';
import useSWR from 'swr';
import useSWRImmutable from 'swr/immutable';
export interface QueueRoomsResponse {
uuid: string;
display: string;
name: string;
description: string;
address1: string;
address2: string;
cityvillage: string;
stateprovince: string;
country: string;
postalcode: string;
latitude: string;
longitude: string;
countydistrict: string;
address3: string;
address4: string;
address5: string;
address6: string;
parentLocation: ParentLocation;
childLocations: String[];
retired: boolean;
attributes: String[];
address7: string;
address8: string;
address9: string;
address10: string;
address11: string;
address12: string;
address13: string;
address14: string;
address15: string;
resourceVersion: string;
}
export interface ParentLocation {
uuid: string;
display: string;
name: string;
description: string;
address1: string;
address2: string;
cityVillage: string;
stateProvince: string;
country: string;
postalcode: string;
latitude: string;
longitude: string;
countydistrict: string;
address3: string;
address4: string;
address5: string;
address6: string;
parentLocation: ParentLocation;
childLocations: ChildLocations[];
retired: boolean;
attributes: String[];
address7: string;
address8: string;
address9: string;
address10: string;
address11: string;
address12: string;
address13: string;
address14: string;
address15: string;
resourceversion: string;
}
export interface ChildLocations {
uuid: string;
display: string;
}
export interface ParentLocation {
uuid: string;
display: string;
}
// get queue rooms
export function useQueueRoomLocations(currentQueueLocation: string) {
const apiUrl = `${restBaseUrl}/location/${currentQueueLocation}?v=full`;
const { data, error, isLoading } = useSWR<{ data: QueueRoomsResponse }>(apiUrl, openmrsFetch);
const queueRoomLocations = useMemo(
() => data?.data?.parentLocation?.childLocations?.map((response) => response) ?? [],
[data?.data?.parentLocation?.childLocations],
);
return {
queueRoomLocations: queueRoomLocations ? queueRoomLocations : [],
isLoading,
error,
};
}
// get referral locations
export function useReferralLocations() {
const config = useConfig();
const { laboratoryReferalDestinationUuid } = config;
const apiUrl = `${restBaseUrl}/concept/${laboratoryReferalDestinationUuid}`;
const { data, error, isLoading } = useSWRImmutable<FetchResponse>(apiUrl, openmrsFetch);
return {
referrals: data ? data?.data?.answers : [],
isLoading,
};
}
// get specimen types
export function useSpecimenTypes() {
const config = useConfig();
const { laboratorySpecimenTypeConcept } = config;
const apiUrl = `${restBaseUrl}/concept/${laboratorySpecimenTypeConcept}`;
const { data, error, isLoading } = useSWRImmutable<FetchResponse>(apiUrl, openmrsFetch);
let specimenTypes = [];
if (data) {
if (data?.data?.setMembers?.length) {
specimenTypes = data.data.setMembers;
} else if (data?.data?.answers?.length) {
specimenTypes = data.data.answers;
}
}
return {
specimenTypes: specimenTypes,
isLoading,
};
}
// generate specimen id
export async function GenerateSpecimenId(uuid: string) {
const abortController = new AbortController();
return openmrsFetch(`${restBaseUrl}/generatesampleId?uuid=${uuid}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
});
}
// update Order
export async function UpdateOrder(uuid: string, body: any) {
const abortController = new AbortController();
return openmrsFetch(`${restBaseUrl}/accessionorder/${uuid}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
body: body,
});
}
export async function GetOrderByUuid(uuid: string) {
const abortController = new AbortController();
return openmrsFetch(`${restBaseUrl}/order/${uuid}`, {
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
});
}
export function extractLetters(statement: string, extractUppercase: boolean = true): string {
const words = statement.split(' ');
let result = '';
for (const word of words) {
for (const char of word) {
if (extractUppercase) {
if (char >= 'A' && char <= 'Z') {
result += char;
}
} else {
if (char >= 'a' && char <= 'z') {
result += char;
}
}
}
}
return result;
}