import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { api } from "@/lib/api"; import { productOptionsFromT } from "@/lib/productLabels"; import type { Product } from "@/types"; import { Button } from "@/ui/Button"; import { CheckboxesGroup } from "@/ui/CheckboxesGroup"; import { PhoneInputRu } from "@/ui/PhoneInputRu"; import { useRefererStore } from "@/stores/useRefererStore"; import { Controller, FormProvider, useForm, type SubmitHandler, } from "react-hook-form"; import type { LeadFormValues } from "./types"; export type { LeadFormValues } from "./types"; <<<<<<< HEAD ======= const GENERIC_SUBMIT_ERROR = "Не удалось отправить заявку. Попробуйте позже."; >>>>>>> 258568050cbcb3e46cc31416fc9a0ba7a21ac066 export function LeadForm({ defaultProducts, idPrefix = "", onSuccess, phonePlaceholder, formClassName = "lg:space-y-[1.944vw] md:max-lg:space-y-7 space-y-3", }: { defaultProducts: Product[]; /** Префикс для id полей (например `modal-`), чтобы избежать дублей в DOM */ idPrefix?: string; onSuccess: (id: string) => void; phonePlaceholder?: string; formClassName?: string; }) { const { t } = useTranslation(); const { referer } = useRefererStore(); const [submitError, setSubmitError] = useState(null); const projectOptions = useMemo(() => productOptionsFromT(t), [t]); const form = useForm({ defaultValues: { fullname: "", email: "", phone: "", products: defaultProducts, }, }); const { register, handleSubmit, formState, control } = form; const nameId = idPrefix ? `${idPrefix}name` : "name"; const emailId = idPrefix ? `${idPrefix}email` : "email"; const onSubmit: SubmitHandler = async (data) => { setSubmitError(null); try { const { id } = await api .post("mail", { json: { ...data, referer } }) .json<{ id: string }>(); onSuccess(id); } catch { setSubmitError(t("leadForm.submitError")); } }; return (
{submitError ? (

{submitError}

) : null}

{t("leadForm.needTitle")}

name="products" options={projectOptions} />
( )} />
{t("leadForm.consentBefore")}{" "} {t("leadForm.consentLinkData")} {" "} {t("leadForm.consentMiddle")} {t("leadForm.consentLinkPolicy")}
); }