cambios postproduccion
This commit is contained in:
+33
-31
@@ -1,19 +1,21 @@
|
||||
/**
|
||||
* Envío AJAX del formulario de contacto + modal de éxito.
|
||||
* Envío de formulario de contacto con Formspree + modal de éxito.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
const form = document.getElementById('form-contacto');
|
||||
const modal = document.getElementById('modal-contacto');
|
||||
|
||||
const form = document.getElementById('form-contacto');
|
||||
const modal = document.getElementById('modal-contacto');
|
||||
const btnCerrar = modal ? modal.querySelector('.modal-cerrar') : null;
|
||||
const btnEnviar = form ? form.querySelector('button[type="submit"]') : null;
|
||||
|
||||
|
||||
if (!form || !modal) return;
|
||||
|
||||
|
||||
const FORMSPREE_URL = 'https://formspree.io/f/xkjgjzpn';
|
||||
|
||||
let redirectTimer = null;
|
||||
|
||||
// ── Helpers ────────────────────────────────────────────────────────
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────
|
||||
function abrirModal(mensaje) {
|
||||
const texto = modal.querySelector('.modal-mensaje');
|
||||
if (texto && mensaje) texto.textContent = mensaje;
|
||||
@@ -21,7 +23,7 @@
|
||||
modal.setAttribute('aria-hidden', 'false');
|
||||
document.body.style.overflow = 'hidden';
|
||||
}
|
||||
|
||||
|
||||
function cerrarModal(redirigir = false) {
|
||||
modal.classList.remove('is-visible');
|
||||
modal.setAttribute('aria-hidden', 'true');
|
||||
@@ -29,67 +31,67 @@
|
||||
if (redirectTimer) clearTimeout(redirectTimer);
|
||||
if (redirigir) window.location.href = 'index.html';
|
||||
}
|
||||
|
||||
|
||||
function mostrarError(mensaje) {
|
||||
// Reutilizamos el modal con estética distinta
|
||||
modal.classList.add('is-error');
|
||||
abrirModal(mensaje);
|
||||
}
|
||||
|
||||
|
||||
// ── Cerrar modal ───────────────────────────────────────────────────
|
||||
if (btnCerrar) {
|
||||
btnCerrar.addEventListener('click', () => cerrarModal(true));
|
||||
}
|
||||
|
||||
modal.addEventListener('click', (e) => {
|
||||
if (e.target === modal) cerrarModal(true);
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape' && modal.classList.contains('is-visible')) {
|
||||
cerrarModal(true);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// ── Submit ─────────────────────────────────────────────────────────
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
|
||||
// Validación nativa HTML5
|
||||
if (!form.checkValidity()) {
|
||||
form.reportValidity();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Estado de carga
|
||||
const textoOriginal = btnEnviar ? btnEnviar.innerHTML : '';
|
||||
if (btnEnviar) {
|
||||
btnEnviar.disabled = true;
|
||||
btnEnviar.innerHTML = 'Enviando…';
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const data = new FormData(form);
|
||||
const res = await fetch('enviar.php', {
|
||||
|
||||
const res = await fetch(FORMSPREE_URL, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
headers: { 'X-Requested-With': 'XMLHttpRequest' },
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const json = await res.json().catch(() => ({
|
||||
status: 'error',
|
||||
message: 'Respuesta inválida del servidor.',
|
||||
}));
|
||||
|
||||
if (json.status === 'success') {
|
||||
|
||||
if (res.ok) {
|
||||
modal.classList.remove('is-error');
|
||||
abrirModal(json.message || '¡Gracias! Te contactaremos en breve.');
|
||||
abrirModal('¡Mensaje enviado! Te contactaremos en menos de 48 horas hábiles.');
|
||||
form.reset();
|
||||
|
||||
const delay = Number(json.delay) || 4000;
|
||||
|
||||
// Redirección automática después de 4 segundos
|
||||
redirectTimer = setTimeout(() => {
|
||||
window.location.href = json.redirect || 'index.html';
|
||||
}, delay);
|
||||
window.location.href = 'index.html';
|
||||
}, 4000);
|
||||
} else {
|
||||
mostrarError(json.message || 'No pudimos enviar tu mensaje.');
|
||||
const json = await res.json().catch(() => ({}));
|
||||
mostrarError(json.error || 'No pudimos enviar tu mensaje. Intentá nuevamente.');
|
||||
}
|
||||
} catch (err) {
|
||||
mostrarError('Hubo un problema de conexión. Probá nuevamente.');
|
||||
|
||||
Reference in New Issue
Block a user