From 8d10aa77c8cc9c27c396f768f93f8d1360bb455b Mon Sep 17 00:00:00 2001 From: smanylov Date: Tue, 3 Feb 2026 11:44:02 +0700 Subject: [PATCH] add errors handlers and translate --- src/app/actions/auth.ts | 28 +++++++++++++++-------- src/app/ui/forms/forgot-password-form.tsx | 16 ++++--------- src/app/ui/forms/reset-password-form.tsx | 6 ++--- src/i18n/messages/en.json | 4 +++- src/i18n/messages/ru.json | 4 +++- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts index 39cfbcd..fd1b6e4 100644 --- a/src/app/actions/auth.ts +++ b/src/app/actions/auth.ts @@ -249,9 +249,9 @@ export async function registration( error: null }; */ } catch (error: unknown) { + const errors: Record = {}; if (error) { const typedError = error as any; - const errors: Record = {}; switch (typedError.message_code) { case 1: @@ -295,7 +295,18 @@ export async function registration( }; } - throw error; + return { + previousState: { + fullName, + email, + password, + confirm_password, + phone, + companyName, + agree + }, + error: errors + }; } redirect('/pages/dashboard'); @@ -345,7 +356,7 @@ export async function confirmEmail( export async function resetPassword( state: any | undefined, - formData: any, + formData: FormData, ) { const email = formData.get('email'); console.log('resetPassword'); @@ -368,19 +379,20 @@ export async function resetPassword( }); if (response.ok) { - const parsed = await response.json(); console.log(parsed); if (parsed.message_code === 0) { } else { - throw parsed.message_body; + throw 'error'; } } } catch (error) { - throw error; + return { + error: error + }; } - + console.log('redirect') redirect(`/reset-password?email=${email}`); } @@ -413,8 +425,6 @@ export async function confirmPassword( const validatedFields = SignupFormSchema.safeParse(dataToValidate); - console.log(validatedFields); - if (!validatedFields.success) { const errors: Record = {} JSON.parse(validatedFields.error.message).forEach((obj: { diff --git a/src/app/ui/forms/forgot-password-form.tsx b/src/app/ui/forms/forgot-password-form.tsx index 93f48c5..f7430a4 100644 --- a/src/app/ui/forms/forgot-password-form.tsx +++ b/src/app/ui/forms/forgot-password-form.tsx @@ -2,9 +2,8 @@ import styles from '@/app/styles/module/login.module.scss' import { useActionState } from 'react'; - import { useTranslations } from 'next-intl'; -import {resetPassword} from '@/app/actions/auth'; +import { resetPassword } from '@/app/actions/auth'; export default function ForgotPasswordForm() { const [state, formAction, isPending] = useActionState( @@ -26,19 +25,14 @@ export default function ForgotPasswordForm() { className={`${styles['form-input']}`} placeholder={t('enter-email')} /> - {/* {state === 'error' && ( + {state?.error === 'error' && (

- Восстановление пароля не работает + {t('register-error-email-invalid')}

- )} */} + )} - {/* {state?.error.server && ( -

- {t(state?.error.server)} -

- )} */} ) diff --git a/src/app/ui/forms/reset-password-form.tsx b/src/app/ui/forms/reset-password-form.tsx index 3550d9c..7417924 100644 --- a/src/app/ui/forms/reset-password-form.tsx +++ b/src/app/ui/forms/reset-password-form.tsx @@ -112,14 +112,14 @@ export default function ResetPasswordForm() {
@@ -201,7 +201,7 @@ export default function ResetPasswordForm() {

)} */} ) diff --git a/src/i18n/messages/en.json b/src/i18n/messages/en.json index b222f6b..3bfea82 100644 --- a/src/i18n/messages/en.json +++ b/src/i18n/messages/en.json @@ -275,6 +275,8 @@ "confirmation-code": "Confirmation code", "enter-confirmation-code": "Enter the confirmation code", "personal": "Personal", - "unknown-error": "Unknown error" + "unknown-error": "Unknown error", + "change-password": "Change Password", + "recovery-code": "Recovery code" } } \ No newline at end of file diff --git a/src/i18n/messages/ru.json b/src/i18n/messages/ru.json index 527aad1..04dd8f6 100644 --- a/src/i18n/messages/ru.json +++ b/src/i18n/messages/ru.json @@ -275,6 +275,8 @@ "confirmation-code": "Код подтверждения", "enter-confirmation-code": "Введите код подтверждения", "personal": "Личный", - "unknown-error": "Неизвестная ошибка" + "unknown-error": "Неизвестная ошибка", + "change-password": "Изменить пароль", + "recovery-code": "Код восстановления" } } \ No newline at end of file