Changes in Change Password Popup improve its layout and functionality.

This commit is contained in:
Umesh Desai 2025-06-09 15:29:35 +05:30 committed by Vikas Nale
parent b4b64c4c91
commit 3a45f80f36

View File

@ -5,37 +5,53 @@ import { zodResolver } from "@hookform/resolvers/zod";
import showToast from "../../services/toastService"; import showToast from "../../services/toastService";
import AuthRepository from "../../repositories/AuthRepository"; import AuthRepository from "../../repositories/AuthRepository";
import "./page-auth.css"; import "./page-auth.css";
import { useSelector } from "react-redux";
const ChangePasswordSchema = z.object({ // --- Zod Schema for validation ---
email: z.string().email(), const ChangePasswordSchema = z
newPassword: z .object({
.string() newPassword: z
.min(8, "Password must be at least 8 characters") .string()
.regex(/[A-Z]/, "Must contain an uppercase letter") .min(8, "Password must be at least 8 characters")
.regex(/[a-z]/, "Must contain a lowercase letter") .regex(/[A-Z]/, "Must contain an uppercase letter")
.regex(/\d/, "Must contain a number") .regex(/[a-z]/, "Must contain a lowercase letter")
.regex(/[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/, "Must contain a special character"), .regex(/\d/, "Must contain a number")
oldPassword: z.string().min(1, "Old Password is required"), .regex(/[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/, "Must contain a special character"),
}); confirmPassword: z.string().min(1, "Confirm Password is required"),
oldPassword: z.string().min(1, "Old Password is required"),
})
.refine((data) => data.newPassword === data.confirmPassword, {
message: "New password and Confirm password do not match",
path: ["confirmPassword"],
});
const ChangePasswordPage = ({ onClose }) => { const ChangePasswordPage = ({ onClose }) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [hidepass, setHidepass] = useState(true); const [hideOldPass, setHideOldPass] = useState(true);
const [hidepass1, setHidepass1] = useState(true); const [hideNewPass, setHideNewPass] = useState(true);
const [hideConfirmPass, setHideConfirmPass] = useState(true);
const loggedUser = useSelector((store) => store.globalVariables.loginUser);
const { const {
register, register,
handleSubmit, handleSubmit,
formState: { errors }, formState: { errors },
reset, reset,
setError,
} = useForm({ } = useForm({
resolver: zodResolver(ChangePasswordSchema), resolver: zodResolver(ChangePasswordSchema),
}); });
const onChangePassword = async (data) => { const onChangePassword = async (data) => {
try { try {
let formData = {
email: loggedUser?.employeeInfo?.email,
oldPassword: data.oldPassword,
newPassword: data.newPassword,
};
console.log(formData);
setLoading(true); setLoading(true);
await AuthRepository.changepassword(data); await AuthRepository.changepassword(formData);
showToast("Your Password changed Successfully", "success"); showToast("Your Password changed Successfully", "success");
setLoading(false); setLoading(false);
reset(); reset();
@ -57,7 +73,7 @@ const ChangePasswordPage = ({ onClose }) => {
className="modal-dialog" className="modal-dialog"
role="document" role="document"
style={{ style={{
maxWidth: "400px", maxWidth: "600px",
width: "100%", width: "100%",
}} }}
> >
@ -73,159 +89,125 @@ const ChangePasswordPage = ({ onClose }) => {
<h5 className="mb-2">Change Password</h5> <h5 className="mb-2">Change Password</h5>
<p className="mb-4" style={{ fontSize: "14px" }}> <p className="mb-4" style={{ fontSize: "14px" }}>
Enter your email and old password to update. Enter old and new password to update.
</p> </p>
<form onSubmit={handleSubmit(onChangePassword)}> <form onSubmit={handleSubmit(onChangePassword)}>
{/* Old Password */}
<div className="mb-3"> <div className="mb-3">
<label htmlFor="email" className="form-label">
Email
</label>
<input
type="text"
className="form-control"
id="email"
{...register("email")}
placeholder="Enter your email"
/>
{errors.email && (
<div className="danger-text" style={{ fontSize: "12px" }}>
{errors.email.message}
</div>
)}
</div>
{/* <div className="mb-3">
<label className="form-label">Old Password</label> <label className="form-label">Old Password</label>
<div className="input-group"> <div className="input-group input-group-merge d-flex align-items-center border rounded px-2">
<input <input
type={hidepass ? "password" : "text"} type={hideOldPass ? "password" : "text"}
className="form-control" className="form-control form-control-sm border-0 shadow-none"
{...register("oldPassword")} {...register("oldPassword")}
placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;" placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
style={{ flex: 1 }}
/> />
<button <button
type="button" type="button"
className="btn btn-outline-secondary" className="btn btn-link p-0 ms-2"
onClick={() => setHidepass(!hidepass)} style={{ fontSize: "18px", color: "#6c757d" }}
onClick={() => setHideOldPass(!hideOldPass)}
> >
<i className={`bx ${hidepass ? "bx-hide" : "bx-show"}`} /> <i className={`bx ${hideOldPass ? "bx-hide" : "bx-show"}`} />
</button> </button>
</div> </div>
{errors.oldPassword && ( {errors.oldPassword && (
<p className="danger-text small-text text-start">{errors.oldPassword.message}</p> <p className="danger-text small-text text-start">
{errors.oldPassword.message}
</p>
)} )}
</div> </div>
<div className="mb-3"> <div className="row">
<label className="form-label">New Password</label> <div className="mb-3 col-md-6">
<div className="input-group"> <label className="form-label">New Password</label>
<input <div className="input-group input-group-merge d-flex align-items-center border rounded px-2">
type={hidepass1 ? "password" : "text"} <input
className="form-control" type={hideNewPass ? "password" : "text"}
{...register("newPassword")} className="form-control form-control-sm border-0 shadow-none"
placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;" {...register("newPassword")}
/> placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
<button style={{ flex: 1 }}
type="button" />
className="btn btn-outline-secondary" <button
onClick={() => setHidepass1(!hidepass1)} type="button"
> className="btn btn-link p-0 ms-2"
<i className={`bx ${hidepass1 ? "bx-hide" : "bx-show"}`} /> style={{ fontSize: "18px", color: "#6c757d" }}
</button> onClick={() => setHideNewPass(!hideNewPass)}
</div> >
{errors.newPassword && ( <i className={`bx ${hideNewPass ? "bx-hide" : "bx-show"}`} />
<div className="danger-text text-start" style={{ fontSize: "12px" }}> </button>
{errors.newPassword.message}
</div> </div>
)} {errors.newPassword && (
</div> */} <div className="danger-text text-start" style={{ fontSize: "12px" }}>
{errors.newPassword.message}
<div className="mb-3"> </div>
<label className="form-label">Old Password</label> )}
<div className="input-group" style={{ border: "1px solid #ced4da", borderRadius: "0.375rem", overflow: "hidden" }}>
<input
type={hidepass ? "password" : "text"}
className="form-control border-0"
{...register("oldPassword")}
placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
style={{ boxShadow: "none" }}
/>
<button
type="button"
className="btn btn-outline-light"
style={{
border: "none",
backgroundColor: "#fff",
borderLeft: "1px solid #ced4da",
}}
onClick={() => setHidepass(!hidepass)}
>
<i className={`bx ${hidepass ? "bx-hide" : "bx-show"}`} />
</button>
</div> </div>
{errors.oldPassword && (
<p className="danger-text small-text text-start">{errors.oldPassword.message}</p>
)}
</div>
<div className="mb-3"> {/* Confirm Password */}
<label className="form-label">New Password</label> <div className="mb-3 col-md-6">
<div className="input-group" style={{ border: "1px solid #ced4da", borderRadius: "0.375rem", overflow: "hidden" }}> <label className="form-label">Confirm New Password</label>
<input <div className="input-group input-group-merge d-flex align-items-center border rounded px-2">
type={hidepass1 ? "password" : "text"} <input
className="form-control border-0" type={hideConfirmPass ? "password" : "text"}
{...register("newPassword")} className="form-control form-control-sm border-0 shadow-none"
placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;" {...register("confirmPassword")}
style={{ boxShadow: "none" }} placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
/> style={{ flex: 1 }}
<button />
type="button" <button
className="btn btn-outline-light" type="button"
style={{ className="btn btn-link p-0 ms-2"
border: "none", style={{ fontSize: "18px", color: "#6c757d" }}
backgroundColor: "#fff", onClick={() => setHideConfirmPass(!hideConfirmPass)}
borderLeft: "1px solid #ced4da", >
}} <i className={`bx ${hideConfirmPass ? "bx-hide" : "bx-show"}`} />
onClick={() => setHidepass1(!hidepass1)} </button>
>
<i className={`bx ${hidepass1 ? "bx-hide" : "bx-show"}`} />
</button>
</div>
{errors.newPassword && (
<div className="danger-text text-start" style={{ fontSize: "12px" }}>
{errors.newPassword.message}
</div> </div>
)} {errors.confirmPassword && (
<div className="danger-text text-start" style={{ fontSize: "12px" }}>
{errors.confirmPassword.message}
</div>
)}
</div>
</div> </div>
{/* Action Buttons */}
<div className="mb-3 text-start "> <div className="d-flex justify-content-end">
<p className="p-0 m-0" style={{ fontSize: "9px" }}> <button type="submit" className="btn btn-primary btn-sm me-2" disabled={loading}>
Password must be at least 8 characters {loading ? "Please Wait..." : "Change Password"}
</p> </button>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
Password must contain at least one uppercase letter
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
Password must contain at least one number
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
Password must contain at least one special character
</p>
</div>
<div className="d-flex justify-content-between">
<button <button
type="button" type="button"
className="btn btn-outline-secondary btn-sm" className="btn btn-outline-secondary btn-sm"
onClick={onClose} onClick={onClose}
disabled={loading}
> >
Cancel Cancel
</button> </button>
<button type="submit" className="btn btn-primary btn-sm"> </div>
{loading ? "Please Wait..." : "Update Password"} <div className="mb-3 text-start ">
</button> <p className="p-0 m-0" style={{ fontSize: "9px" }}>
Password must :
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
- be at least 8 characters
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
- must contain at least one uppercase letter
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
- must contain at least one lowercase letter
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
- must contain at least one number
</p>
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
- must contain at least one special character
</p>
</div> </div>
</form> </form>
</div> </div>