import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { ChangePasswordCredentials } from '../types'; import { changePassword } from '../services/authService'; interface ChangePasswordFormProps { onSuccess?: () => void; onCancel?: () => void; } const ChangePasswordForm: React.FC = ({ onSuccess, onCancel }) => { const { t } = useTranslation(); const [formData, setFormData] = useState({ currentPassword: '', newPassword: '', }); const [confirmPassword, setConfirmPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; if (name === 'confirmPassword') { setConfirmPassword(value); } else { setFormData(prev => ({ ...prev, [name]: value })); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); // Validate passwords match if (formData.newPassword !== confirmPassword) { setError(t('auth.passwordsNotMatch')); return; } setIsLoading(true); try { const response = await changePassword(formData); if (response.success) { setSuccess(true); if (onSuccess) { onSuccess(); } } else { setError(response.message || t('auth.changePasswordError')); } } catch (err) { setError(t('auth.changePasswordError')); } finally { setIsLoading(false); } }; return (

{t('auth.changePassword')}

{success ? (
{t('auth.changePasswordSuccess')}
) : (
{error && (
{error}
)}
{onCancel && ( )}
)}
); }; export default ChangePasswordForm;