import React, { useState, useEffect } from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { X, Eye, EyeOff, Lock, Loader2, CheckCircle2, XCircle, AlertCircle, KeyRound } from 'lucide-react';
import useAuthStore from '../../store/useAuthStore';
import { resetPasswordSchema, ResetPasswordFormData } from '../../utils/validationSchemas';
import { useCompanySettings } from '../../contexts/CompanySettingsContext';
import { useAuthModal } from '../../contexts/AuthModalContext';
const PasswordRequirement: React.FC<{ met: boolean; text: string }> = ({ met, text }) => (
{met ? (
) : (
)}
{text}
);
interface ResetPasswordModalProps {
token: string;
}
const ResetPasswordModal: React.FC = ({ token }) => {
const { closeModal, openModal } = useAuthModal();
const { resetPassword, isLoading, error, clearError, isAuthenticated } = useAuthStore();
const { settings } = useCompanySettings();
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);
// Close modal on successful reset
useEffect(() => {
if (!isLoading && isAuthenticated) {
setTimeout(() => {
closeModal();
openModal('login');
}, 2000);
}
}, [isLoading, isAuthenticated, closeModal, openModal]);
const {
register,
handleSubmit,
watch,
formState: { errors },
} = useForm({
resolver: yupResolver(resetPasswordSchema),
defaultValues: {
password: '',
confirmPassword: '',
},
});
const password = watch('password');
const getPasswordStrength = (pwd: string) => {
if (!pwd) return { strength: 0, label: '', color: '' };
let strength = 0;
if (pwd.length >= 8) strength++;
if (/[a-z]/.test(pwd)) strength++;
if (/[A-Z]/.test(pwd)) strength++;
if (/\d/.test(pwd)) strength++;
if (/[@$!%*?&]/.test(pwd)) strength++;
const labels = [
{ label: 'Very Weak', color: 'bg-red-500' },
{ label: 'Weak', color: 'bg-orange-500' },
{ label: 'Medium', color: 'bg-yellow-500' },
{ label: 'Strong', color: 'bg-blue-500' },
{ label: 'Very Strong', color: 'bg-green-500' },
];
return { strength, ...labels[strength] };
};
const passwordStrength = getPasswordStrength(password || '');
const onSubmit = async (data: ResetPasswordFormData) => {
if (!token) {
return;
}
try {
clearError();
await resetPassword({
token,
password: data.password,
confirmPassword: data.confirmPassword,
});
setIsSuccess(true);
} catch (error) {
console.error('Reset password error:', error);
}
};
const isTokenError = error?.includes('token') || error?.includes('expired');
const isReuseError = error?.toLowerCase().includes('must be different') || error?.toLowerCase().includes('different from old');
// Handle escape key
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape' && !isSuccess) {
closeModal();
}
};
document.addEventListener('keydown', handleEscape);
return () => document.removeEventListener('keydown', handleEscape);
}, [closeModal, isSuccess]);
if (!token) {
return null;
}
return (
{
if (e.target === e.currentTarget && !isSuccess) {
closeModal();
}
}}
>
{/* Backdrop */}
{/* Modal */}
{/* Close button */}
{!isSuccess && (
)}
{/* Header */}
{settings.company_logo_url ? (
)
) : (
)}
{settings.company_tagline && (
{settings.company_tagline}
)}
{isSuccess ? 'Complete!' : 'Reset Password'}
{isSuccess
? 'Password has been reset successfully'
: `Enter a new password for your ${settings.company_name || 'Luxury Hotel'} account`}
{isSuccess ? (
Password reset successful!
Your password has been updated.
You can now login with your new password.
) : (
)}
);
};
export default ResetPasswordModal;