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 | 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 5x 4x 4x 4x 4x 4x 2x 4x 2x 1x 1x 1x 1x 4x 2x 2x 4x 4x 1x 1x 1x 4x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x | import { LoadingOutlined } from "@ant-design/icons"; import { Button, DatePicker, Form, Input, Spin, Typography, message, Radio, } from "antd"; import { useNavigate, useParams } from "react-router-dom"; import { useEffect, useState } from "react"; import { useMutation, useQuery } from "react-query"; import APIResponse from "@/types/APIResponse"; import dayjs from "dayjs"; import NotFoundPage from "@/components/404NotFound/NotFoundPage"; import { AssetAPICaller } from "@/services/apis/asset.api"; import { AssetEdit } from "@/types/Asset"; type CreateAssetBody = { name: string; specification: string; installDate: Date; state: string; version: number; }; function EditAsset() { // state const { id } = useParams<{ id: string }>(); const isValidId = (id: string) => { return !isNaN(Number(id)); }; if (!isValidId(id as string)) { return <NotFoundPage />; } // state const [isButtonDisabled, setIsButtonDisabled] = useState<boolean>(false); const [version, setVersion] = useState(-1); const [form] = Form.useForm(); const navigate = useNavigate(); // query const { mutate, data, isLoading, isError, error, isSuccess } = useMutation( AssetAPICaller.editAsset ); const { data: assetData, isSuccess: isGetAssetSuccess, isLoading: isGetAssetLoading, error: getAssetError, } = useQuery( ["getAsset", { id }], () => AssetAPICaller.getAsset(Number.parseInt(id ?? "0")), { retry: false } ); // useEffect useEffect(() => { if (isGetAssetSuccess && assetData) { const data = assetData?.data as APIResponse; const asset = data.result as AssetEdit; form.setFieldsValue({ name: asset.name, specification: asset.specification, installDate: dayjs(asset.installDate).valueOf(), category: asset.category, state: asset.state.toLowerCase(), version: asset.version, }); setVersion(asset.version); } }, [isGetAssetSuccess, assetData]); useEffect(() => { Iif (isError) { const errorResponse = (error as { response: { data: APIResponse } }) .response?.data; message.error(errorResponse.message); } Iif (isSuccess) { const newAsset: AssetEdit = data.data.result; navigate("/admin/assets", { state: { asset: newAsset, }, }); message.success("Update asset success"); } }, [isSuccess, isError]); const onFinish = (values: CreateAssetBody) => { const body = { name: values.name, specification: values.specification, installDate: dayjs(values.installDate).format("YYYY-MM-DD"), state: values.state, version: version, }; mutate({ assetId: Number.parseInt(id ?? "0"), body }); }; const handleFieldsChange = () => { const fields = form.getFieldsValue(); const { name, installDate, specification, state } = fields; setIsButtonDisabled( !name || !installDate || !specification || !state || name.length > 255 || specification.length > 1024 ); }; if (isGetAssetLoading) { return ( <div className="flex justify-center items-center"> <Spin size="large" indicator={<LoadingOutlined spin />} className="text-[#cf2338]" /> </div> ); } Iif (getAssetError) { return <div>asset Not Found</div>; } return ( <> <Typography className="text-xl font-semibold text-[#cf2338] font-serif pb-5"> Edit asset </Typography> <Form form={form} labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} layout="horizontal" style={{ maxWidth: 800 }} colon={false} requiredMark={false} onFinish={onFinish} onFieldsChange={handleFieldsChange} > <Form.Item label="Name" name="name" hasFeedback labelAlign="left" rules={[ { max: 255, message: "Must be less than 255 characters!" }, { required: true, message: "Please insert name" }, ]} > <Input name="name" /> </Form.Item> <Form.Item label="Category" name="category" hasFeedback labelAlign="left" > <Input name="category" readOnly disabled /> </Form.Item> <Form.Item label="Installed Date" name="installDate" hasFeedback labelAlign="left" getValueProps={(value) => ({ value: value && dayjs(Number(value)) })} rules={[{ required: true, message: "Please insert install date" }]} > <DatePicker type="date" data-testid="installDate" style={{ width: "100%" }} disabledDate={(current) => { const today = new Date(); const threeMonthsAgo = new Date(); threeMonthsAgo.setMonth(today.getMonth() - 3); threeMonthsAgo.setDate(today.getDate()); const currentDate = current.toDate(); return ( currentDate && currentDate <= threeMonthsAgo && currentDate < dayjs(assetData?.data.result.installDate).toDate() ); }} /> </Form.Item> <Form.Item name="specification" label="Specification" labelAlign="left" hasFeedback rules={[ { max: 1024, message: "Must be less than 1024 characters!" }, { required: true, message: "Please insert specification" }, ]} > <Input.TextArea rows={4} placeholder="Note" /> </Form.Item> <Form.Item name="state" labelAlign="left" label="State" hasFeedback> <Radio.Group className="flex flex-col" aria-label="State"> <Radio id="state" value="available" aria-label="Available"> Available </Radio> <Radio id="state" value="not_available" aria-label="Not Available"> Not Available </Radio> <Radio id="state" value="waiting_for_recycle" aria-label="Waiting For Recycle" > Waiting For Recycle </Radio> <Radio id="state" value="recycled" aria-label="Recycled"> Recycled </Radio> </Radio.Group> </Form.Item> <div className="button-container" style={{ display: "flex", justifyContent: "flex-end", gap: "20px" }} > <Form.Item> <Button type="primary" name="save" danger htmlType="submit" loading={isLoading} disabled={isButtonDisabled} > Save </Button> </Form.Item> <Form.Item label=""> <Button onClick={() => { navigate("/admin/assets"); }} > Cancel </Button> </Form.Item> </div> </Form> </> ); } export default EditAsset; |