|
| 1 | +import { useRouter } from "next/router"; |
| 2 | +import { useRef, useState } from "react"; |
| 3 | +import { useForm } from "react-hook-form"; |
| 4 | +import { toast, ToastContainer } from "react-toastify"; |
| 5 | + |
| 6 | +import styles from "../styles/form.module.css"; |
| 7 | + |
| 8 | +const endpoint = |
| 9 | + process.env.NODE_ENV === "production" ? `` : "http://localhost:3000"; |
| 10 | + |
| 11 | +const PasswordChangeForm = () => { |
| 12 | + const { |
| 13 | + register, |
| 14 | + handleSubmit, |
| 15 | + watch, |
| 16 | + formState: { errors }, |
| 17 | + } = useForm(); |
| 18 | + const router = useRouter(); |
| 19 | + |
| 20 | + const [loading, setloading] = useState(false); |
| 21 | + |
| 22 | + const password = useRef({}); |
| 23 | + password.current = watch("newPassword", ""); |
| 24 | + |
| 25 | + const updatePassword = async (data) => { |
| 26 | + console.log(data); |
| 27 | + setloading(true); |
| 28 | + |
| 29 | + // let payload = {}; |
| 30 | + // currentpassword: "123" |
| 31 | + // |
| 32 | + // newpassword: "D6taSRDazH9giCG" |
| 33 | + // return; |
| 34 | + |
| 35 | + try { |
| 36 | + let res = await fetch(`${endpoint}/api/changepassword`, { |
| 37 | + method: "POST", |
| 38 | + body: JSON.stringify(data), |
| 39 | + headers: { "Content-Type": "application/json" }, |
| 40 | + }).then((res) => res.json()); |
| 41 | + |
| 42 | + if (!res.success) { |
| 43 | + toast.error(`Error : ${res.msg}`, { |
| 44 | + position: "bottom-left", |
| 45 | + autoClose: 5000, |
| 46 | + hideProgressBar: false, |
| 47 | + closeOnClick: true, |
| 48 | + pauseOnHover: true, |
| 49 | + draggable: true, |
| 50 | + progress: undefined, |
| 51 | + }); |
| 52 | + setloading(false); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + toast.success(`successfully updated password`, { |
| 57 | + position: "bottom-left", |
| 58 | + autoClose: 5000, |
| 59 | + hideProgressBar: false, |
| 60 | + closeOnClick: true, |
| 61 | + pauseOnHover: true, |
| 62 | + draggable: true, |
| 63 | + progress: undefined, |
| 64 | + }); |
| 65 | + await logout(); |
| 66 | + } catch (error) { |
| 67 | + console.log(error); |
| 68 | + toast.error(`Error : ${error.message}`, { |
| 69 | + position: "bottom-left", |
| 70 | + autoClose: 5000, |
| 71 | + hideProgressBar: false, |
| 72 | + closeOnClick: true, |
| 73 | + pauseOnHover: true, |
| 74 | + draggable: true, |
| 75 | + progress: undefined, |
| 76 | + }); |
| 77 | + } |
| 78 | + setloading(false); |
| 79 | + }; |
| 80 | + |
| 81 | + const logout = async () => { |
| 82 | + try { |
| 83 | + let res = await fetch(`${endpoint}/api/logout`).then((res) => res.json()); |
| 84 | + console.log(res); |
| 85 | + |
| 86 | + if (res.success) { |
| 87 | + router.push("/admin"); |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + toast.error(`Logout Error : ${error.message}`, { |
| 91 | + position: "bottom-left", |
| 92 | + autoClose: 5000, |
| 93 | + hideProgressBar: false, |
| 94 | + closeOnClick: true, |
| 95 | + pauseOnHover: true, |
| 96 | + draggable: true, |
| 97 | + progress: undefined, |
| 98 | + }); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + return ( |
| 103 | + <> |
| 104 | + <div className={styles.Wrapper}> |
| 105 | + <div |
| 106 | + className={`${styles.Inner} col-10 col-sm-10 col-md-10 col-lg-10 col-xl-8 col-xxl-8 `} |
| 107 | + > |
| 108 | + <form onSubmit={(e) => e.preventDefault()}> |
| 109 | + <h3>Updated Password</h3> |
| 110 | + <div className="mb-3 "> |
| 111 | + <label className="form-label">Current Password</label> |
| 112 | + <input |
| 113 | + type="password" |
| 114 | + className={ |
| 115 | + errors.currentPassword |
| 116 | + ? "form-control is-invalid" |
| 117 | + : "form-control" |
| 118 | + } |
| 119 | + placeholder="Enter Current Password" |
| 120 | + {...register("currentPassword")} |
| 121 | + /> |
| 122 | + {errors.currentPassword && ( |
| 123 | + <div className="invalid-feedback"> |
| 124 | + {errors.currentPassword.message} |
| 125 | + </div> |
| 126 | + )} |
| 127 | + </div> |
| 128 | + <div className="mb-3 "> |
| 129 | + <label className="form-label">New Password</label> |
| 130 | + <input |
| 131 | + type="password" |
| 132 | + className={ |
| 133 | + errors.newPassword |
| 134 | + ? "form-control is-invalid" |
| 135 | + : "form-control" |
| 136 | + } |
| 137 | + placeholder="Enter New Password" |
| 138 | + {...register("newPassword", { |
| 139 | + required: "You must specify a password", |
| 140 | + minLength: { |
| 141 | + value: 6, |
| 142 | + message: "Password must have at least 6 characters", |
| 143 | + }, |
| 144 | + })} |
| 145 | + /> |
| 146 | + {errors.newPassword && ( |
| 147 | + <div className="invalid-feedback"> |
| 148 | + {errors.newPassword.message} |
| 149 | + </div> |
| 150 | + )} |
| 151 | + </div> |
| 152 | + <div className="mb-3 "> |
| 153 | + <label className="form-label">Confirm Password</label> |
| 154 | + <input |
| 155 | + type="password" |
| 156 | + className={ |
| 157 | + errors.confirm_password |
| 158 | + ? "form-control is-invalid" |
| 159 | + : "form-control" |
| 160 | + } |
| 161 | + placeholder="Enter New Password" |
| 162 | + {...register("confirm_password", { |
| 163 | + validate: (value) => { |
| 164 | + return ( |
| 165 | + value === password.current || "The passwords do not match" |
| 166 | + ); |
| 167 | + }, |
| 168 | + })} |
| 169 | + />{" "} |
| 170 | + {errors.confirm_password && ( |
| 171 | + <div className="invalid-feedback"> |
| 172 | + {errors.confirm_password.message} |
| 173 | + </div> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + <button |
| 177 | + type="submit" |
| 178 | + className="btn btn-primary btn-block" |
| 179 | + onClick={handleSubmit(updatePassword)} |
| 180 | + disabled={loading} |
| 181 | + > |
| 182 | + {loading && ( |
| 183 | + <span |
| 184 | + className="spinner-border spinner-border-sm me-1" |
| 185 | + role="status" |
| 186 | + aria-hidden="true" |
| 187 | + ></span> |
| 188 | + )} |
| 189 | + Change Password |
| 190 | + </button> |
| 191 | + </form> |
| 192 | + <ToastContainer |
| 193 | + position="bottom-left" |
| 194 | + autoClose={5000} |
| 195 | + hideProgressBar={true} |
| 196 | + newestOnTop={false} |
| 197 | + closeOnClick |
| 198 | + rtl={false} |
| 199 | + pauseOnFocusLoss |
| 200 | + draggable |
| 201 | + pauseOnHover |
| 202 | + /> |
| 203 | + </div> |
| 204 | + </div> |
| 205 | + </> |
| 206 | + ); |
| 207 | +}; |
| 208 | +export default PasswordChangeForm; |
0 commit comments