import React, { useState } from 'react'; import { useAuth } from '../../contexts/AuthContext'; import './ChangePasswordOverlay.css'; function ChangePasswordOverlay({ onClose }) { const [oldPassword, setOldPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); const [isLoading, setIsLoading] = useState(false); const { changePassword } = useAuth(); const handleSubmit = async (e) => { e.preventDefault(); setError(''); // Validation if (newPassword !== confirmPassword) { setError('New passwords do not match'); return; } if (newPassword.length < 8) { setError('Password must be at least 8 characters'); return; } if (oldPassword === newPassword) { setError('New password must be different from old password'); return; } setIsLoading(true); const result = await changePassword(oldPassword, newPassword); if (result.success) { setSuccess(true); setTimeout(() => { onClose(); }, 2000); } else { // Extract error message let errorMsg = 'Failed to change password'; if (result.error.old_password) { errorMsg = result.error.old_password[0]; } else if (result.error.new_password) { errorMsg = result.error.new_password[0]; } else if (result.error.detail) { errorMsg = result.error.detail; } setError(errorMsg); setIsLoading(false); } }; const handleOverlayClick = (e) => { if (e.target.className === 'overlay') { onClose(); } }; return (
Password changed successfully!