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 | 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 3x 3x 3x 3x 2x 3x 2x 1x 1x 1x 3x 1x 1x 1x 1x 3x 2x 2x 3x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 2x 2x 2x 12x 2x 2x 1x | import React, { useEffect, useState } from "react"; import { Button, DatePicker, Form, Input, Radio, Typography, Select, message, } from "antd"; import { UserAPICaller } from "../../../services/apis/user.api"; import { UserUpdateRequest } from "@/types/UserRequest"; import { useNavigate, useParams } from "react-router-dom"; import { useQuery } from "react-query"; import dayjs from "dayjs"; const { Option } = Select; interface FormValues { firstName: string; lastName: string; dob: Date; gender: string; joinDate: Date; role: string; location?: number; } const EditUser: React.FC = () => { const [form] = Form.useForm<FormValues>(); const navigate = useNavigate(); const { id } = useParams<{ id: string }>(); const [isButtonDisabled, setIsButtonDisabled] = useState<boolean>(false); const [version, setVersion] = useState(""); const { data, isLoading, error } = useQuery(["user", id], () => UserAPICaller.getUserById(Number(id)) ); useEffect(() => { if (data) { const user = data.data.result; form.setFieldsValue({ firstName: user.firstName, lastName: user.lastName, dob: dayjs(user.dob), gender: user.gender, joinDate: dayjs(user.joinDate), role: user.type, }); setVersion(data.data.result.version); } }, [data, form]); const onFinish = (values: FormValues) => { const requestData: UserUpdateRequest = { dob: dayjs(values.dob).format("YYYY-MM-DD"), gender: values.gender, joinDate: dayjs(values.joinDate).format("YYYY-MM-DD"), type: values.role, version: Number(version), }; UserAPICaller.editUser(Number(id), requestData) .then((response) => { console.log(response.data.result); navigate("/admin/users", { state: { newUser: response.data.result }, }); }) .catch((error) => { message.error(error); }); }; const validateWhitespace = (_: unknown, value: string) => { Iif (!value) { return Promise.reject(new Error("Cannot be empty")); } return Promise.resolve(); }; 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); if (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("Please select the join date!"); } const dob = form.getFieldValue("dob"); Iif (dob && value < dob) { return Promise.reject("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("Join date cannot be on Saturday or Sunday!"); } return Promise.resolve(); }; const handleBackUserPage = () => { navigate("/admin/users"); }; if (isLoading) { return <div>Loading...</div>; } Iif (error) { return <div>Error loading user data</div>; } const handleFieldsChange = () => { const fieldsError = form.getFieldsError(); const hasErrors = fieldsError.some(field => field.errors.length > 0); setIsButtonDisabled(hasErrors); }; 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"> Edit User </Typography> {/* First Name */} <Form.Item label="First Name" name="firstName" hasFeedback labelAlign="left" rules={[{ validator: validateWhitespace }]} > <Input data-testid="firstName" disabled /> </Form.Item> {/* Last Name */} <Form.Item label="Last Name" name="lastName" hasFeedback labelAlign="left" rules={[{ validator: validateWhitespace }]} > <Input disabled /> </Form.Item> {/* 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="female-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!" }]} > <Select> <Option value="USER">User</Option> <Option value="ADMIN">Admin</Option> </Select> </Form.Item> {/* Button */} <div className="button-container" style={{ display: "flex", justifyContent: "flex-end", gap: "20px" }} > <Form.Item> <Button type="primary" danger htmlType="submit" disabled={isButtonDisabled} > Save </Button> </Form.Item> <Form.Item> <Button onClick={handleBackUserPage}>Cancel</Button> </Form.Item> </div> </Form> ); }; export default EditUser; |