import React, { useState } from 'react'; import { ComboBox, InlineNotification, DropdownSkeleton, InlineLoading, Layer } from '@carbon/react'; import { useTranslation } from 'react-i18next'; import { useLocationsByTag } from '../hooks/useAdmissionLocation'; import { type Location, navigate, setUserProperties, showSnackbar, useSession } from '@openmrs/esm-framework'; import styles from './ward-selector.scss'; const WardSelector = () => { const { t } = useTranslation(); const { locations, isLoading, error } = useLocationsByTag('Admission Location'); const { user } = useSession(); const [isSaving, setIsSaving] = useState(false); const handleWardSelect = async ({ selectedItem }) => { const selectedWardUuid = selectedItem?.uuid; if (!selectedWardUuid) { return; } if (user?.uuid) { setIsSaving(true); try { await setUserProperties(user.uuid, { defaultWardLocationUuid: selectedWardUuid, }); } catch { showSnackbar({ title: t('errorSettingDefaultWard', 'Error setting default ward view'), subtitle: t( 'errorMessageForSettingDefaultWard', 'Proceed to selected ward view, your preference has not been set, you can try again', ), isLowContrast: true, kind: 'info', }); } finally { setIsSaving(false); } } navigate({ to: `${globalThis.spaBase}/home/ward/${selectedWardUuid}` }); }; if (error) { return ( ); } if (isLoading) { return ; } return (
item?.display ?? ''} onChange={handleWardSelect} disabled={isLoading || isSaving} size="lg" light={false} downshiftProps={{ id: 'ward-selector-downshift' }} /> {isSaving && ( )}
); }; export default WardSelector;