import React, { useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import uniqBy from 'lodash-es/uniqBy'; import { Button, Dropdown, DropdownSkeleton, InlineNotification, ModalBody, ModalFooter, ModalHeader, Stack, } from '@carbon/react'; import { getLocale } from '@openmrs/esm-framework'; import { useQueueLocations } from '../../create-queue-entry/hooks/useQueueLocations'; import { useQueues } from '../../hooks/useQueues'; import { updateSelectedQueueLocationName, updateSelectedQueueLocationUuid, updateSelectedService, useServiceQueuesStore, } from '../../store/store'; import { type Concept } from '../../types'; interface ChangeQueueLocationModalProps { closeModal: () => void; } const ALL = 'all'; // The dropdowns list an "All" entry alongside the fetched locations/services, so items are typed to the // fields both share: `id`/`name` for locations (fhir.Location) and `uuid`/`display` for services. type LocationItem = Pick; type ServiceItem = Pick; const ChangeQueueLocationModal: React.FC = ({ closeModal }) => { const { t } = useTranslation(); const { queueLocations, isLoading, error } = useQueueLocations(); const { selectedQueueLocationUuid, selectedServiceUuid } = useServiceQueuesStore(); const [locationUuid, setLocationUuid] = useState(selectedQueueLocationUuid ?? ALL); const [serviceUuid, setServiceUuid] = useState(selectedServiceUuid ?? ALL); const { queues, isLoading: isLoadingQueues, error: queuesError, } = useQueues(locationUuid === ALL ? undefined : locationUuid); useEffect(() => { if (queuesError) { console.error('Failed to load queues for the change-queue-location modal: ', queuesError); } }, [queuesError]); const services = useMemo( () => uniqBy( queues.flatMap((queue) => queue.service), (service) => service?.uuid, ).sort((a, b) => (a?.display ?? '').localeCompare(b?.display ?? '', getLocale())), [queues], ); const allLocation: LocationItem = { id: ALL, name: t('all', 'All') }; const locationItems: Array = [allLocation, ...queueLocations]; const allService: ServiceItem = { uuid: ALL, display: t('all', 'All') }; const serviceItems: Array = [allService, ...services]; const handleLocationChange = ({ selectedItem }: { selectedItem: LocationItem }) => { setLocationUuid(selectedItem.id); setServiceUuid(ALL); }; const handleSubmit = () => { if (locationUuid === ALL) { updateSelectedQueueLocationUuid(null); updateSelectedQueueLocationName(null); } else { const location = queueLocations.find(({ id }) => id === locationUuid); updateSelectedQueueLocationUuid(location?.id); updateSelectedQueueLocationName(location?.name); } if (serviceUuid === ALL) { updateSelectedService(null, t('all', 'All')); } else { const service = services.find(({ uuid }) => uuid === serviceUuid); updateSelectedService(service?.uuid, service?.display); } closeModal(); }; return ( <> {isLoading ? ( ) : error ? ( ) : ( item?.name ?? ''} selectedItem={locationItems.find(({ id }) => id === locationUuid) ?? allLocation} onChange={handleLocationChange} /> item?.display ?? ''} selectedItem={serviceItems.find(({ uuid }) => uuid === serviceUuid) ?? allService} onChange={({ selectedItem }: { selectedItem: ServiceItem }) => setServiceUuid(selectedItem?.uuid ?? ALL)} disabled={isLoadingQueues || !!queuesError} /> {queuesError && ( )} )} ); }; export default ChangeQueueLocationModal;