-
Notifications
You must be signed in to change notification settings - Fork 206
/
patient-queue-header.component.tsx
95 lines (89 loc) · 3.28 KB
/
patient-queue-header.component.tsx
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
import React, { useCallback, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Dropdown } from '@carbon/react';
import { useConfig, useSession, PageHeader, PageHeaderContent, ServiceQueuesPictogram } from '@openmrs/esm-framework';
import { useQueueLocations } from '../patient-search/hooks/useQueueLocations';
import {
updateSelectedQueueLocationUuid,
updateSelectedQueueLocationName,
updateSelectedService,
useSelectedQueueLocationName,
useSelectedQueueLocationUuid,
} from '../helpers/helpers';
import type { ConfigObject } from '../config-schema';
import styles from './patient-queue-header.scss';
interface PatientQueueHeaderProps {
title?: string | JSX.Element;
showLocationDropdown: boolean;
actions?: React.ReactNode;
}
const PatientQueueHeader: React.FC<PatientQueueHeaderProps> = ({ title, showLocationDropdown, actions }) => {
const { t } = useTranslation();
const { queueLocations, isLoading, error } = useQueueLocations();
const { dashboardTitle } = useConfig<ConfigObject>();
const userSession = useSession();
const currentQueueLocationName = useSelectedQueueLocationName();
const currentQueueLocationUuid = useSelectedQueueLocationUuid();
const handleQueueLocationChange = useCallback(({ selectedItem }) => {
if (selectedItem.id === 'all') {
updateSelectedQueueLocationUuid(null);
updateSelectedQueueLocationName(null);
} else {
updateSelectedQueueLocationUuid(selectedItem.id);
updateSelectedQueueLocationName(selectedItem.name);
updateSelectedService(null, t('all', 'All'));
}
}, []);
useEffect(() => {
if (!isLoading && !error && !currentQueueLocationUuid) {
if (queueLocations.length === 1) {
handleQueueLocationChange({ selectedItem: queueLocations[0] });
}
if (
queueLocations.some((location) => location.id === userSession?.sessionLocation?.uuid) &&
currentQueueLocationUuid
) {
handleQueueLocationChange({
selectedItem: {
id: userSession?.sessionLocation?.uuid,
name: userSession?.sessionLocation?.display,
},
});
}
}
}, [
queueLocations,
currentQueueLocationName,
currentQueueLocationUuid,
isLoading,
error,
userSession?.sessionLocation?.uuid,
]);
return (
<PageHeader className={styles.header} data-testid="patient-queue-header">
<PageHeaderContent
title={title ? title : t(dashboardTitle.key, dashboardTitle.value)}
illustration={<ServiceQueuesPictogram />}
/>
<div className={styles.dropdownContainer}>
{showLocationDropdown && (
<Dropdown
aria-label={t('selectQueueLocation', 'Select queue location')}
className={styles.dropdown}
id="queueLocationDropdown"
label={currentQueueLocationName ?? t('all', 'All')}
items={
queueLocations.length !== 1 ? [{ id: 'all', name: t('all', 'All') }, ...queueLocations] : queueLocations
}
itemToString={(item) => (item ? item.name : '')}
titleText={t('location', 'Location')}
type="inline"
onChange={handleQueueLocationChange}
/>
)}
{actions}
</div>
</PageHeader>
);
};
export default PatientQueueHeader;