-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCreateTicketForm.tsx
More file actions
executable file
·473 lines (440 loc) · 13.7 KB
/
CreateTicketForm.tsx
File metadata and controls
executable file
·473 lines (440 loc) · 13.7 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import { InfoIcon } from "@chakra-ui/icons";
import {
Box,
Button,
Flex,
FormControl,
FormLabel,
HStack,
Input,
Radio,
RadioGroup,
Switch,
Text,
Textarea,
Tooltip,
useToast,
} from "@chakra-ui/react";
import { Category, PersonalQueue, TicketType } from "@prisma/client";
import { Select, SingleValue } from "chakra-react-select";
import Router from "next/router";
import { useEffect, useState } from "react";
import { TicketWithNames } from "../../server/trpc/router/ticket";
import {
STARTER_CONCEPTUAL_TICKET_DESCRIPTION,
STARTER_DEBUGGING_TICKET_DESCRIPTION,
} from "../../utils/constants";
import { trpc } from "../../utils/trpc";
import {
getTicketUrl,
joinArrayAsString,
uppercaseFirstLetter,
} from "../../utils/utils";
import ConfirmPublicToggleModal from "../modals/ConfirmPublicToggleModal";
interface Assignment {
id: number;
label: string;
value: string;
categoryId?: number | undefined;
template: string;
}
interface Location {
id: number;
label: string;
value: string;
online: boolean;
}
interface CreateTicketFormProps {
arePublicTicketsEnabled: boolean;
personalQueue?: PersonalQueue;
isEditingTicket?: boolean;
// Existing ticket is used to prepopulate the form when editing a ticket
existingTicket?: TicketWithNames;
setExistingTicket?: (ticket: TicketWithNames) => void;
}
const CreateTicketForm = (props: CreateTicketFormProps) => {
const {
arePublicTicketsEnabled,
personalQueue,
isEditingTicket,
existingTicket,
setExistingTicket,
} = props;
const [ticketType, setTicketType] = useState<TicketType | undefined>(
existingTicket?.ticketType,
);
const [description, setDescription] = useState<string>(
existingTicket?.description ?? "",
);
const [locationDescription, setLocationDescription] = useState<string>(
existingTicket?.locationDescription ?? "",
);
const [assignmentOptions, setAssignmentOptions] = useState<Assignment[]>([]);
const [locationOptions, setLocationOptions] = useState<Location[]>([]);
const [isPublicModalOpen, setIsPublicModalOpen] = useState<boolean>(false);
const [isPublic, setIsPublic] = useState<boolean>(
existingTicket?.isPublic ?? false,
);
const [isButtonLoading, setIsButtonLoading] = useState<boolean>(false);
const [assignment, setAssignment] = useState<Assignment | undefined>(
existingTicket
? {
id: existingTicket.assignmentId,
label: existingTicket.assignmentName,
value: existingTicket.assignmentName,
categoryId: existingTicket.assignmentCategoryId,
template: existingTicket.template,
}
: undefined,
);
const [location, setLocation] = useState<Location | undefined>(
existingTicket
? {
id: existingTicket.locationId,
label: existingTicket.locationName,
value: existingTicket.locationName,
online: existingTicket.isOnline,
}
: undefined,
);
const [allCategories, setAllCategories] = useState<Category[]>();
const toast = useToast();
// When a property of the ticket changes, update the existing ticket if it exists
useEffect(() => {
if (existingTicket && setExistingTicket) {
setExistingTicket({
...existingTicket,
description,
locationDescription,
assignmentId: assignment?.id ?? existingTicket.assignmentId,
assignmentName: assignment?.label ?? existingTicket.assignmentName,
locationId: location?.id ?? existingTicket.locationId,
locationName: location?.label ?? existingTicket.locationName,
ticketType: ticketType ?? existingTicket.ticketType,
isPublic,
});
}
}, [
description,
locationDescription,
assignment,
location,
ticketType,
isPublic,
existingTicket,
setExistingTicket,
]);
const createTicketMutation = trpc.ticket.createTicket.useMutation();
trpc.admin.getActiveAssignments.useQuery(
{},
{
refetchOnWindowFocus: false,
onSuccess: (data) => {
setAssignmentOptions(
data.map(
(assignment) =>
({
label: assignment.name,
value: assignment.name,
id: assignment.id,
categoryId: assignment.categoryId,
template: assignment.template,
}) as Assignment,
),
);
},
},
);
const { refetch } = trpc.admin.getActiveLocations.useQuery(
{ categoryId: assignment?.categoryId },
{
refetchOnWindowFocus: false,
onSuccess: (data) => {
setLocationOptions(
data.map(
(location) =>
({
label: location.name,
value: location.name,
id: location.id,
online: location.isOnline,
}) as Location,
),
);
},
},
);
trpc.admin.getAllCategories.useQuery(undefined, {
refetchOnWindowFocus: false,
onSuccess: (data) => {
setAllCategories(data);
},
});
const { data: cooldownPeriod } = trpc.admin.getCoolDownTime.useQuery(
undefined,
{
refetchOnWindowFocus: false,
},
);
const handleTicketTypeChange = (newVal: TicketType) => {
setTicketType(newVal);
if (newVal === TicketType.DEBUGGING) {
setIsPublic(false);
} else {
if (arePublicTicketsEnabled) {
setIsPublic(true);
}
}
changeDescription(assignment, newVal);
};
const changeDescription = (assignment: Assignment | undefined, ticketType: TicketType | undefined) => {
if (assignment === undefined || assignment?.template === "") {
if (ticketType == TicketType.DEBUGGING) {
setDescription(STARTER_DEBUGGING_TICKET_DESCRIPTION);
} else {
setDescription(STARTER_CONCEPTUAL_TICKET_DESCRIPTION);
}
} else {
setDescription(assignment.template);
}
}
const handleTogglePublic = () => {
if (ticketType === TicketType.CONCEPTUAL && isPublic) {
setIsPublicModalOpen(true);
return;
}
setIsPublic(!isPublic);
};
const handleAssignmentChange = (newVal: SingleValue<Assignment>) => {
setLocation(undefined); // todo look at this
setAssignment(newVal ?? undefined);
refetch();
changeDescription(newVal ?? undefined, ticketType ?? undefined);
};
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (isEditingTicket) {
// Edit ticket has it's own submit handler
return;
}
if (!assignment || !location || !ticketType) {
toast({
title: "Error",
description: "Please select an assignment and location",
status: "error",
position: "top-right",
duration: 3000,
isClosable: true,
});
return;
}
// If description has "[this test]" or "[this concept]" in it, toast and return
if (
description.includes("[this test]") ||
description.includes("[this concept]")
) {
toast({
title: "Error",
description:
"Please replace [this concept] or [this test] with the the specific concept or test",
status: "error",
position: "top-right",
duration: 3000,
isClosable: true,
});
return;
}
// Prevents spamming the button
setIsButtonLoading(true);
await createTicketMutation
.mutateAsync({
description: description.trim(),
assignmentId: assignment.id,
locationId: location.id,
locationDescription: locationDescription.trim(),
personalQueueName: personalQueue?.name,
ticketType,
isPublic,
})
.then((ticket) => {
if (!ticket) {
const coolDownText = cooldownPeriod
? `You must wait ${cooldownPeriod} minutes since your last ticket was resolved.`
: "";
toast({
title: "Error",
description: `Could not create ticket. You may already have a ticket open. If not, refresh and try again. ${coolDownText}`,
status: "error",
position: "top-right",
duration: 10000,
isClosable: true,
});
return;
}
setDescription("");
// Resets the select options
setAssignment("" as unknown as Assignment);
setLocation("" as unknown as Location);
toast({
title: "Ticket created",
description: "Your help request has been created",
status: "success",
duration: 3000,
isClosable: true,
position: "top-right",
});
Router.push(getTicketUrl(ticket.id));
});
setIsButtonLoading(false);
};
const getCategoryTooltipText = (
categories: Category[],
assignment: Assignment,
) => {
const category = categories.find(
(category) => category.id === assignment.categoryId,
);
if (category === undefined) {
return "This assignment can be taken in any room.";
}
if (locationOptions.length === 0) {
return `There are no rooms that can take ${category.name} tickets.`;
}
return `${category.name} tickets are limited to
${joinArrayAsString(
locationOptions.map((locationOption) => locationOption.value),
)}.`;
};
return (
<Box
p={8}
pt={2}
width="full"
borderWidth={1}
borderRadius={8}
boxShadow="lg"
mt={5}
>
<Box my={4} textAlign="left">
<form onSubmit={onSubmit}>
<FormControl isRequired>
<Flex>
<FormLabel>Ticket Type</FormLabel>
<RadioGroup onChange={handleTicketTypeChange} value={ticketType}>
{Object.keys(TicketType).map((type) => (
<Radio mr={2} key={type} value={type}>
{uppercaseFirstLetter(type)}
</Radio>
))}
</RadioGroup>
</Flex>
<Text hidden={ticketType !== TicketType.CONCEPTUAL} mb={2}>
For conceptual questions, staff will not look at code or help with debugging.
</Text>
</FormControl>
<FormControl mt={2} isRequired isDisabled={ticketType === undefined}>
<FormLabel>Assignment</FormLabel>
<Select
value={assignment}
onChange={handleAssignmentChange}
options={assignmentOptions}
/>
</FormControl>
<FormControl mt={2} isRequired isDisabled={ticketType === undefined || assignment === undefined}>
<FormLabel>Description</FormLabel>
<Textarea
value={description}
onChange={(e) => setDescription(e.target.value)}
name="description"
size="md"
maxLength={1000}
/>
</FormControl>
<FormControl mt={6} isRequired isDisabled={assignment === undefined}>
<HStack>
<FormLabel margin={0}>Location</FormLabel>
{allCategories !== undefined && assignment !== undefined && (
<Tooltip
hidden={allCategories.length === 0}
hasArrow
label={getCategoryTooltipText(allCategories, assignment)}
bg="gray.300"
color="black"
>
<InfoIcon mr={1} mb={1} />
</Tooltip>
)}
<Box width="30%">
<Select
value={location === undefined ? null : location}
onChange={(val) => setLocation(val ?? undefined)}
options={locationOptions}
/>
</Box>
</HStack>
</FormControl>
<FormControl
mt={6}
isRequired
isDisabled={location === undefined}
>
<FormLabel>Briefly describe where you are</FormLabel>
<Input
value={locationDescription}
onChange={e => {
setLocationDescription(e.target.value);
}}
type="text"
// TODO: make the location options customizable per location
placeholder={location?.online ? "Breakout Room 10" : "Back right corner of the room"}
name="locationDescription"
maxLength={140}
/>
</FormControl>
<FormControl
mt={6}
display="flex"
hidden={!arePublicTicketsEnabled}
isDisabled={
ticketType === TicketType.DEBUGGING || ticketType === undefined
}
>
<FormLabel>
Public
<Tooltip
hasArrow
label="Public tickets can be joined by other students. This is great for group work
or conceptual questions! If your ticket is public, we are more likely to
help you for a longer time."
bg="gray.300"
color="black"
>
<InfoIcon ml={2} mb={1} />
</Tooltip>
</FormLabel>
<Switch isChecked={isPublic} mt={1} onChange={handleTogglePublic} />
</FormControl>
<Button
hidden={isEditingTicket}
type="submit"
width="full"
mt={4}
colorScheme="whatsapp"
isLoading={isButtonLoading}
>
Request Help
</Button>
</form>
</Box>
<ConfirmPublicToggleModal
isModalOpen={isPublicModalOpen}
setIsModalOpen={setIsPublicModalOpen}
handleConfirm={() => {
setIsPublicModalOpen(false);
setIsPublic(false);
}}
/>
</Box>
);
};
export default CreateTicketForm;