diff --git a/src/pages/contacto.astro b/src/pages/contacto.astro index 9909fd5..3f491c3 100644 --- a/src/pages/contacto.astro +++ b/src/pages/contacto.astro @@ -1,8 +1,9 @@ --- import Layout from '~/layouts/PageLayout.astro'; -import Contact from '~/components/widgets/Contact.astro'; import Note from '~/components/widgets/Note.astro'; import { getPermalink } from '~/utils/permalinks'; +import Headline from '~/components/blog/Headline.astro'; +import { Icon } from 'astro-icon/components'; const metadata = { title: 'Contacto', @@ -11,34 +12,69 @@ const metadata = { - + + + + + + + Nombre * + + + + Email * + + + + Mensaje * + + + + + Enviar + + + + + + + + El formulario se ha enviado con éxito. + + + + + + Ha habido un error, inténtalo de nuevo mas tarde o contáctanos por correo directamente. + + + + Puedes leer la política de privacidad y el código de conducta. @@ -46,11 +82,84 @@ const metadata = { - \ No newline at end of file + const form = document.getElementById("form") as HTMLFormElement; + const successDiv = document.getElementById("success") as HTMLDivElement; + const errorDiv = document.getElementById("error") as HTMLDivElement; + const submitBtn = document.getElementById("submit") as HTMLButtonElement; + const nameInput = document.getElementById('name') as HTMLInputElement; + const emailInput = document.getElementById('email') as HTMLInputElement; + const messageInput = document.getElementById('message') as HTMLInputElement; + + async function postData(url = '', data = {}) { + const response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }); + + if (response.status !== 200) { + throw new Error('Status not 200'); + } + + return response.json(); + } + + submitBtn?.addEventListener("click", async (event) => { + event.preventDefault(); + + // Remove existing red borders + [nameInput, emailInput, messageInput].forEach(input => { + input?.classList.remove('border-red-500'); + }); + + // Validate fields and add red borders to empty ones + let isValid = true; + [nameInput, emailInput, messageInput].forEach((input: HTMLInputElement) => { + if (!input?.value?.trim()) { + input?.classList.add('border-red-500'); + isValid = false; + } + }); + + // Validate email + const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + if (!emailRegex.test(emailInput?.value.trim())) { + emailInput.classList.add('border-red-500'); + isValid = false; + } + + if (!isValid) { + return; + } + + // Disable the button and show loading state + submitBtn.disabled = true; + submitBtn.textContent = 'Enviando...'; + + // Your form data + const data = { + name: nameInput.value, + email: emailInput.value, + request: messageInput.value + }; + + // Send POST request + try { + await postData('https://n8n.jorgeteixeira.es/webhook/hackudc-contact', data); + // Handle success + submitBtn.textContent = 'Enviado'; + form.classList.add("hidden"); + successDiv.classList.toggle("hidden"); + successDiv.classList.add("flex"); + } catch (error) { + // Handle error (optional) + submitBtn.textContent = 'Error'; + form.classList.add("hidden"); + errorDiv.classList.toggle("hidden"); + errorDiv.classList.add("flex"); + } + }); + diff --git a/src/pages/registro.astro b/src/pages/registro.astro index 6fb79e1..6e1d6ff 100644 --- a/src/pages/registro.astro +++ b/src/pages/registro.astro @@ -9,7 +9,7 @@ import { getPermalink } from '~/utils/permalinks';
El formulario se ha enviado con éxito.
Ha habido un error, inténtalo de nuevo mas tarde o contáctanos por correo directamente.