import React, { useState } from 'react'; import { X, Lock, Eye, EyeOff, AlertCircle } from 'lucide-react'; import { toast } from 'react-toastify'; import { useMutation } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { Button, Input, Card } from '../common'; import { adminService } from '../../services/admin.service'; interface PasswordChangeModalProps { isOpen: boolean; onClose: () => void; } export const PasswordChangeModal: React.FC = ({ isOpen, onClose }) => { const { t } = useTranslation(); const [formData, setFormData] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }); const [showPasswords, setShowPasswords] = useState({ current: false, new: false, confirm: false }); const [errors, setErrors] = useState>({}); const changePasswordMutation = useMutation({ mutationFn: adminService.changePassword, onSuccess: () => { toast.success(t('passwordChange.success')); onClose(); // Reset form setFormData({ currentPassword: '', newPassword: '', confirmPassword: '' }); setErrors({}); }, onError: (error: any) => { if (error.response?.data?.error) { toast.error(error.response.data.error); } else { toast.error(t('passwordChange.failed')); } } }); const validateForm = (): boolean => { const newErrors: Record = {}; if (!formData.currentPassword) { newErrors.currentPassword = t('passwordChange.currentRequired'); } if (!formData.newPassword) { newErrors.newPassword = t('passwordChange.newRequired'); } else if (formData.newPassword.length < 6) { newErrors.newPassword = t('passwordChange.minLengthError'); } if (!formData.confirmPassword) { newErrors.confirmPassword = t('passwordChange.confirmRequired'); } else if (formData.newPassword !== formData.confirmPassword) { newErrors.confirmPassword = t('passwordChange.noMatch'); } if (formData.currentPassword === formData.newPassword) { newErrors.newPassword = t('passwordChange.mustBeDifferent'); } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } changePasswordMutation.mutate({ currentPassword: formData.currentPassword, newPassword: formData.newPassword }); }; const handleInputChange = (field: keyof typeof formData) => (e: React.ChangeEvent) => { setFormData(prev => ({ ...prev, [field]: e.target.value })); // Clear error when user types if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })); } }; if (!isOpen) return null; return (

{t('passwordChange.title')}

{/* Current Password */}
} />
{/* New Password */}
} />
{/* Confirm Password */}
} />
{/* Password Requirements */}

{t('passwordChange.requirements')}

  • {t('passwordChange.minLength')}
  • {t('passwordChange.mustDiffer')}
{/* Action Buttons */}
); };