Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 8x 8x 8x 8x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 78x 13x 13x 13x 8x 1x 8x 1x | import React, { useState } from "react"; import { Button, DatePicker, Form, Input, Radio, Typography } from "antd"; import TypeSelector from "./components/TypeSelector"; import LocationSelector from "./components/LocationSelector"; import UsernameGenerator from "./components/UsernameGenerator"; import { UserAPICaller } from "../../../services/apis/user.api"; import { UserRequest } from "@/types/UserRequest"; import { useNavigate } from "react-router-dom"; import dayjs from "dayjs"; interface FormValues { firstName: string; lastName: string; dob: Date; gender: string; joinDate: Date; role: string; location?: number; } const CreateUser: React.FC = () => { const [form] = Form.useForm<FormValues>(); const [isStaticFieldVisible, setIsStaticFieldVisible] = useState(false); const [userType, setUserType] = useState<string>("USER"); const [, setUsername] = useState<string>(""); const [isButtonDisabled, setIsButtonDisabled] = useState(true); const navigate = useNavigate(); const onFinish = (values: FormValues) => { Iif (userType === "ADMIN" && !values.location) { values.location = 1; } const requestData: UserRequest = { firstName: values.firstName, lastName: values.lastName, dob: dayjs(values.dob).format("YYYY-MM-DD"), gender: values.gender, joinDate: dayjs(values.joinDate).format("YYYY-MM-DD"), role: values.role, locationId: values.location, }; UserAPICaller.createUser(requestData) .then((response) => { navigate("/admin/users", { state: { newUser: response.data.result } }); }) .catch((error) => { console.error("Error creating user:", error); }); }; const validateWhitespace = (_: unknown, value: string) => { Iif (!value) { return Promise.reject(new Error("Cannot be empty")); } else Iif (value.trim().length > 128) { return Promise.reject( new Error("Length must be less than 128 characters") ); } // Check if the value contains only letters, digits, or underscores else if (/^[a-zA-Z ]+$/.test(value)) { return Promise.resolve(); } return Promise.reject(new Error("Must not contains special chars")); }; const validatedob = (_: unknown, value: Date) => { Iif (!value) { return Promise.reject("Please select the date of birth!"); } const today = new Date(); const minDOB = new Date(); minDOB.setFullYear(today.getFullYear() - 18); Iif (value <= minDOB) { return Promise.resolve(); } return Promise.reject("Age must be at least 18 years old!"); }; const validateJoinDate = (_: unknown, value: Date) => { Iif (!value) { return Promise.reject(new Error("Please select the join date!")); } const dob = form.getFieldValue("dob"); Iif (dob && value < dob) { return Promise.reject( new Error("Join date must be after the Date of Birth!") ); } const date = new Date(value); const dayOfWeek = date.getDay(); Iif (dayOfWeek === 0 || dayOfWeek === 6) { return Promise.reject( new Error("Join date cannot be on Saturday or Sunday!") ); } return Promise.resolve(); }; const handleTypeChange = (value: string) => { setUserType(value); form.setFieldsValue({ role: value }); }; // Handlers const handleFieldsChange = () => { const fieldsValues = form.getFieldsValue(); const firstName = form.getFieldValue("firstName") || ""; const lastName = form.getFieldValue("lastName") || ""; const isFirstNameValid = firstName.trim() !== ""; const isLastNameValid = lastName.trim() !== ""; const isDobValid = fieldsValues.dob != null; const isGenderValid = fieldsValues.gender != null; const isJoinDateValid = fieldsValues.joinDate != null; const isRoleValid = fieldsValues.role != null && fieldsValues.role !== ""; const fieldsError = form.getFieldsError(); const hasErrors = fieldsError.some(field => field.errors.length > 0); setIsButtonDisabled( !( isFirstNameValid && isLastNameValid && isDobValid && isGenderValid && isJoinDateValid && isRoleValid && firstName.length <= 128 && lastName.length <= 128 && !hasErrors ) ); setUsername( isFirstNameValid && isLastNameValid ? `${firstName} ${lastName}` : "" ); setIsStaticFieldVisible(isFirstNameValid && isLastNameValid); }; const handleBackUserPage = () => { navigate("/admin/users"); }; return ( <Form form={form} labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} layout="horizontal" style={{ maxWidth: 500 }} colon={false} requiredMark={false} onFinish={onFinish} onFieldsChange={handleFieldsChange} > <Typography className="text-xl font-semibold text-red-500 font-serif pb-5"> Create New User </Typography> {/* First Name */} <Form.Item label="First Name" name="firstName" hasFeedback labelAlign="left" rules={[{ validator: validateWhitespace }]} > <Input data-testid="firstName" /> </Form.Item> {/* Last Name */} <Form.Item label="Last Name" name="lastName" hasFeedback labelAlign="left" rules={[{ validator: validateWhitespace }]} > <Input /> </Form.Item> {/* Static Field */} {isStaticFieldVisible && ( <UsernameGenerator label="Username" firstName={form.getFieldValue("firstName")} lastName={form.getFieldValue("lastName")} /> )} {/* Date of Birth */} <Form.Item label="Date of Birth" name="dob" hasFeedback rules={[{ validator: validatedob }]} labelAlign="left" > <DatePicker data-testid="dob" style={{ width: "100%" }} placeholder=" " /> </Form.Item> {/* Gender */} <Form.Item name="gender" label="Gender" labelAlign="left" data-testid="gender" rules={[{ required: true, message: "Please select a gender!" }]} > <Radio.Group className="flex flex-row"> <Radio data-testid="male-option" value="FEMALE"> Female </Radio> <Radio value="MALE">Male</Radio> </Radio.Group> </Form.Item> {/* Join Date */} <Form.Item label="Joined Date" name="joinDate" hasFeedback dependencies={['dob']} rules={[{ validator: validateJoinDate }]} labelAlign="left" > <DatePicker data-testid="joinDate" style={{ width: "100%" }} placeholder=" " /> </Form.Item> {/* Type */} <Form.Item aria-label="Type" initialValue="USER" label="Type" name="role" hasFeedback labelAlign="left" rules={[{ required: true, message: "Please select a type!" }]} > <TypeSelector value={userType} onChange={handleTypeChange} /> </Form.Item> {/* Location */} {userType === "ADMIN" && ( <Form.Item label="Location" name="location" hasFeedback labelAlign="left" > <LocationSelector /> </Form.Item> )} {/* Button */} <div className="button-container" style={{ display: "flex", justifyContent: "flex-end", gap: "20px" }} > <Form.Item> <Button type="primary" disabled={isButtonDisabled} danger htmlType="submit" > Save </Button> </Form.Item> <Form.Item> <Button onClick={handleBackUserPage}>Cancel</Button> </Form.Item> </div> </Form> ); }; export default CreateUser; |