-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOccupancy.tsx
executable file
·206 lines (193 loc) · 6.04 KB
/
Occupancy.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-FileCopyrightText: 2017-2024 City of Espoo
//
// SPDX-License-Identifier: LGPL-2.1-or-later
import React, {
Dispatch,
SetStateAction,
useCallback,
useMemo,
useState
} from 'react'
import styled from 'styled-components'
import { DaycareGroupResponse } from 'lib-common/generated/api-types/daycare'
import { UUID } from 'lib-common/types'
import Select from 'lib-components/atoms/dropdowns/Select'
import TreeDropdown, {
TreeNode
} from 'lib-components/atoms/dropdowns/TreeDropdown'
import { CollapsibleContentArea } from 'lib-components/layout/Container'
import { FixedSpaceRow } from 'lib-components/layout/flex-helpers'
import { H3, Label } from 'lib-components/typography'
import { Gap } from 'lib-components/white-space'
import { Translations, useTranslation } from '../../../state/i18n'
import { UnitFilters } from '../../../utils/UnitFilters'
import UnitDataFilters from '../UnitDataFilters'
import OccupanciesForDateRange from './occupancy/OccupanciesForDateRange'
import {
RealtimePlannedOccupanciesForSingleDay,
RealtimeRealizedOccupanciesForSingleDay,
SimpleOccupanciesForSingleDay
} from './occupancy/OccupanciesForSingleDay'
const Container = styled.div`
display: flex;
flex-direction: column;
`
type Props = {
unitId: UUID
filters: UnitFilters
setFilters: Dispatch<SetStateAction<UnitFilters>>
realtimeStaffAttendanceEnabled: boolean
shiftCareUnit: boolean
groups: DaycareGroupResponse[]
}
const dayGraphModes = ['REALIZED', 'PLANNED'] as const
type DayGraphMode = (typeof dayGraphModes)[number]
export default React.memo(function OccupancyContainer({
unitId,
filters,
setFilters,
realtimeStaffAttendanceEnabled,
shiftCareUnit,
groups
}: Props) {
const { startDate, endDate } = filters
const { i18n } = useTranslation()
const [open, setOpen] = useState(true)
const [groupIds, setGroupIds] = useState<UUID[] | null>(null)
const [mode, setMode] = useState<DayGraphMode>('PLANNED')
const activeGroups = useMemo(
() =>
groups.filter(
(group) =>
group.endDate === null || group.endDate.isEqualOrAfter(endDate)
),
[groups, endDate]
)
const tree = useMemo(
() => groupsToTree(i18n, activeGroups, groupIds),
[i18n, activeGroups, groupIds]
)
const handleTreeChange = useCallback((newTree: TreeNode[]) => {
setGroupIds(treeToGroupIds(newTree))
}, [])
return (
<CollapsibleContentArea
title={<H3 noMargin>{i18n.unit.occupancies}</H3>}
open={open}
toggleOpen={() => setOpen(!open)}
opaque
data-qa="unit-attendances"
>
<FixedSpaceRow alignItems="center">
<Label>{i18n.unit.filters.title}</Label>
<UnitDataFilters canEdit filters={filters} setFilters={setFilters} />
</FixedSpaceRow>
<Gap size="s" />
<FixedSpaceRow alignItems="center">
{startDate.isEqual(endDate) && realtimeStaffAttendanceEnabled ? (
<>
<Label>{i18n.unit.occupancy.display}</Label>
<GroupSelectWrapper>
<TreeDropdown
tree={tree}
onChange={handleTreeChange}
placeholder={i18n.common.select}
/>
</GroupSelectWrapper>
<Select
items={dayGraphModes}
selectedItem={mode}
onChange={(newMode) => {
if (newMode !== null) setMode(newMode)
}}
getItemLabel={(item) => i18n.unit.occupancy.realtime.modes[item]}
data-qa="graph-mode-select"
/>
</>
) : (
<Select
items={[null, ...activeGroups]}
selectedItem={
groupIds !== null && groupIds.length > 0
? (activeGroups.find((g) => g.id === groupIds[0]) ?? null)
: null
}
onChange={(g) => setGroupIds(g ? [g.id] : null)}
getItemValue={(g: DaycareGroupResponse | null) => (g ? g.id : '')}
getItemLabel={(g) => (g ? g.name : i18n.unit.occupancy.fullUnit)}
/>
)}
</FixedSpaceRow>
<Gap />
<Container data-qa="occupancies">
{startDate.isEqual(endDate) ? (
realtimeStaffAttendanceEnabled ? (
<div>
{mode === 'REALIZED' && (
<RealtimeRealizedOccupanciesForSingleDay
unitId={unitId}
groupIds={groupIds}
date={startDate}
shiftCareUnit={shiftCareUnit}
/>
)}
{mode === 'PLANNED' && (
<RealtimePlannedOccupanciesForSingleDay
unitId={unitId}
groupIds={groupIds}
date={startDate}
/>
)}
</div>
) : (
<SimpleOccupanciesForSingleDay
unitId={unitId}
groupId={groupIds !== null ? (groupIds[0] ?? null) : null}
date={startDate}
/>
)
) : (
<OccupanciesForDateRange
unitId={unitId}
groupId={groupIds !== null ? (groupIds[0] ?? null) : null}
from={startDate}
to={endDate}
/>
)}
</Container>
</CollapsibleContentArea>
)
})
const GroupSelectWrapper = styled.div`
min-width: 300px;
`
function groupsToTree(
i18n: Translations,
groups: DaycareGroupResponse[],
selectedGroupIds: UUID[] | null
): TreeNode[] {
return [
{
text: i18n.unit.occupancy.fullUnit,
key: 'unit',
checked: selectedGroupIds === null || selectedGroupIds.length > 0,
children: groups.map((group) => ({
text: group.name,
key: group.id,
checked:
selectedGroupIds === null || selectedGroupIds.includes(group.id),
children: []
}))
}
]
}
function treeToGroupIds(tree: TreeNode[]): UUID[] | null {
const root = tree[0]
const checkedGroupIds = root.children
.filter((child) => child.checked)
.map((child) => child.key)
if (checkedGroupIds.length === root.children.length) {
return null
}
return checkedGroupIds
}