Skip to content

Commit

Permalink
(refactor) updated the hook
Browse files Browse the repository at this point in the history
  • Loading branch information
its-kios09 committed Aug 21, 2024
1 parent 2eaad49 commit a16a5a5
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { openmrsFetch, type OpenmrsResource, type Patient, type Visit } from '@openmrs/esm-framework';
import {
type FetchResponse,
openmrsFetch,
type OpenmrsResource,
type Patient,
type Visit,
} from '@openmrs/esm-framework';
import useSWR from 'swr';
import useSWRImmutable from 'swr/immutable';
import { type QueueEntry } from '../types';
import { FetcherResponse } from 'swr/_internal';

export function useLatestQueueEntry(patientUuid: string) {
const customRepresentation =
'custom:(uuid,display,queue:(uuid,display,name,location:(uuid,display),service:(uuid,display),allowedPriorities:(uuid,display),allowedStatuses:(uuid,display)),status,patient:(uuid,display),visit:(uuid,display,startDatetime),priority,priorityComment,sortWeight,startedAt,endedAt,locationWaitingFor,queueComingFrom,providerWaitingFor,previousQueueEntry)';

const encodedRepresentation = encodeURIComponent(customRepresentation);
const url = `/ws/rest/v1/queue-entry?v=${encodedRepresentation}&patient=${patientUuid}&isEnded=false`;

const fetcher = async (url: string) => {
const response = await openmrsFetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return data;
};

const { data, error, isLoading, mutate } = useSWR<{ results: QueueEntry[] }>(url, fetcher);
const { data, error, isLoading, mutate } = useSWR<FetchResponse<{ results: QueueEntry[] }>>(url, openmrsFetch);

const queueEntry =
data?.results.reduce((latestEntry, currentEntry) => {
data?.data?.results?.reduce((latestEntry, currentEntry) => {
if (!latestEntry || new Date(currentEntry.startedAt) > new Date(latestEntry.startedAt)) {
return currentEntry;
}
Expand Down

0 comments on commit a16a5a5

Please sign in to comment.