'use client'; import { useEffect, useRef, useState } from 'react'; import { useRouter } from 'next/navigation'; import { crearLead } from '@/app/solicitud/actions'; type FormData = { name: string; email: string; phone: string; }; type FormErrors = Partial>; type SubmitStatus = 'idle' | 'loading' | 'success' | 'error'; const initialData: FormData = { name: '', email: '', phone: '', }; const initialConsents = { privacy: false, contracting: false, }; function validateForm(data: FormData): FormErrors { const errors: FormErrors = {}; if (!data.name.trim()) errors.name = 'El nombre es obligatorio'; if (!data.email.trim()) { errors.email = 'El email es obligatorio'; } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)) { errors.email = 'Introduce un email válido'; } if (!data.phone.trim()) { errors.phone = 'El teléfono es obligatorio'; } else if (!/^[+\d\s\-().]{7,20}$/.test(data.phone)) { errors.phone = 'Introduce un teléfono válido'; } return errors; } function LeadForm({ slug }: { slug: string }) { const router = useRouter(); const [formData, setFormData] = useState(initialData); const [consents, setConsents] = useState(initialConsents); const [errors, setErrors] = useState({}); const [touched, setTouched] = useState>>({}); const [status, setStatus] = useState('idle'); const [submitError, setSubmitError] = useState(null); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); if (touched[name as keyof FormData]) { const newErrors = validateForm({ ...formData, [name]: value }); setErrors((prev) => ({ ...prev, [name]: newErrors[name as keyof FormData] })); } }; const handleBlur = (e: React.FocusEvent) => { const { name } = e.target; setTouched((prev) => ({ ...prev, [name]: true })); const newErrors = validateForm(formData); setErrors((prev) => ({ ...prev, [name]: newErrors[name as keyof FormData] })); }; const consentsGranted = consents.privacy && consents.contracting; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setTouched({ name: true, email: true, phone: true }); const validationErrors = validateForm(formData); if (Object.keys(validationErrors).length > 0) { setErrors(validationErrors); return; } if (!consentsGranted) return; setStatus('loading'); setSubmitError(null); const result = await crearLead(slug, { nombre: formData.name, email: formData.email, telefono: formData.phone, consentPrivacidad: consents.privacy, consentContratacion: consents.contracting, }); if (!result.ok) { setStatus('error'); setSubmitError(result.error); return; } router.push(`/solicitud/${result.leadId}/fotos`); }; const handleReset = () => { setStatus('idle'); setFormData(initialData); setConsents(initialConsents); setErrors({}); setTouched({}); }; if (status === 'success') { return (

¡Te llamamos enseguida!

En menos de 2 minutos te llamamos al teléfono que nos has dejado. Tendrás el render y el presupuesto en tu WhatsApp.

); } return (
{/* Name + Email */}
{errors.name && touched.name && ( {errors.name} )}
{errors.email && touched.email && ( {errors.email} )}
{/* Phone */}
{errors.phone && touched.phone && ( {errors.phone} )}
{/* Consents */}
Consentimientos
{submitError && (

{submitError}

)} {/* Submit */}
); } export default function Hero({ slug }: { slug: string }) { const heroRef = useRef(null); useEffect(() => { const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { entry.target.classList.add('opacity-100', 'translate-y-0'); entry.target.classList.remove('opacity-0', 'translate-y-6'); } }); }, { threshold: 0.1 } ); const elements = heroRef.current?.querySelectorAll('.reveal'); elements?.forEach((el) => observer.observe(el)); return () => observer.disconnect(); }, []); return (
{/* Grid 2 columnas */}
{/* Columna izquierda — textos */}

Tu reforma,
presupuestada
en 5 minutos.

Deja tu teléfono, sube una foto de tu cocina o baño y te llamamos desde tu provincia en menos de 2 minutos. Al colgar recibirás por WhatsApp el render de tu reforma + presupuesto desglosado.

{/* CTAs */}
{/* Columna derecha — formulario */}

Pide tu presupuesto

En menos de 2 minutos te llamamos · Render por WhatsApp


{/* Servicios */}
{[ { icon: ( ), title: 'Reformas Integrales', description: 'Gestionamos tu reforma de principio a fin con un objetivo claro: cumplir plazos y superar expectativas.', }, { icon: ( ), title: 'Reformas de Cocinas', description: 'Transforma tu cocina en el espacio que siempre quisiste. Materiales de calidad, diseño a tu medida.', }, { icon: ( ), title: 'Reformas de Baños', description: 'El baño es el espacio más personal del hogar. Te ayudamos a conseguir el resultado que mereces.', }, ].map(({ icon, title, description }) => (
{icon}

{title}

{description}

))}
); }