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 | 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 3x 16x 11x 1x 1x 1x 11x 1x 1x 1x 1x 16x 42x 42x 42x 42x 42x 15x 15x 27x 6x 6x 21x 16x 3x 3x 16x 1x 1x 1x 16x 25x 5x 5x 5x 39x 12x 12x 2x 10x 8x 2x 1x | import { EInternalCode } from "@/enums/InternalCode.enum"; import { UserAPICaller } from "@/services/apis/user.api"; import APIResponse from "@/types/APIResponse"; import { ValidatorType, validatorPassword } from "@/utils/validator"; import { Button, Form, Input, Modal, message } from "antd"; import { useEffect, useState } from "react"; import { useMutation } from "react-query"; import { useNavigate } from "react-router-dom"; function ModalChangePassword({ isOpen, setIsOpenModal, }: { isOpen: boolean; setIsOpenModal: (isOpen: boolean) => void; }) { // state const [form] = Form.useForm(); const [isButtonDisabled, setIsButtonDisabled] = useState(true); const [isSuccessModalOpen, setIsSuccessModalOpen] = useState(false); const navigate = useNavigate(); // query const { mutate, isLoading, isError, error, isSuccess } = useMutation( (body: { password: string }) => UserAPICaller.changePassword(body) ); // effect useEffect(() => { if (isError) { const errorResponse = (error as { response: { data: APIResponse } }) .response.data; if (errorResponse.internalCode === EInternalCode.WRONG_PASSWORD) { form.setFields([ { name: "password", errors: ["Password is incorrect"], }, ]); } } if (isSuccess) { localStorage.removeItem("user"); localStorage.removeItem("token"); setIsOpenModal(false); setIsSuccessModalOpen(true); } }, [isError, isSuccess]); // handlers const handleFieldsChange = () => { const password = form.getFieldValue("password"); const validatorPass: ValidatorType = validatorPassword(password); const newPassword = form.getFieldValue("newPassword"); const validatorNewPass: ValidatorType = validatorPassword(newPassword); if (!validatorPass.isValid || !validatorNewPass.isValid) { setIsButtonDisabled(true); return; } if (password === newPassword) { setIsButtonDisabled(true); return; } setIsButtonDisabled(false); }; const handleOk = () => { const body = { password: form.getFieldValue("password"), newPassword: form.getFieldValue("newPassword"), }; mutate(body); }; const handleCancel = () => { form.resetFields(); setIsButtonDisabled(true); setIsOpenModal(false); }; return ( <> <Modal title={ <div> <p className="text-lg font-semibold primary-color"> Change Password </p> </div> } destroyOnClose={true} onOk={handleOk} open={isOpen} closable={false} okText="Save" footer={[ <Button key="save" onClick={handleOk} className="text-[#E9424D]" disabled={isButtonDisabled} danger loading={isLoading} type="primary" > Save </Button>, <Button key="cancel" onClick={handleCancel}> Cancel </Button>, ]} > <div className="flex flex-col text-sm"> <Form form={form} labelCol={{ span: 6 }} wrapperCol={{ span: 24 }} name="form_change_password" autoComplete="on" className="mt-4" onFinish={handleOk} onFieldsChange={handleFieldsChange} > <Form.Item label="Old Password" name="password" hasFeedback rules={[ () => ({ validator(_, value) { const validatorPass: ValidatorType = validatorPassword(value); if (validatorPass.isValid) { return Promise.resolve(); } else E{ return Promise.reject(new Error(validatorPass.message)); } }, }), ]} > <Input.Password /> </Form.Item> <Form.Item label="New Password" name="newPassword" dependencies={["password"]} hasFeedback rules={[ () => ({ validator(_, value) { const validatorPass: ValidatorType = validatorPassword(value); // check if new password is the same as old password --> reject if (value === form.getFieldValue("password")) { return Promise.reject( new Error( "New password must be different from old password" ) ); } if (validatorPass.isValid) { return Promise.resolve(); } return Promise.reject(new Error(validatorPass.message)); }, }), ]} > <Input.Password /> </Form.Item> </Form> </div> </Modal> <Modal title={ <div> <p className="text-lg font-semibold primary-color"> Change Password </p> </div> } maskClosable={false} closable={false} open={isSuccessModalOpen} footer={ <Button key="cancel" onClick={() => { message.success("Please login again to continue"); navigate("/login"); }} > Close </Button> } onCancel={() => setIsSuccessModalOpen(false)} > <p>Your password has been changed successfully</p> </Modal> </> ); } export default ModalChangePassword; |