-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathEditTicketForm.jsx
More file actions
180 lines (167 loc) · 4.31 KB
/
EditTicketForm.jsx
File metadata and controls
180 lines (167 loc) · 4.31 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
'use client';
import { useRouter } from 'next/navigation';
import React, { useState } from 'react';
const EditTicketForm = ({ ticket }) => {
const EDITMODE = ticket._id === 'new' ? false : true;
const router = useRouter();
const startingTicketData = {
title: '',
description: '',
priority: 1,
progress: 0,
status: 'not started',
category: 'Hardware Problem',
};
if (EDITMODE) {
for (let i in startingTicketData) {
startingTicketData[i] = ticket[i];
}
}
const [formData, setFormData] = useState(startingTicketData);
const handleChange = (e) => {
const value = e.target.value;
const name = e.target.name;
setFormData((preState) => ({
...preState,
[name]: value,
}));
};
const handleSubmit = async (e) => {
e.preventDefault();
let url = '/api/Tickets';
let method = 'POST';
if (EDITMODE) {
url += '/' + ticket._id;
method = 'PUT';
}
const res = await fetch(url, {
method: method,
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({ formData }),
});
if (!res.ok) {
throw new Error('Failed to update ticket');
}
router.refresh();
router.push('/');
};
const categories = [
'Hardware Problem',
'Software Problem',
'Application Deveopment',
'Project',
];
return (
<div className=" flex justify-center">
<form
onSubmit={handleSubmit}
method="post"
className="flex flex-col gap-3 w-1/2"
>
<h3>{EDITMODE ? 'Update Your Ticket' : 'Create New Ticket'}</h3>
<label>Title</label>
<input
id="title"
name="title"
type="text"
onChange={handleChange}
required={true}
value={formData.title}
/>
<label>Description</label>
<textarea
id="description"
name="description"
onChange={handleChange}
required={true}
value={formData.description}
rows="5"
/>
<label>Category</label>
<select
name="category"
value={formData.category}
onChange={handleChange}
>
{categories?.map((category, _index) => (
<option key={_index} value={category}>
{category}
</option>
))}
</select>
<label>Priority</label>
<div>
<input
id="priority-1"
name="priority"
type="radio"
onChange={handleChange}
value={1}
checked={formData.priority == 1}
/>
<label>1</label>
<input
id="priority-2"
name="priority"
type="radio"
onChange={handleChange}
value={2}
checked={formData.priority == 2}
/>
<label>2</label>
<input
id="priority-3"
name="priority"
type="radio"
onChange={handleChange}
value={3}
checked={formData.priority == 3}
/>
<label>3</label>
<input
id="priority-4"
name="priority"
type="radio"
onChange={handleChange}
value={4}
checked={formData.priority == 4}
/>
<label>4</label>
<input
id="priority-5"
name="priority"
type="radio"
onChange={handleChange}
value={5}
checked={formData.priority == 5}
/>
<label>5</label>
</div>
<label>Progress</label>
<input
type="range"
id="progress"
name="progress"
value={formData.progress}
min="0"
max="100"
onChange={handleChange}
/>
<label>Status</label>
<select name="status" value={formData.status} onChange={handleChange}>
<option value="not started">Not Started</option>
<option value="started">Started</option>
<option value="done">Done</option>
</select>
<input
type="submit"
className="btn max-w-xs"
value={EDITMODE ? 'Update Ticket' : 'Create Ticket'}
/>
</form>
</div>
);
};
export default EditTicketForm;