-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathusePatientCheckedInAppointments.ts
52 lines (45 loc) · 1.79 KB
/
usePatientCheckedInAppointments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { openmrsFetch, restBaseUrl, useOpenmrsSWR } from '@openmrs/esm-framework';
import dayjs from 'dayjs';
import useSWR, { mutate, SWRResponse } from 'swr';
import { type AppointmentsResponse } from '../types';
import { useCallback, useMemo, useState } from 'react';
export function usePatientAppointments(patientUuid: string, encounterUUID: string) {
const [appointmentUuids, setAppointmentUuids] = useState<Array<string>>([]);
const startDate = useMemo(() => dayjs().subtract(6, 'month').toISOString(), []);
// We need to fetch the appointments with the specified fulfilling encounter
const appointmentsSearchUrl =
encounterUUID || appointmentUuids.length > 0 ? `${restBaseUrl}/appointments/search` : null;
const swrResult = useOpenmrsSWR<AppointmentsResponse, Error>(appointmentsSearchUrl, {
fetchInit: {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
patientUuid: patientUuid,
startDate: startDate,
},
},
});
const addAppointmentForCurrentEncounter = useCallback(
(appointmentUuid: string) => {
setAppointmentUuids((prev) => (!prev.includes(appointmentUuid) ? [...prev, appointmentUuid] : prev));
swrResult.mutate();
},
[swrResult.mutate, setAppointmentUuids],
);
const results = useMemo(
() => ({
appointments: (swrResult.data?.data ?? [])?.filter(
(appointment) =>
appointment.fulfillingEncounters?.includes(encounterUUID) || appointmentUuids.includes(appointment.uuid),
),
mutateAppointments: swrResult.mutate,
isLoading: swrResult.isLoading,
error: swrResult.error,
addAppointmentForCurrentEncounter,
}),
[swrResult, addAppointmentForCurrentEncounter, appointmentUuids, encounterUUID],
);
return results;
}