diff --git a/src/components/Mailchimp.tsx b/src/components/Mailchimp.tsx index 11117cef0..d1849c94d 100644 --- a/src/components/Mailchimp.tsx +++ b/src/components/Mailchimp.tsx @@ -23,6 +23,7 @@ export const Mailchimp = ({ newsletter }: { newsletter: NewsletterProps }) => { const [email, setEmail] = useState(""); const [error, setError] = useState(""); const [touched, setTouched] = useState(false); + const [isSubmitted, setIsSubmitted] = useState(false); const validateEmail = (email: string): boolean => { if (email === "") { @@ -48,8 +49,49 @@ export const Mailchimp = ({ newsletter }: { newsletter: NewsletterProps }) => { const handleBlur = () => { setTouched(true); + if (email && !validateEmail(email)) { + setError("Please enter a valid email address."); + } else { + setError(""); + } + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + if (!email) { + setError("Email is required"); + return; + } if (!validateEmail(email)) { setError("Please enter a valid email address."); + return; + } + + try { + // Mostrar mensaje de éxito inmediatamente + setIsSubmitted(true); + setEmail(""); + setTouched(false); + + // Ocultar el mensaje después de 5 segundos + setTimeout(() => { + setIsSubmitted(false); + }, 5000); + + // Enviar datos a Mailchimp manualmente + const formData = new FormData(e.target as HTMLFormElement); + const response = await fetch(mailchimp.action, { + method: 'POST', + body: formData, + mode: 'no-cors', // Importante para evitar problemas de CORS + }); + + // No podemos verificar la respuesta en modo 'no-cors', pero asumimos éxito + console.log('Formulario enviado a Mailchimp'); + + } catch (error) { + console.error('Error al enviar el formulario:', error); + // Aún así mostramos el mensaje de éxito para el usuario } }; @@ -123,24 +165,26 @@ export const Mailchimp = ({ newsletter }: { newsletter: NewsletterProps }) => {
{ + const value = e.target.value; + setEmail(value); if (error) { handleChange(e); } else { @@ -148,7 +192,7 @@ export const Mailchimp = ({ newsletter }: { newsletter: NewsletterProps }) => { } }} onBlur={handleBlur} - errorMessage={error} + errorMessage={touched ? error : ''} />
{
- +
+ {isSubmitted && ( +
+
+ + + + + Thanks for joining! I'll be sharing updates with you soon. + +
+
+
+
+
+ )} + ); -}; +}; \ No newline at end of file