|
| 1 | +import React, { useCallback, useEffect, useMemo, useState } from 'react'; |
| 2 | +import { Button, Form, ModalBody, ModalFooter, ModalHeader, InlineLoading } from '@carbon/react'; |
| 3 | +import styles from './end-visit-modal.scss'; |
| 4 | +import { useTranslation } from 'react-i18next'; |
| 5 | +import { |
| 6 | + getCoreTranslation, |
| 7 | + getSessionStore, |
| 8 | + navigate, |
| 9 | + parseDate, |
| 10 | + restBaseUrl, |
| 11 | + showNotification, |
| 12 | + showSnackbar, |
| 13 | + useSession, |
| 14 | + useVisit, |
| 15 | +} from '@openmrs/esm-framework'; |
| 16 | +import { |
| 17 | + getCareProvider, |
| 18 | + getCurrentPatientQueueByPatientUuid, |
| 19 | + updateQueueEntry, |
| 20 | + updateVisit, |
| 21 | +} from '../patient-queues.resource'; |
| 22 | +import { QueueStatus, extractErrorMessagesFromResponse, handleMutate } from '../../utils/utils'; |
| 23 | + |
| 24 | +interface EndVisitConfirmationProps { |
| 25 | + patientUuid: string; |
| 26 | + closeModal: () => void; |
| 27 | +} |
| 28 | + |
| 29 | +const EndVisitConfirmation: React.FC<EndVisitConfirmationProps> = ({ closeModal, patientUuid }) => { |
| 30 | + const { t } = useTranslation(); |
| 31 | + |
| 32 | + const [isFetchingProvider, setIsFetchingProvider] = useState(false); |
| 33 | + |
| 34 | + const [isEndingVisit, setIsEndingVisit] = useState(false); |
| 35 | + |
| 36 | + const priorityLabels = useMemo(() => ['Not Urgent', 'Urgent', 'Emergency'], []); |
| 37 | + |
| 38 | + const [provider, setProvider] = useState(''); |
| 39 | + |
| 40 | + const { activeVisit } = useVisit(patientUuid); |
| 41 | + |
| 42 | + const sessionUser = useSession(); |
| 43 | + |
| 44 | + // Memoize the function to fetch the provider using useCallback |
| 45 | + const fetchProvider = useCallback(() => { |
| 46 | + if (!sessionUser?.user?.uuid) return; |
| 47 | + |
| 48 | + setIsFetchingProvider(true); |
| 49 | + |
| 50 | + getCareProvider(sessionUser?.user?.uuid).then( |
| 51 | + (response) => { |
| 52 | + const uuid = response?.data?.results[0].uuid; |
| 53 | + setIsFetchingProvider(false); |
| 54 | + setProvider(uuid); |
| 55 | + }, |
| 56 | + (error) => { |
| 57 | + const errorMessages = extractErrorMessagesFromResponse(error); |
| 58 | + setIsFetchingProvider(false); |
| 59 | + showNotification({ |
| 60 | + title: "Couldn't get provider", |
| 61 | + kind: 'error', |
| 62 | + critical: true, |
| 63 | + description: errorMessages.join(','), |
| 64 | + }); |
| 65 | + }, |
| 66 | + ); |
| 67 | + }, [sessionUser?.user?.uuid]); |
| 68 | + |
| 69 | + useEffect(() => fetchProvider(), [fetchProvider]); |
| 70 | + |
| 71 | + const handleEndVisit = async () => { |
| 72 | + setIsEndingVisit(true); |
| 73 | + |
| 74 | + const endVisitPayload = { |
| 75 | + location: activeVisit?.location?.uuid, |
| 76 | + startDatetime: parseDate(activeVisit?.startDatetime), |
| 77 | + visitType: activeVisit?.visitType?.uuid, |
| 78 | + stopDatetime: new Date(), |
| 79 | + }; |
| 80 | + |
| 81 | + try { |
| 82 | + let hasEndedVisit = false; |
| 83 | + let hasEndedQueue = false; |
| 84 | + let queueEntry; |
| 85 | + |
| 86 | + // 1. Attempt to end the visit if it exists |
| 87 | + if (activeVisit?.uuid) { |
| 88 | + const visitResponse = await updateVisit(activeVisit.uuid, endVisitPayload); |
| 89 | + if (visitResponse.status === 200) { |
| 90 | + hasEndedVisit = true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // 2. Get queue entry and end it if found |
| 95 | + const queueResponse = await getCurrentPatientQueueByPatientUuid(patientUuid, sessionUser?.sessionLocation?.uuid); |
| 96 | + |
| 97 | + const queues = queueResponse?.data?.results?.[0]?.patientQueues || []; |
| 98 | + queueEntry = queues.find((item) => item?.patient?.uuid === patientUuid); |
| 99 | + |
| 100 | + if (queueEntry) { |
| 101 | + await updateQueueEntry(QueueStatus.Completed, provider, queueEntry.uuid, 0, priorityLabels[0], 'visit-ended'); |
| 102 | + hasEndedQueue = true; |
| 103 | + } |
| 104 | + |
| 105 | + // 3. If anything was ended, proceed with navigation and feedback |
| 106 | + if (hasEndedVisit || hasEndedQueue) { |
| 107 | + let navigateTo = `${window.getOpenmrsSpaBase()}home`; |
| 108 | + |
| 109 | + if (queueEntry) { |
| 110 | + const roles = getSessionStore().getState().session?.user?.roles || []; |
| 111 | + const hasClinicianRole = roles.some((role) => role?.display === 'Organizational: Clinician'); |
| 112 | + const hasTriageRole = roles.some((role) => role?.display === 'Triage'); |
| 113 | + |
| 114 | + if (hasClinicianRole) { |
| 115 | + navigateTo = `${window.getOpenmrsSpaBase()}home/clinical-room-patient-queues`; |
| 116 | + } else if (hasTriageRole) { |
| 117 | + navigateTo = `${window.getOpenmrsSpaBase()}home/triage-patient-queues`; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + closeModal(); |
| 122 | + navigate({ to: navigateTo }); |
| 123 | + handleMutate(`${restBaseUrl}/patientqueue`); |
| 124 | + setIsEndingVisit(false); |
| 125 | + |
| 126 | + showSnackbar({ |
| 127 | + title: hasEndedVisit ? 'Visit Ended' : 'Queue Completed', |
| 128 | + subtitle: t( |
| 129 | + hasEndedVisit && hasEndedQueue ? 'endedSuccessfully' : hasEndedVisit ? 'visitEndedOnly' : 'queueEndedOnly', |
| 130 | + hasEndedVisit && hasEndedQueue |
| 131 | + ? 'Visit and queue ended successfully' |
| 132 | + : hasEndedVisit |
| 133 | + ? 'Visit ended successfully' |
| 134 | + : 'Queue ended successfully', |
| 135 | + ), |
| 136 | + kind: 'success', |
| 137 | + }); |
| 138 | + } else { |
| 139 | + // Nothing was ended |
| 140 | + closeModal(); |
| 141 | + setIsEndingVisit(false); |
| 142 | + showSnackbar({ |
| 143 | + title: 'No Action Taken', |
| 144 | + subtitle: t('noVisitOrQueueToEnd', 'No active visit or queue found to end.'), |
| 145 | + kind: 'info', |
| 146 | + }); |
| 147 | + } |
| 148 | + } catch (error) { |
| 149 | + closeModal(); |
| 150 | + setIsEndingVisit(false); |
| 151 | + const errorMessages = extractErrorMessagesFromResponse(error); |
| 152 | + showNotification({ |
| 153 | + title: t('endVisit', 'Error ending visit'), |
| 154 | + kind: 'error', |
| 155 | + critical: true, |
| 156 | + description: errorMessages.join(','), |
| 157 | + }); |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + return ( |
| 162 | + <Form> |
| 163 | + {isFetchingProvider && <InlineLoading status="active" description="Is Fetching" />} |
| 164 | + <ModalHeader closeModal={close} className={styles.modalHeader}> |
| 165 | + {t('endVisit', 'End Visit')}? |
| 166 | + </ModalHeader> |
| 167 | + <ModalBody> |
| 168 | + <p className={styles.bodyText}> |
| 169 | + {t('endVisitText', `Are you sure you want to end this visit? This action can't be undone.`)} |
| 170 | + </p> |
| 171 | + </ModalBody> |
| 172 | + <ModalFooter> |
| 173 | + <Button size="lg" kind="secondary" onClick={closeModal}> |
| 174 | + {getCoreTranslation('cancel')} |
| 175 | + </Button> |
| 176 | + <Button autoFocus kind="danger" onClick={handleEndVisit} size="lg" disabled={isEndingVisit}> |
| 177 | + {isEndingVisit ? ( |
| 178 | + <InlineLoading description={t('endingVisit', 'Ending visit...')} /> |
| 179 | + ) : ( |
| 180 | + t('endAVisit', 'End a visit') |
| 181 | + )} |
| 182 | + </Button> |
| 183 | + </ModalFooter> |
| 184 | + </Form> |
| 185 | + ); |
| 186 | +}; |
| 187 | + |
| 188 | +export default EndVisitConfirmation; |
0 commit comments