-
Notifications
You must be signed in to change notification settings - Fork 558
/
Copy pathCalendarEventsModal.tsx
314 lines (297 loc) · 8.97 KB
/
CalendarEventsModal.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import React, { useCallback, useState } from "react";
import { IPolicy } from "interfaces/policy";
import validURL from "components/forms/validators/valid_url";
import Button from "components/buttons/Button";
import RevealButton from "components/buttons/RevealButton";
import CustomLink from "components/CustomLink";
import Slider from "components/forms/fields/Slider";
// @ts-ignore
import InputField from "components/forms/fields/InputField";
import Graphic from "components/Graphic";
import Modal from "components/Modal";
import Checkbox from "components/forms/fields/Checkbox";
import { syntaxHighlight } from "utilities/helpers";
const baseClass = "calendar-events-modal";
interface IFormPolicy {
name: string;
id: number;
isChecked: boolean;
}
export interface ICalendarEventsFormData {
enabled: boolean;
url: string;
policies: IFormPolicy[];
}
interface ICalendarEventsModal {
onExit: () => void;
updatePolicyEnabledCalendarEvents: (
formData: ICalendarEventsFormData
) => void;
isUpdating: boolean;
configured: boolean;
enabled: boolean;
url: string;
policies: IPolicy[];
}
// allows any policy name to be the name of a form field, one of the checkboxes
type FormNames = string;
const CalendarEventsModal = ({
onExit,
updatePolicyEnabledCalendarEvents,
isUpdating,
configured,
enabled,
url,
policies,
}: ICalendarEventsModal) => {
const [formData, setFormData] = useState<ICalendarEventsFormData>({
enabled,
url,
policies: policies.map((policy) => ({
name: policy.name,
id: policy.id,
isChecked: policy.calendar_events_enabled || false,
})),
});
const [formErrors, setFormErrors] = useState<Record<string, string | null>>(
{}
);
const [showPreviewCalendarEvent, setShowPreviewCalendarEvent] = useState(
false
);
const [showExamplePayload, setShowExamplePayload] = useState(false);
const validateCalendarEventsFormData = (
curFormData: ICalendarEventsFormData
) => {
const errors: Record<string, string> = {};
if (curFormData.enabled) {
const { url: curUrl } = curFormData;
if (!validURL({ url: curUrl })) {
const errorPrefix = curUrl ? `${curUrl} is not` : "Please enter";
errors.url = `${errorPrefix} a valid resolution webhook URL`;
}
}
return errors;
};
// two onChange handlers to handle different levels of nesting in the form data
const onFeatureEnabledOrUrlChange = useCallback(
(newVal: { name: "enabled" | "url"; value: string | boolean }) => {
const { name, value } = newVal;
const newFormData = { ...formData, [name]: value };
setFormData(newFormData);
setFormErrors(validateCalendarEventsFormData(newFormData));
},
[formData]
);
const onPolicyEnabledChange = useCallback(
(newVal: { name: FormNames; value: boolean }) => {
const { name, value } = newVal;
const newFormPolicies = formData.policies.map((formPolicy) => {
if (formPolicy.name === name) {
return { ...formPolicy, isChecked: value };
}
return formPolicy;
});
const newFormData = { ...formData, policies: newFormPolicies };
setFormData(newFormData);
setFormErrors(validateCalendarEventsFormData(newFormData));
},
[formData]
);
const togglePreviewCalendarEvent = () => {
setShowPreviewCalendarEvent(!showPreviewCalendarEvent);
};
const renderExamplePayload = () => {
return (
<>
<pre>POST https://server.com/example</pre>
<pre
dangerouslySetInnerHTML={{
__html: syntaxHighlight({
timestamp: "0000-00-00T00:00:00Z",
host_id: 1,
host_display_name: "Anna's MacBook Pro",
host_serial_number: "ABCD1234567890",
failing_policies: [
{
id: 123,
name: "macOS - Disable guest account",
},
],
}),
}}
/>
</>
);
};
const renderPolicies = () => {
return (
<div className="form-field">
<div className="form-field__label">Policies:</div>
{formData.policies.map((policy) => {
const { isChecked, name, id } = policy;
return (
<div key={id}>
<Checkbox
value={isChecked}
name={name}
// can't use parseTarget as value needs to be set to !currentValue
onChange={() => {
onPolicyEnabledChange({ name, value: !isChecked });
}}
>
{name}
</Checkbox>
</div>
);
})}
<span className="form-field__help-text">
A calendar event will be created for end users if one of their hosts
fail any of these policies.{" "}
<CustomLink
url="https://www.fleetdm.com/learn-more-about/calendar-events"
text="Learn more"
newTab
/>
</span>
</div>
);
};
const renderPreviewCalendarEventModal = () => {
return (
<Modal
title="Calendar event preview"
width="large"
onExit={togglePreviewCalendarEvent}
className="calendar-event-preview"
>
<>
<p>A similar event will appear in the end user's calendar:</p>
<Graphic name="calendar-event-preview" />
<div className="modal-cta-wrap">
<Button onClick={togglePreviewCalendarEvent} variant="brand">
Done
</Button>
</div>
</>
</Modal>
);
};
const renderPlaceholderModal = () => {
return (
<div className="placeholder">
<a href="https://www.fleetdm.com/learn-more-about/calendar-events">
<Graphic name="calendar-event-preview" />
</a>
<div>
To create calendar events for end users if their hosts fail policies,
you must first connect Fleet to your Google Workspace service account.
</div>
<div>
This can be configured in{" "}
<b>Settings > Integrations > Calendars.</b>
</div>
<CustomLink
url="https://www.fleetdm.com/learn-more-about/calendar-events"
text="Learn more"
newTab
/>
<div className="modal-cta-wrap">
<Button onClick={onExit} variant="brand">
Done
</Button>
</div>
</div>
);
};
const renderConfiguredModal = () => (
<div className={`${baseClass} form`}>
<div className="form-header">
<Slider
value={formData.enabled}
onChange={() => {
onFeatureEnabledOrUrlChange({
name: "enabled",
value: !formData.enabled,
});
}}
inactiveText="Disabled"
activeText="Enabled"
/>
<Button
type="button"
variant="text-link"
onClick={togglePreviewCalendarEvent}
>
Preview calendar event
</Button>
</div>
<div
className={`form ${formData.enabled ? "" : "form-fields--disabled"}`}
>
<InputField
placeholder="https://server.com/example"
label="Resolution webhook URL"
onChange={onFeatureEnabledOrUrlChange}
name="url"
value={formData.url}
parseTarget
error={formErrors.url}
tooltip="Provide a URL to deliver a webhook request to."
labelTooltipPosition="top-start"
helpText="A request will be sent to this URL during the calendar event. Use it to trigger auto-remidiation."
/>
<RevealButton
isShowing={showExamplePayload}
className={`${baseClass}__show-example-payload-toggle`}
hideText="Hide example payload"
showText="Show example payload"
caretPosition="after"
onClick={() => {
setShowExamplePayload(!showExamplePayload);
}}
/>
{showExamplePayload && renderExamplePayload()}
{renderPolicies()}
</div>
<div className="modal-cta-wrap">
<Button
type="submit"
variant="brand"
onClick={() => {
updatePolicyEnabledCalendarEvents(formData);
}}
className="save-loading"
isLoading={isUpdating}
disabled={Object.keys(formErrors).length > 0}
>
Save
</Button>
<Button onClick={onExit} variant="inverse">
Cancel
</Button>
</div>
</div>
);
if (showPreviewCalendarEvent) {
return renderPreviewCalendarEventModal();
}
return (
<Modal
title="Calendar events"
onExit={onExit}
onEnter={
configured
? () => {
updatePolicyEnabledCalendarEvents(formData);
}
: onExit
}
className={baseClass}
width="large"
>
{configured ? renderConfiguredModal() : renderPlaceholderModal()}
</Modal>
);
};
export default CalendarEventsModal;