-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathusePatientCheckedInAppointments.ts
More file actions
97 lines (87 loc) · 3.29 KB
/
usePatientCheckedInAppointments.ts
File metadata and controls
97 lines (87 loc) · 3.29 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { restBaseUrl, useOpenmrsSWR } from '@openmrs/esm-framework';
import dayjs from 'dayjs';
import type { Appointment, AppointmentsResponse } from '../types';
import { useCallback, useMemo, useState } from 'react';
export interface UsePatientAppointmentsResults {
/**
* All the appointments for the patient and encounter, including the newly created appointments
*/
appointments: Array<Appointment>;
/**
* The newly created appointments that doesn't have fulfilling encounters yet
*/
newlyCreatedAppointments: Array<Appointment>;
isLoadingAppointments: boolean;
errorFetchingAppointments: Error;
isValidatingAppointments: boolean;
/**
* When new appointments are created, they need to be added to the list of newly created appointments
* @param appointmentUuid The UUID of the appointment to add
*/
addAppointmentForCurrentEncounter: (appointmentUuid: string) => void;
}
/**
* Returns the appointments for the specified patient and encounter
* @param patientUuid The UUID of the patient
* @param encounterUUID The encounter UUID to filter the appointments by their fulfilling encounters
*/
export function usePatientAppointments(patientUuid: string, encounterUUID: string): UsePatientAppointmentsResults {
const [newlyCreatedAppointmentUuids, setNewlyCreatedAppointmentUuids] = 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 || newlyCreatedAppointmentUuids.length > 0 ? `${restBaseUrl}/appointments/search` : null;
const {
data,
isLoading: isLoadingAppointments,
error: errorFetchingAppointments,
isValidating: isValidatingAppointments,
mutate: refetchAppointments,
} = useOpenmrsSWR<AppointmentsResponse, Error>(appointmentsSearchUrl, {
fetchInit: {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
patientUuid: patientUuid,
startDate: startDate,
},
},
});
const addAppointmentForCurrentEncounter = useCallback(
(appointmentUuid: string) => {
setNewlyCreatedAppointmentUuids((prev) => (!prev.includes(appointmentUuid) ? [...prev, appointmentUuid] : prev));
refetchAppointments();
},
[refetchAppointments, setNewlyCreatedAppointmentUuids],
);
const results = useMemo(() => {
const appointmentsWithEncounter = [];
const newlyCreatedAppointments = [];
(data?.data ?? [])?.forEach((appointment) => {
if (appointment.fulfillingEncounters?.includes(encounterUUID)) {
appointmentsWithEncounter.push(appointment);
} else if (newlyCreatedAppointmentUuids.includes(appointment.uuid)) {
newlyCreatedAppointments.push(appointment);
}
});
return {
appointments: [...newlyCreatedAppointments, ...appointmentsWithEncounter],
newlyCreatedAppointments,
isLoadingAppointments,
errorFetchingAppointments,
isValidatingAppointments,
addAppointmentForCurrentEncounter,
};
}, [
addAppointmentForCurrentEncounter,
data,
encounterUUID,
errorFetchingAppointments,
isLoadingAppointments,
isValidatingAppointments,
newlyCreatedAppointmentUuids,
]);
return results;
}