const ReservationWidget = () => { const [formData, setFormData] = React.useState({ name: "", contact: "", people: 2, date: "", time: "", sector: "vereda", comments: "", }); const [errors, setErrors] = React.useState({}); const validators = { name: (value) => (value.trim().length < 3 ? "Ingresá un nombre válido" : ""), contact: (value) => /@/.test(value) ? /.+@.+\..+/.test(value) ? "" : "Email inválido" : value.replace(/[^0-9+ ]/g, "").length < 8 ? "Ingresá un teléfono o email válido" : "", people: (value) => (value < 1 ? "Debe ser al menos 1" : value > 12 ? "Consultanos por grupos grandes" : ""), date: (value) => (!value ? "Seleccioná una fecha" : ""), time: (value) => (!value ? "Seleccioná un horario" : ""), }; function handleChange(event) { const { name, value } = event.target; const newValue = name === "people" ? Number(value) : value; setFormData((prev) => ({ ...prev, [name]: newValue })); if (validators[name]) { setErrors((prev) => ({ ...prev, [name]: validators[name](newValue) })); } } function validateForm() { const nextErrors = {}; Object.entries(validators).forEach(([field, validate]) => { const result = validate(formData[field]); if (result) { nextErrors[field] = result; } }); setErrors(nextErrors); return Object.keys(nextErrors).length === 0; } function handleSubmit(event) { event.preventDefault(); if (!validateForm()) return; // Formatear la fecha en formato argentino (dd/mm/aaaa) let fechaArg = formData.date; if (fechaArg && /^\d{4}-\d{2}-\d{2}$/.test(fechaArg)) { const [a, m, d] = fechaArg.split("-"); fechaArg = `${d}/${m}/${a}`; } else { fechaArg = "[fecha]"; } // Generar el mensaje de WhatsApp const message = `Hola Parrilla La Nueva Adelina, quiero reservar para ${formData.people} personas el ${fechaArg} a las ${formData.time || "[hora]"} en el sector ${formData.sector}.\nNombre: ${formData.name}\nContacto: ${formData.contact}\nComentarios: ${formData.comments}`; const url = `https://wa.me/5491132907617?text=${encodeURIComponent(message)}`; window.open(url, "_blank"); } return (
{errors.name &&

{errors.name}

}
{errors.contact &&

{errors.contact}

}
{errors.people &&

{errors.people}

}
{errors.date &&

{errors.date}

}
{errors.time &&

{errors.time}

}