This repository was archived by the owner on Mar 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathpick-patient-dialog.component.tsx
More file actions
136 lines (115 loc) · 4.18 KB
/
pick-patient-dialog.component.tsx
File metadata and controls
136 lines (115 loc) · 4.18 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { useCallback, useEffect, useState } from 'react';
import { Button, Form, ModalBody, ModalFooter, ModalHeader, InlineLoading } from '@carbon/react';
import {
formatDate,
navigate,
parseDate,
restBaseUrl,
showNotification,
showToast,
useSession,
} from '@openmrs/esm-framework';
import { useTranslation } from 'react-i18next';
import { extractErrorMessagesFromResponse, handleMutate, trimVisitNumber } from '../helpers/functions';
import { PatientQueue } from '../types/patient-queues';
import { getCareProvider, updateQueueEntry } from './patient-queues.resource';
interface PickPatientDialogProps {
queueEntry: PatientQueue;
closeModal: () => void;
}
const PickPatientStatus: React.FC<PickPatientDialogProps> = ({ queueEntry, closeModal }) => {
const { t } = useTranslation();
const sessionUser = useSession();
const [isLoading, setIsLoading] = useState(true);
const [provider, setProvider] = useState('');
const [priorityComment, setPriorityComment] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
// Memoize the function to fetch the provider using useCallback
const fetchProvider = useCallback(() => {
if (!sessionUser?.user?.uuid) return;
setIsLoading(true);
getCareProvider(sessionUser?.user?.uuid).then(
(response) => {
const uuid = response?.data?.results[0].uuid;
setIsLoading(false);
setProvider(uuid);
},
(error) => {
const errorMessages = extractErrorMessagesFromResponse(error);
setIsLoading(false);
showNotification({
title: "Couldn't get provider",
kind: 'error',
critical: true,
description: errorMessages.join(','),
});
},
);
}, [sessionUser?.user?.uuid]);
useEffect(() => fetchProvider(), [fetchProvider]);
const pickPatientQueueStatus = useCallback(
async (event) => {
event.preventDefault();
setIsSubmitting(true);
try {
const status = 'Picked';
await updateQueueEntry(status, provider, queueEntry?.uuid, 0, priorityComment, 'comment');
showToast({
critical: true,
title: t('updateEntry', 'Update entry'),
kind: 'success',
description: t('queueEntryUpdateSuccessfully', 'Queue Entry Updated Successfully'),
});
navigate({ to: `\${openmrsSpaBase}/patient/${queueEntry?.patient?.uuid}/chart` });
closeModal();
handleMutate(`${restBaseUrl}/patientqueue`);
setIsSubmitting(false);
} catch (error: any) {
setIsSubmitting(false);
showNotification({
title: t('queueEntryUpdateFailed', 'Error updating queue entry status'),
kind: 'error',
critical: true,
description: error?.message,
});
}
},
[provider, queueEntry?.uuid, queueEntry?.patient?.uuid, priorityComment, t, closeModal],
);
if (queueEntry && Object.keys(queueEntry)?.length === 0) {
return <ModalHeader closeModal={closeModal} title={t('patientNotInQueue', 'The patient is not in the queue')} />;
}
if (queueEntry && Object.keys(queueEntry)?.length > 0) {
return (
<div>
{isLoading && <InlineLoading description={'Fetching Provider..'} />}
<Form onSubmit={pickPatientQueueStatus}>
<ModalHeader closeModal={closeModal} title={t('pickPatient', 'Pick Patient')} />
<ModalBody>
<h5>{queueEntry?.patient?.person?.display}</h5>
<h5>VisitNo : {trimVisitNumber(queueEntry?.visitNumber)}</h5>
<h5>
Date Created :
{formatDate(parseDate(queueEntry?.dateCreated), {
time: true,
})}
</h5>
</ModalBody>
<ModalFooter>
<Button kind="secondary" onClick={closeModal}>
{t('cancel', 'Cancel')}
</Button>
{isSubmitting ? (
<InlineLoading description={'Submitting...'} />
) : (
<Button disabled={isLoading} type="submit">
{t('pickPatient', 'Pick Patient')}
</Button>
)}
</ModalFooter>
</Form>
</div>
);
}
};
export default PickPatientStatus;