1
- import { useState } from 'react' ;
1
+ import { useState , useEffect } from 'react' ;
2
2
import { trpc } from '../../utils/trpc' ;
3
3
import { Select } from 'chakra-react-select' ;
4
4
import Router from 'next/router' ;
@@ -22,6 +22,7 @@ import { PersonalQueue, TicketType } from '@prisma/client';
22
22
import { STARTER_CONCEPTUAL_TICKET_DESCRIPTION , STARTER_DEBUGGING_TICKET_DESCRIPTION } from '../../utils/constants' ;
23
23
import { getTicketUrl , uppercaseFirstLetter } from '../../utils/utils' ;
24
24
import ConfirmPublicToggleModal from '../modals/ConfirmPublicToggleModal' ;
25
+ import { TicketWithNames } from '../../server/trpc/router/ticket' ;
25
26
26
27
interface Assignment {
27
28
id : number ;
@@ -38,22 +39,55 @@ interface Location {
38
39
interface CreateTicketFormProps {
39
40
arePublicTicketsEnabled : boolean ;
40
41
personalQueue ?: PersonalQueue ;
42
+ isEditingTicket ?: boolean ;
43
+ // Existing ticket is used to prepopulate the form when editing a ticket
44
+ existingTicket ?: TicketWithNames ;
45
+ setExistingTicket ?: ( ticket : TicketWithNames ) => void ;
41
46
}
42
47
43
48
const CreateTicketForm = ( props : CreateTicketFormProps ) => {
44
- const { arePublicTicketsEnabled, personalQueue } = props ;
45
- const [ ticketType , setTicketType ] = useState < TicketType > ( ) ;
46
- const [ description , setDescription ] = useState < string > ( '' ) ;
47
- const [ locationDescription , setLocationDescription ] = useState < string > ( '' ) ;
48
- const [ assignment , setAssignment ] = useState < Assignment > ( ) ;
49
+ const { arePublicTicketsEnabled, personalQueue, isEditingTicket, existingTicket, setExistingTicket } = props ;
50
+ const [ ticketType , setTicketType ] = useState < TicketType | undefined > ( existingTicket ?. ticketType ) ;
51
+ const [ description , setDescription ] = useState < string > ( existingTicket ?. description ?? '' ) ;
52
+ const [ locationDescription , setLocationDescription ] = useState < string > ( existingTicket ?. locationDescription ?? '' ) ;
49
53
const [ assignmentOptions , setAssignmentOptions ] = useState < Assignment [ ] > ( [ ] ) ;
50
54
const [ locationOptions , setLocationOptions ] = useState < Location [ ] > ( [ ] ) ;
51
55
const [ isPublicModalOpen , setIsPublicModalOpen ] = useState < boolean > ( false ) ;
52
- const [ location , setLocation ] = useState < Location > ( ) ;
53
- const [ isPublic , setIsPublic ] = useState < boolean > ( false ) ;
56
+ const [ isPublic , setIsPublic ] = useState < boolean > ( existingTicket ?. isPublic ?? false ) ;
54
57
const [ isButtonLoading , setIsButtonLoading ] = useState < boolean > ( false ) ;
58
+ const [ assignment , setAssignment ] = useState < Assignment | undefined > (
59
+ existingTicket
60
+ ? { id : existingTicket . assignmentId , label : existingTicket . assignmentName , value : existingTicket . assignmentName }
61
+ : undefined ,
62
+ ) ;
63
+ const [ location , setLocation ] = useState < Location | undefined > (
64
+ existingTicket
65
+ ? {
66
+ id : existingTicket . locationId ,
67
+ label : existingTicket . locationName ,
68
+ value : existingTicket . locationName ,
69
+ }
70
+ : undefined ,
71
+ ) ;
55
72
const toast = useToast ( ) ;
56
73
74
+ // When a property of the ticket changes, update the existing ticket if it exists
75
+ useEffect ( ( ) => {
76
+ if ( existingTicket && setExistingTicket ) {
77
+ setExistingTicket ( {
78
+ ...existingTicket ,
79
+ description,
80
+ locationDescription,
81
+ assignmentId : assignment ?. id ?? existingTicket . assignmentId ,
82
+ assignmentName : assignment ?. label ?? existingTicket . assignmentName ,
83
+ locationId : location ?. id ?? existingTicket . locationId ,
84
+ locationName : location ?. label ?? existingTicket . locationName ,
85
+ ticketType : ticketType ?? existingTicket . ticketType ,
86
+ isPublic,
87
+ } ) ;
88
+ }
89
+ } , [ description , locationDescription , assignment , location , ticketType , isPublic , existingTicket , setExistingTicket ] ) ;
90
+
57
91
const createTicketMutation = trpc . ticket . createTicket . useMutation ( ) ;
58
92
trpc . admin . getActiveAssignments . useQuery ( undefined , {
59
93
refetchOnWindowFocus : false ,
@@ -96,6 +130,11 @@ const CreateTicketForm = (props: CreateTicketFormProps) => {
96
130
97
131
const onSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
98
132
e . preventDefault ( ) ;
133
+ if ( isEditingTicket ) {
134
+ // Edit ticket has it's own submit handler
135
+ return ;
136
+ }
137
+
99
138
if ( ! assignment || ! location || ! ticketType ) {
100
139
toast ( {
101
140
title : 'Error' ,
@@ -232,7 +271,14 @@ const CreateTicketForm = (props: CreateTicketFormProps) => {
232
271
</ FormLabel >
233
272
< Switch isChecked = { isPublic } mt = { 1 } onChange = { handleTogglePublic } />
234
273
</ FormControl >
235
- < Button type = 'submit' width = 'full' mt = { 4 } colorScheme = 'whatsapp' isLoading = { isButtonLoading } >
274
+ < Button
275
+ hidden = { isEditingTicket }
276
+ type = 'submit'
277
+ width = 'full'
278
+ mt = { 4 }
279
+ colorScheme = 'whatsapp'
280
+ isLoading = { isButtonLoading }
281
+ >
236
282
Request Help
237
283
</ Button >
238
284
</ form >
0 commit comments