-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseNewForm.ts
More file actions
272 lines (227 loc) · 6.49 KB
/
useNewForm.ts
File metadata and controls
272 lines (227 loc) · 6.49 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
import { useRequest } from "ahooks";
import { useEffect, useState } from "react";
import { useStatus } from "~/Services/Status";
import { Models } from "~/Services/Status.Models";
import { useAccessToken } from "../Auth/useAccessToken";
import { EventStatus, EventType, GetEventImpact, IsIncident } from "../Event/Enums";
import { useRouter } from "../Router";
/**
* Custom hook to manage the state and validation of a form.
*
* This hook provides various state variables and functions to handle
* form inputs, validation, and submission. It ensures that the form
* data is properly managed and validated before submission.
*
* @author Aloento
* @since 1.0.0
* @version 0.3.0
*/
export function useNewForm() {
const { DB, Update } = useStatus();
const [title, _setTitle] = useState("");
const [valTitle, setValTitle] = useState<string>();
function setTitle(value = title) {
let err: boolean = false;
if (value.length < 8 || value.length > 100) {
setValTitle("Title must be between 8 and 100 characters.");
err = true;
}
if (!value) {
setValTitle("Title is required.");
err = true;
}
_setTitle(value);
if (!err) {
setValTitle(undefined);
}
return !err;
}
const [type, _setType] = useState(EventType.Maintenance);
const [valType, setValType] = useState<string>();
function setType(value = type) {
if (!value) {
setValType("Type is required.");
return false;
}
if (!Object.values(EventType).includes(value)) {
setValType("Invalid event type.");
return false;
}
_setType(value);
setValType(undefined);
if (IsIncident(value)) {
_setEnd(undefined);
}
return true;
}
const [description, _setDescription] = useState("");
const [valDescription, setValDescription] = useState<string>();
function setDescription(value = description) {
let err: boolean = false;
if (value && (value.length < 10 || value.length > 500)) {
setValDescription("Description must be between 10 and 500 characters.");
err = true;
}
_setDescription(value);
if (!err) {
setValDescription(undefined);
}
return !err;
}
const [start, _setStart] = useState(new Date());
const [valStart, setValStart] = useState<string>();
function setStart(value = start) {
let err: boolean = false;
const now = new Date();
if (end && value > end) {
setValStart("Start Date cannot be later than End Date.");
err = true;
}
if (value > now && IsIncident(type)) {
setValStart("Start Date cannot be in the future.");
err = true;
}
if (!err) {
setValStart(undefined);
}
_setStart(value);
return !err;
}
const [end, _setEnd] = useState<Date>();
const [valEnd, setValEnd] = useState<string>();
function setEnd(value = end) {
let err: boolean = false;
if (value && value < start) {
setValEnd("End Date cannot be before Start Date.");
err = true;
}
if (!err) {
setValEnd(undefined);
}
_setEnd(value);
return !err;
}
useEffect(() => {
setStart();
setEnd();
}, [start, end]);
const [contactEmail, _setContactEmail] = useState("");
const [valContactEmail, setValContactEmail] = useState<string>();
function setContactEmail(value = contactEmail) {
let err: boolean = false;
if (type === EventType.Maintenance && !value) {
setValContactEmail("Contact Email is required for maintenance.");
err = true;
}
if (value && !value.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) {
setValContactEmail("Please enter a valid email address.");
err = true;
}
if (value && value.length > 100) {
setValContactEmail("Email must be less than 100 characters.");
err = true;
}
_setContactEmail(value);
!err && setValContactEmail(undefined);
return !err;
}
const [services, _setServices] = useState<Models.IRegionService[]>([]);
const [valServices, setValServices] = useState<string>();
function setServices(action: (curr: Models.IRegionService[]) => Models.IRegionService[] = (s) => s) {
const updated = action(services);
let err: boolean = false;
if (!updated || !updated.length) {
setValServices("At least one service is required.");
err = true;
}
_setServices(updated);
if (!err) {
setValServices(undefined);
}
return !err;
}
const { Nav } = useRouter();
const getToken = useAccessToken();
const { runAsync, loading } = useRequest(async () => {
if (![setTitle(), setType(), setDescription(), setStart(), setEnd(), setServices(), setContactEmail()].every(Boolean)) {
return;
}
const status = IsIncident(type)
? EventStatus.Detected : EventStatus.Planned
const event: Models.IEvent = {
Id: Math.max(...DB.Events.map(event => event.Id), 0) + 1,
Title: title,
Type: type,
Start: start,
End: end,
Status: status,
RegionServices: new Set(services),
Histories: new Set(),
Description: description
};
const url = process.env.SD_BACKEND_URL!;
const body: Record<string, any> = {
title,
description,
type: IsIncident(type) ? "incident" : type === EventType.Maintenance ? "maintenance" : "info",
impact: GetEventImpact(type),
components: services.map(s => s.Id),
start_date: start.toISOString()
}
if (type === EventType.Maintenance && contactEmail) {
body.contact_email = contactEmail;
}
if (!IsIncident(type) && end) {
body.end_date = end
}
const raw = await fetch(`${url}/v2/incidents`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${getToken()}`
},
body: JSON.stringify(body)
});
const res = await raw.json();
const id = res.result.at(0)?.incident_id;
if (id) {
event.Id = id;
}
DB.Events.push(event);
Update();
Nav(`/Event/${event.Id}`);
}, {
manual: true
});
return {
State: {
title,
type,
description,
start,
end,
services,
contactEmail
},
Actions: {
setTitle,
setType,
setDescription,
setStart,
setEnd,
setServices,
setContactEmail
},
Validation: {
title: valTitle,
type: valType,
description: valDescription,
start: valStart,
end: valEnd,
services: valServices,
contactEmail: valContactEmail
},
OnSubmit: runAsync,
Loading: loading
}
}