Creating a Change Password popup. #189
42
src/components/Context/ChangePasswordContext.jsx
Normal file
42
src/components/Context/ChangePasswordContext.jsx
Normal file
@ -0,0 +1,42 @@
|
||||
import React, { createContext, useState, useContext } from "react";
|
||||
import ChangePasswordPage from "../../pages/authentication/ChangePassword";
|
||||
|
||||
const ChangePasswordContext = createContext();
|
||||
|
||||
export const ChangePasswordProvider = ({ children }) => {
|
||||
const [isChangePasswordOpen, setIsChangePasswordOpen] = useState(false);
|
||||
|
||||
const openChangePassword = () => setIsChangePasswordOpen(true);
|
||||
const closeChangePassword = () => setIsChangePasswordOpen(false);
|
||||
|
||||
return (
|
||||
<ChangePasswordContext.Provider
|
||||
value={{ isChangePasswordOpen, openChangePassword, closeChangePassword }}
|
||||
>
|
||||
{children}
|
||||
|
||||
{isChangePasswordOpen && (
|
||||
<>
|
||||
{/* This is the main Bootstrap modal container */}
|
||||
{/* It provides the fixed positioning and high z-index */}
|
||||
<div
|
||||
className="modal fade show" // 'fade' for animation, 'show' to make it visible
|
||||
style={{ display: 'block' }} // Explicitly set display: block for immediate visibility
|
||||
tabIndex="-1" // Makes the modal focusable
|
||||
role="dialog" // ARIA role for accessibility
|
||||
aria-labelledby="changePasswordModalLabel" // Link to a heading for accessibility
|
||||
aria-modal="true" // Indicate it's a modal dialog
|
||||
>
|
||||
{/* The ChangePasswordPage component itself contains the modal-dialog and modal-content */}
|
||||
<ChangePasswordPage onClose={closeChangePassword} />
|
||||
</div>
|
||||
|
||||
{/* The modal backdrop */}
|
||||
<div className="modal-backdrop fade show"></div>
|
||||
</>
|
||||
)}
|
||||
</ChangePasswordContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useChangePassword = () => useContext(ChangePasswordContext);
|
@ -7,6 +7,7 @@ import useMaster from "../../hooks/masterHook/useMaster";
|
||||
import { useProfile } from "../../hooks/useProfile";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import Avatar from "../../components/common/Avatar";
|
||||
import { useChangePassword } from "../Context/ChangePasswordContext";
|
||||
const Header = () => {
|
||||
const { profile } = useProfile();
|
||||
const dispatch = useDispatch(changeMaster("Job Role"));
|
||||
@ -56,9 +57,11 @@ const Header = () => {
|
||||
const handleProfilePage = () => {
|
||||
navigate(`/employee/${profile?.employeeInfo?.id}?for=attendance`);
|
||||
};
|
||||
const ChangePasswordPage = () => {
|
||||
navigate(`/auth/changepassword`);
|
||||
};
|
||||
// const ChangePasswordPage = () => {
|
||||
// navigate(`/auth/changepassword`);
|
||||
// };
|
||||
|
||||
const { openChangePassword } = useChangePassword();
|
||||
return (
|
||||
<nav
|
||||
className="layout-navbar container-xxl navbar navbar-expand-xl navbar-detached align-items-center bg-navbar-theme"
|
||||
@ -624,12 +627,12 @@ const Header = () => {
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
<li onClick={ChangePasswordPage}>
|
||||
<li onClick={openChangePassword}> {/* Use the function from the context */}
|
||||
<a
|
||||
aria-label="go to profile"
|
||||
className="dropdown-item cusor-pointer"
|
||||
>
|
||||
<i className="bx bx-user me-2"></i>
|
||||
<i className="bx bx-lock-alt me-2"></i>
|
||||
<span className="align-middle">Change Password</span>
|
||||
</a>
|
||||
</li>
|
||||
@ -654,4 +657,4 @@ const Header = () => {
|
||||
</nav>
|
||||
);
|
||||
};
|
||||
export default Header;
|
||||
export default Header;
|
@ -7,7 +7,7 @@ import App from './App.tsx'
|
||||
import { Provider } from 'react-redux';
|
||||
import { store } from './store/store';
|
||||
import { ModalProvider } from './ModalContext.jsx';
|
||||
|
||||
import { ChangePasswordProvider } from './components/Context/ChangePasswordContext.jsx';
|
||||
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
@ -15,10 +15,13 @@ createRoot(document.getElementById('root')!).render(
|
||||
// <MasterDataProvider>
|
||||
<Provider store={ store }>
|
||||
<ModalProvider>
|
||||
<ChangePasswordProvider >
|
||||
<App />
|
||||
</ChangePasswordProvider>
|
||||
</ModalProvider>
|
||||
</Provider>
|
||||
// </MasterDataProvider>
|
||||
|
||||
// </StrictMode>,
|
||||
)
|
||||
|
@ -1,201 +1,231 @@
|
||||
import { useState } from "react";
|
||||
import { Link, useSearchParams } from "react-router-dom";
|
||||
import "./page-auth.css";
|
||||
import { AuthWrapper } from "./AuthWrapper";
|
||||
import showToast from "../../services/toastService";
|
||||
import AuthRepository from "../../repositories/AuthRepository";
|
||||
import React, { useState } from "react";
|
||||
import { z } from "zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { clearAllCache } from "../../slices/apiDataManager";
|
||||
import showToast from "../../services/toastService";
|
||||
import AuthRepository from "../../repositories/AuthRepository";
|
||||
import "./page-auth.css";
|
||||
import { useSelector } from "react-redux";
|
||||
|
||||
// --- Zod Schema for validation ---
|
||||
const ChangePasswordSchema = z
|
||||
.object({
|
||||
email: z.string().email(),
|
||||
newPassword: z
|
||||
.string()
|
||||
.min(8, "Password must be at least 8 characters")
|
||||
.regex(/[A-Z]/, "Password must contain at least one uppercase letter")
|
||||
.regex(/[a-z]/, "Password must contain at least one lowercase letter")
|
||||
.regex(/\d/, "Password must contain at least one number")
|
||||
.regex(/[A-Z]/, "Must contain an uppercase letter")
|
||||
.regex(/[a-z]/, "Must contain a lowercase letter")
|
||||
.regex(/\d/, "Must contain a number")
|
||||
.regex(
|
||||
/[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/,
|
||||
"Password must contain at least one special character"
|
||||
"Must contain a special character"
|
||||
),
|
||||
oldPassword: z
|
||||
.string()
|
||||
.min(1, "old Password is required"),
|
||||
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 = () => {
|
||||
const ChangePasswordPage = ({ onClose }) => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [hidepass, setHidepass] = useState(true);
|
||||
const [hidepass1, setHidepass1] = useState(true);
|
||||
|
||||
const navigate = useNavigate();
|
||||
const [hideOldPass, setHideOldPass] = useState(true);
|
||||
const [hideNewPass, setHideNewPass] = useState(true);
|
||||
const [hideConfirmPass, setHideConfirmPass] = useState(true);
|
||||
const loggedUser = useSelector((store) => store.globalVariables.loginUser);
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
reset,
|
||||
setError,
|
||||
} = useForm({
|
||||
resolver: zodResolver(ChangePasswordSchema),
|
||||
});
|
||||
|
||||
const onChangePassword = async (data) => {
|
||||
try {
|
||||
let formData = {
|
||||
email: loggedUser?.employeeInfo?.email,
|
||||
oldPassword: data.oldPassword,
|
||||
newPassword: data.newPassword,
|
||||
};
|
||||
console.log(formData);
|
||||
setLoading(true);
|
||||
const { email, oldPassword, newPassword } = data;
|
||||
let response = await AuthRepository.changepassword(data);
|
||||
await AuthRepository.changepassword(formData);
|
||||
showToast("Your Password changed Successfully", "success");
|
||||
setLoading(false);
|
||||
navigate("/dashboard", { replace: true });
|
||||
reset();
|
||||
onClose();
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
showToast("Something went wrong", "error");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthWrapper>
|
||||
<h4 className="mb-2">Change Password</h4>
|
||||
<p className="mb-4">Enter your email and old password to update.</p>
|
||||
<form
|
||||
id="formAuthentication"
|
||||
className="mb-3"
|
||||
onSubmit={handleSubmit(onChangePassword)}
|
||||
<div
|
||||
className="modal d-flex align-items-center justify-content-center show"
|
||||
tabIndex="-1"
|
||||
role="dialog"
|
||||
style={{ display: "flex", backgroundColor: "rgba(0,0,0,0.5)" }}
|
||||
>
|
||||
<div
|
||||
className="modal-dialog"
|
||||
role="document"
|
||||
style={{
|
||||
maxWidth: "600px",
|
||||
width: "100%",
|
||||
}}
|
||||
>
|
||||
<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"
|
||||
autoFocus
|
||||
/>
|
||||
{errors.email && (
|
||||
<div
|
||||
className="danger-text text-start"
|
||||
style={{ fontSize: "12px" }}
|
||||
>
|
||||
{errors.email.message}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="modal-content p-4 rounded shadow bg-white position-relative">
|
||||
{/* Close Button */}
|
||||
<button
|
||||
type="button"
|
||||
className="btn-close position-absolute"
|
||||
style={{ top: "30px", right: "25px" }}
|
||||
aria-label="Close"
|
||||
onClick={onClose}
|
||||
></button>
|
||||
|
||||
<div className="mb-2 form-password-toggle">
|
||||
<div className="mt-2">
|
||||
<label htmlFor="email" className="form-label list-group-item">
|
||||
Old Password
|
||||
</label>
|
||||
</div>
|
||||
<div className=" input-group input-group-merge">
|
||||
<input
|
||||
type={hidepass ? "password" : "text"}
|
||||
autoComplete="true"
|
||||
id="password"
|
||||
className="form-control"
|
||||
<h5 className="mb-2">Change Password</h5>
|
||||
<p className="mb-4" style={{ fontSize: "14px" }}>
|
||||
Enter old and new password to update.
|
||||
</p>
|
||||
|
||||
{...register("oldPassword")}
|
||||
placeholder="············"
|
||||
/>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-outline-secondy border-top border-end border-bottom "
|
||||
onClick={() => setHidepass(!hidepass)}
|
||||
style={{
|
||||
borderTopLeftRadius: 0,
|
||||
borderBottomLeftRadius: 0,
|
||||
borderLeft: 0,
|
||||
|
||||
}}
|
||||
>
|
||||
{hidepass ? (
|
||||
<i className="bx bx-hide" />
|
||||
) : (
|
||||
<i className="bx bx-show" />
|
||||
<form onSubmit={handleSubmit(onChangePassword)}>
|
||||
{/* Old Password */}
|
||||
<div className="mb-3">
|
||||
<label className="form-label">Old Password</label>
|
||||
<div className="input-group input-group-merge d-flex align-items-center border rounded px-2">
|
||||
<input
|
||||
type={hideOldPass ? "password" : "text"}
|
||||
className="form-control form-control-sm border-0 shadow-none"
|
||||
{...register("oldPassword")}
|
||||
placeholder="············"
|
||||
style={{ flex: 1 }}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-link p-0 ms-2"
|
||||
style={{ fontSize: "18px", color: "#6c757d" }}
|
||||
onClick={() => setHideOldPass(!hideOldPass)}
|
||||
>
|
||||
<i className={`bx ${hideOldPass ? "bx-hide" : "bx-show"}`} />
|
||||
</button>
|
||||
</div>
|
||||
{errors.oldPassword && (
|
||||
<p className="danger-text small-text text-start">
|
||||
{errors.oldPassword.message}
|
||||
</p>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
{errors.oldPassword && <p className="small-text danger-text">{errors.oldPassword.message}</p>}
|
||||
|
||||
<div className="mt-2">
|
||||
{" "}
|
||||
<label htmlFor="email" className="form-label">
|
||||
New Password
|
||||
</label>{" "}
|
||||
</div>
|
||||
<div className=" input-group input-group-merge">
|
||||
<input
|
||||
type={hidepass1 ? "password" : "text"}
|
||||
autoComplete="true"
|
||||
id="password"
|
||||
className="form-control"
|
||||
{...register("newPassword")}
|
||||
placeholder="············"
|
||||
aria-describedby="password"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="btn border-top border-end border-bottom "
|
||||
onClick={() => setHidepass1(!hidepass1)}
|
||||
style={{
|
||||
borderTopLeftRadius: 0,
|
||||
borderBottomLeftRadius: 0,
|
||||
borderLeft: 0,
|
||||
|
||||
}}
|
||||
>
|
||||
{hidepass1 ? (
|
||||
<i className="bx bx-hide" />
|
||||
) : (
|
||||
<i className="bx bx-show" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
{errors.newPassword && (
|
||||
<div
|
||||
className="danger-text text-start"
|
||||
style={{ fontSize: "12px" }}
|
||||
>
|
||||
{errors.newPassword.message}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mb-3 text-start ">
|
||||
<p className="p-0 m-0" style={{ fontSize: "9px" }}>
|
||||
Password must be at least 8 characters
|
||||
</p>
|
||||
<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 className="row">
|
||||
<div className="mb-3 col-md-6">
|
||||
<label className="form-label">New Password</label>
|
||||
<div className="input-group input-group-merge d-flex align-items-center border rounded px-2">
|
||||
<input
|
||||
type={hideNewPass ? "password" : "text"}
|
||||
className="form-control form-control-sm border-0 shadow-none"
|
||||
{...register("newPassword")}
|
||||
placeholder="············"
|
||||
style={{ flex: 1 }}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-link p-0 ms-2"
|
||||
style={{ fontSize: "18px", color: "#6c757d" }}
|
||||
onClick={() => setHideNewPass(!hideNewPass)}
|
||||
>
|
||||
<i
|
||||
className={`bx ${hideNewPass ? "bx-hide" : "bx-show"}`}
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
{errors.newPassword && (
|
||||
<div
|
||||
className="danger-text text-start"
|
||||
style={{ fontSize: "12px" }}
|
||||
>
|
||||
{errors.newPassword.message}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Confirm Password */}
|
||||
<div className="mb-3 col-md-6">
|
||||
<label className="form-label">Confirm New Password</label>
|
||||
<div className="input-group input-group-merge d-flex align-items-center border rounded px-2">
|
||||
<input
|
||||
type={hideConfirmPass ? "password" : "text"}
|
||||
className="form-control form-control-sm border-0 shadow-none"
|
||||
{...register("confirmPassword")}
|
||||
placeholder="············"
|
||||
style={{ flex: 1 }}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-link p-0 ms-2"
|
||||
style={{ fontSize: "18px", color: "#6c757d" }}
|
||||
onClick={() => setHideConfirmPass(!hideConfirmPass)}
|
||||
>
|
||||
<i
|
||||
className={`bx ${
|
||||
hideConfirmPass ? "bx-hide" : "bx-show"
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
{errors.confirmPassword && (
|
||||
<div
|
||||
className="danger-text text-start"
|
||||
style={{ fontSize: "12px" }}
|
||||
>
|
||||
{errors.confirmPassword.message}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Action Buttons */}
|
||||
<div className="d-flex justify-content-end pt-2">
|
||||
<button
|
||||
type="submit"
|
||||
className="btn btn-primary btn-sm me-2"
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? "Please Wait..." : "Change Password"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-outline-secondary btn-sm"
|
||||
onClick={onClose}
|
||||
disabled={loading}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
<div className="mb-3 text-start ">
|
||||
<p className="p-0 m-0">Password must be:</p>
|
||||
<p className="p-0 m-0">- at least 8 characters long</p>
|
||||
<p className="p-0 m-0">
|
||||
- must contain at least one uppercase letter
|
||||
</p>
|
||||
<p className="p-0 m-0">
|
||||
- must contain at least one lowercase letter
|
||||
</p>
|
||||
<p className="p-0 m-0">- must contain at least one number</p>
|
||||
<p className="p-0 m-0">
|
||||
- must contain at least one special character
|
||||
</p>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<button aria-label="Click me" type="submit" className="btn btn-primary d-grid w-100">
|
||||
{loading ? "Please Wait..." : "Update Password"}
|
||||
</button>
|
||||
</form>
|
||||
<div className="text-center">
|
||||
<Link
|
||||
aria-label="Go to Login Page"
|
||||
to="/dashboard"
|
||||
className="d-flex align-items-center justify-content-center"
|
||||
>
|
||||
<i className="bx bx-chevron-left scaleX-n1-rtl bx-sm"></i>
|
||||
Back to home
|
||||
</Link>
|
||||
</div>
|
||||
</AuthWrapper>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user