import { useEffect, useRef, useState, type FormEvent } from 'react' import { clubFormats } from '../data/content' import { submitLead } from '../lib/lead' import { cx } from '../lib/cx' import { Button } from './Button' import { ConsentCheckbox, Honeypot, SelectField, TextField, TextareaField } from './FormField' import { ArrowRightIcon } from './Icons' const EMPTY = { name: '', company: '', phone: '', email: '', city: '', club_format: clubFormats[0], comment: '', } type Status = { text: string; tone: 'idle' | 'success' | 'error' } export function LeadForm({ configuration, commentPrefill }: { configuration: string; commentPrefill: string }) { const formRef = useRef(null) const [values, setValues] = useState(EMPTY) const [consent, setConsent] = useState(false) const [honeypot, setHoneypot] = useState('') const [pending, setPending] = useState(false) const [status, setStatus] = useState({ text: '', tone: 'idle' }) // The constructor's "получить эту конфигурацию" CTA writes into the comment. useEffect(() => { if (commentPrefill) setValues((current) => ({ ...current, comment: commentPrefill })) }, [commentPrefill]) const set = (key: keyof typeof EMPTY) => (event: { target: { value: string } }) => setValues((current) => ({ ...current, [key]: event.target.value })) async function onSubmit(event: FormEvent) { event.preventDefault() const form = formRef.current if (!form) return if (!form.checkValidity()) { form.reportValidity() setStatus({ text: 'Проверьте обязательные поля и согласие.', tone: 'error' }) return } if (honeypot) return setPending(true) setStatus({ text: '', tone: 'idle' }) const result = await submitLead('request', { ...values, configuration, website: honeypot }) if (result.ok) { setValues(EMPTY) setConsent(false) setStatus({ text: 'Спасибо. Заявка отправлена — специалист свяжется с вами.', tone: 'success' }) } else { setStatus({ text: result.error ?? 'Не удалось отправить заявку.', tone: 'error' }) } setPending(false) } return (

Получить конфигурацию

Выбранный в конструкторе состав автоматически добавится в заявку.

{status.text}
) }