// components/ChangePasswordModal.tsx import React, { useState, useEffect } from "react"; import { Typography, TextField, Button, Box } from "@mui/material"; import toast from "react-hot-toast"; interface Props { open: boolean; onClose: () => void; onSubmit: (passwordData: { currentPassword?: string; newPassword: string; }) => void; isUserChange?: boolean; } export const ChangePassword: React.FC = ({ open, onSubmit, isUserChange = false, }) => { const [currentPassword, setCurrentPassword] = useState(""); const [password, setPassword] = useState(""); const [confirm, setConfirm] = useState(""); const [errors, setErrors] = useState([]); const [isValid, setIsValid] = useState(false); // Validate password rules useEffect(() => { if (!password) { setErrors([]); setIsValid(false); return; } const newErrors: string[] = []; if (password.length < 8) newErrors.push("At least 8 characters"); if (!/[A-Z]/.test(password)) newErrors.push("One uppercase letter"); if (!/[a-z]/.test(password)) newErrors.push("One lowercase letter"); if (!/[0-9]/.test(password)) newErrors.push("One number"); if (!/[!@#$%^&*(),.?\":{}|<>]/.test(password)) newErrors.push("One special character"); if (password !== confirm) newErrors.push("Passwords do not match"); // If user change mode, require current password if (isUserChange && !currentPassword) newErrors.push("Current password required"); setErrors(newErrors); setIsValid(newErrors.length === 0); }, [confirm, password, currentPassword, isUserChange]); const handleSubmit = () => { if (!isValid) { toast.error("Please fix the validation errors before continuing"); return; } onSubmit({ ...(isUserChange ? { currentPassword } : {}), newPassword: password, }); }; return ( {isUserChange ? ( Please enter your current password and choose a new one below. ) : ( You’re currently logged in with a temporary password. Please set a new one to continue. )} {isUserChange && ( setCurrentPassword(e.target.value)} fullWidth error={isUserChange && !currentPassword} helperText={ isUserChange && !currentPassword ? "Current password is required" : "" } /> )} setPassword(e.target.value)} fullWidth error={!isValid && password.length > 0} /> setConfirm(e.target.value)} fullWidth error={confirm.length > 0 && password !== confirm} /> 0 ? "visible" : "hidden", }} >

{isValid ? "✓ Password meets all requirements" : "Password must contain:"}

{!isValid && ( {errors.join(", ")} )}
); };