-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathScheduleDetails.tsx
222 lines (205 loc) · 6.55 KB
/
ScheduleDetails.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { useState, useCallback, Suspense } from 'react'
import { gql, useQuery } from 'urql'
import _ from 'lodash'
import { Edit, Delete } from '@mui/icons-material'
import DetailsPage from '../details/DetailsPage'
import ScheduleEditDialog from './ScheduleEditDialog'
import ScheduleDeleteDialog from './ScheduleDeleteDialog'
import ScheduleCalendarQuery from './ScheduleCalendarQuery'
import { QuerySetFavoriteButton } from '../util/QuerySetFavoriteButton'
import CalendarSubscribeButton from './calendar-subscribe/CalendarSubscribeButton'
import { ObjectNotFound, GenericError } from '../error-pages'
import TempSchedDialog from './temp-sched/TempSchedDialog'
import TempSchedDeleteConfirmation from './temp-sched/TempSchedDeleteConfirmation'
import { ScheduleAvatar } from '../util/avatars'
import ScheduleOverrideDialog from './ScheduleOverrideDialog'
import { useIsWidthDown } from '../util/useWidth'
import { TempSchedValue, defaultTempSchedValue } from './temp-sched/sharedUtils'
import { Redirect } from 'wouter'
import { useScheduleTZ } from './useScheduleTZ'
const query = gql`
fragment ScheduleTitleQuery on Schedule {
id
name
description
}
query scheduleDetailsQuery($id: ID!) {
schedule(id: $id) {
...ScheduleTitleQuery
timeZone
}
}
`
export interface OverrideDialog {
variantOptions: string[]
removeUserReadOnly: boolean
defaultValue?: {
addUserID?: string
removeUserID?: string
start: string
end: string
}
}
interface OverrideDialogContext {
onNewTempSched: () => void
onEditTempSched: (v: TempSchedValue) => void
onDeleteTempSched: React.Dispatch<React.SetStateAction<TempSchedValue | null>>
setOverrideDialog: React.Dispatch<React.SetStateAction<OverrideDialog | null>>
}
export const OverrideDialogContext = React.createContext<OverrideDialogContext>(
{
onNewTempSched: () => {},
onEditTempSched: () => {},
onDeleteTempSched: () => {},
setOverrideDialog: () => {},
},
)
export type ScheduleDetailsProps = {
scheduleID: string
}
export default function ScheduleDetails({
scheduleID,
}: ScheduleDetailsProps): JSX.Element {
const [showEdit, setShowEdit] = useState(false)
const [showDelete, setShowDelete] = useState(false)
const isMobile = useIsWidthDown('md')
const [editTempSched, setEditTempSched] = useState(false)
const [configTempSchedule, setConfigTempSchedule] =
useState<TempSchedValue | null>(null)
const { zone } = useScheduleTZ(scheduleID)
const onNewTempSched = useCallback(() => {
setEditTempSched(false)
setConfigTempSchedule(defaultTempSchedValue(zone))
}, [])
const onEditTempSched = useCallback((v: TempSchedValue) => {
setEditTempSched(true)
setConfigTempSchedule(v)
}, [])
const [deleteTempSchedule, setDeleteTempSchedule] =
useState<TempSchedValue | null>(null)
const onDeleteTempSched = useCallback(setDeleteTempSchedule, [])
const [overrideDialog, setOverrideDialog] = useState<OverrideDialog | null>(
null,
)
const [{ data: _data, error }] = useQuery({
query,
variables: { id: scheduleID },
})
const data = _.get(_data, 'schedule', null)
if (error) return <GenericError error={error.message} />
if (!data) {
return showDelete ? <Redirect to='/schedules' /> : <ObjectNotFound />
}
return (
<React.Fragment>
<Suspense>
{showEdit && (
<ScheduleEditDialog
scheduleID={scheduleID}
onClose={() => setShowEdit(false)}
/>
)}
{showDelete && (
<ScheduleDeleteDialog
scheduleID={scheduleID}
onClose={() => setShowDelete(false)}
/>
)}
{configTempSchedule && (
<TempSchedDialog
value={configTempSchedule}
onClose={() => setConfigTempSchedule(null)}
scheduleID={scheduleID}
edit={editTempSched}
/>
)}
{deleteTempSchedule && (
<TempSchedDeleteConfirmation
value={deleteTempSchedule}
onClose={() => setDeleteTempSchedule(null)}
scheduleID={scheduleID}
/>
)}
</Suspense>
<DetailsPage
avatar={<ScheduleAvatar />}
title={data.name}
subheader={`Time Zone: ${data.timeZone || 'Loading...'}`}
details={data.description}
pageContent={
<OverrideDialogContext.Provider
value={{
onNewTempSched,
onEditTempSched,
onDeleteTempSched,
setOverrideDialog,
}}
>
{!isMobile && <ScheduleCalendarQuery scheduleID={scheduleID} />}
<Suspense>
{overrideDialog && (
<ScheduleOverrideDialog
defaultValue={overrideDialog.defaultValue}
variantOptions={overrideDialog.variantOptions}
scheduleID={scheduleID}
onClose={() => setOverrideDialog(null)}
removeUserReadOnly={overrideDialog.removeUserReadOnly}
/>
)}
</Suspense>
</OverrideDialogContext.Provider>
}
primaryActions={[
<CalendarSubscribeButton
key='primary-action-subscribe'
scheduleID={scheduleID}
/>,
]}
secondaryActions={[
{
label: 'Edit',
icon: <Edit />,
handleOnClick: () => setShowEdit(true),
},
{
label: 'Delete',
icon: <Delete />,
handleOnClick: () => setShowDelete(true),
},
<QuerySetFavoriteButton
key='secondary-action-favorite'
id={scheduleID}
type='schedule'
/>,
]}
links={[
{
label: 'Assignments',
url: 'assignments',
subText: 'Manage rules for rotations and users',
},
{
label: 'Escalation Policies',
url: 'escalation-policies',
subText: 'Find escalation policies that link to this schedule',
},
{
label: 'Overrides',
url: 'overrides',
subText: 'Add, remove, or replace a user temporarily',
},
{
label: 'Shifts',
url: 'shifts',
subText: 'Review a list of past and future on-call shifts',
},
{
label: 'On-Call Notifications',
url: 'on-call-notifications',
subText: 'Set up notifications to know who is on-call',
},
]}
/>
</React.Fragment>
)
}