import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import { useTranslation } from 'react-i18next' import { z } from 'zod' import { toast } from '@/shared/ui/toast-store' import { Button } from '@/shared/ui/button' import { Input } from '@/shared/ui/input' import { Label } from '@/shared/ui/label' import { Card, CardContent, CardHeader, CardTitle } from '@/shared/ui/card' import { HttpError } from '@/shared/api/client' import { changePassword } from '@/features/auth/api' const schema = z.object({ currentPassword: z.string().min(1), newPassword: z .string() .min(8) .regex(/^(?=.*\p{Lu})(?=.*\d).+$/u), }) type FormValues = z.infer export function ChangePasswordForm() { const { t } = useTranslation() const { register, handleSubmit, reset, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(schema) }) const onSubmit = async (values: FormValues) => { try { await changePassword(values.currentPassword, values.newPassword) toast.success(t('settings.passwordChanged')) reset() } catch (error) { const message = error instanceof HttpError && error.status === 400 ? t('settings.currentPasswordInvalid') : t('auth.genericError') toast.error(message) } } return ( {t('settings.changePassword')}
{errors.currentPassword &&

{t('settings.currentPasswordRequired')}

}
{errors.newPassword &&

{t('auth.passwordHint')}

}
) }