From be57858e9059d4fe240af6fced3fef94d1bc60b3 Mon Sep 17 00:00:00 2001 From: RedrockJS Date: Mon, 7 Jul 2025 16:18:35 +0300 Subject: [PATCH] fix: add forms sending --- src/entities/callback-form/ui.tsx | 99 ++++++++++++++++-- src/feature/article/consultation/ui.tsx | 106 ++++++++++++++++++-- src/feature/article/related-articles/ui.tsx | 5 +- src/views/expertise/autotech/ui.tsx | 8 +- 4 files changed, 196 insertions(+), 22 deletions(-) diff --git a/src/entities/callback-form/ui.tsx b/src/entities/callback-form/ui.tsx index 5e2f392..10e58f8 100644 --- a/src/entities/callback-form/ui.tsx +++ b/src/entities/callback-form/ui.tsx @@ -1,20 +1,103 @@ +'use client'; + import s from './styles.module.scss'; import { Button, Input, PhoneInput } from '@shared/ui'; +import { Controller, useForm } from 'react-hook-form'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { isValidPhoneNumber } from 'libphonenumber-js/min'; +import { z } from 'zod'; +import { sendFormFn } from '@shared/api/api.service'; +import toast from 'react-hot-toast'; type CallbackFormProps = { - pageName: string; + pageName?: string; }; -function CallbackForm({ pageName }: CallbackFormProps) { +const FormSchema = z.object({ + name: z + .string() + .min(3, { message: 'Поле должно содержать не менее 3-х букв' }) + .regex(/^[A-Za-zА-Яа-яЁё]+(?:[ '-][A-Za-zА-Яа-яЁё]+)*$/, { + message: 'Поле содержит некорректные символы', + }), + phone: z.string().refine(isValidPhoneNumber, 'Некорректный номер телефона'), +}); + +type TForm = z.infer; + +const defaultValues = { + name: '', + phone: '', +}; + +function CallbackForm({ pageName = 'noname-form' }: CallbackFormProps) { + const { + handleSubmit, + control, + reset, + clearErrors, + formState: { errors }, + } = useForm({ + mode: 'onSubmit', + reValidateMode: 'onBlur', + resolver: zodResolver(FormSchema), + defaultValues, + }); + + const onSubmit = async (data: TForm) => { + const payload = { + ...data, + form: pageName, + }; + + try { + await sendFormFn(payload); + toast.success('Заявка на консультацию принята'); + reset(defaultValues); + } catch (e) { + toast.error('Ошибка при отправке заявки...', { + duration: 3000, + }); + } + }; + return (

Узнать точную стоимость и срок экспертизы

-
- - + ( + { + clearErrors('name'); + field.onChange(e); + }} + error={errors && errors.name?.message} + /> + )} + /> + ( + { + clearErrors('phone'); + field.onChange(e); + }} + error={errors && errors.phone?.message} + /> + )} />
- +
diff --git a/src/views/expertise/autotech/ui.tsx b/src/views/expertise/autotech/ui.tsx index 18c234e..966ab88 100644 --- a/src/views/expertise/autotech/ui.tsx +++ b/src/views/expertise/autotech/ui.tsx @@ -5,7 +5,7 @@ import { Breadcrumbs, PartnersSlider } from '@/widgets'; import { sidebarData } from './model/sidebar'; import { relatedArticlesData } from './model/relatedArticles'; import { expertData } from './model/expert'; -import { breadcrumbData } from './model/breadcrums'; +import { breadcrumbData, PAGE_NAME } from './model/breadcrums'; function AutoTech() { return ( @@ -58,7 +58,7 @@ function AutoTech() { - +

Документы, необходимые для экспертизы:

@@ -95,8 +95,8 @@ function AutoTech() { - - + +