87 lines
2.7 KiB
JavaScript
87 lines
2.7 KiB
JavaScript
(function (run) {
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
|
else run();
|
|
})(function () {
|
|
(function () {
|
|
var form = document.querySelector('[data-subscribe]');
|
|
if (!form || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
|
var cfg = {};
|
|
try { cfg = JSON.parse(document.getElementById('vino-config').textContent); } catch (e) {}
|
|
var text = cfg.text || {};
|
|
var done = form.parentNode.querySelector('[data-subscribe-msg]');
|
|
var err = form.querySelector('[data-subscribe-err]');
|
|
var guard = form.querySelector('altcha-widget');
|
|
|
|
function clear(el) {
|
|
el.classList.remove('vino-inp--invalid');
|
|
el.removeAttribute('aria-invalid');
|
|
}
|
|
|
|
function mark(el) {
|
|
el.classList.add('vino-inp--invalid');
|
|
el.setAttribute('aria-invalid', 'true');
|
|
}
|
|
|
|
function say(msg) {
|
|
err.textContent = msg;
|
|
err.hidden = !msg;
|
|
}
|
|
|
|
form.addEventListener('focusin', function () { VinoAltcha.arm(guard); }, { once: true });
|
|
form.addEventListener('input', function (e) { clear(e.target); say(''); });
|
|
form.addEventListener('change', function (e) { clear(e.target); say(''); });
|
|
|
|
form.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
var f = form.elements, focus = null, msg = '';
|
|
[f.email, f.consent].forEach(clear);
|
|
if (!f.email.value.trim() || !f.email.checkValidity()) {
|
|
mark(f.email);
|
|
focus = f.email;
|
|
msg = form.getAttribute('data-error');
|
|
}
|
|
if (!f.consent.checked) {
|
|
mark(f.consent);
|
|
if (!focus) focus = f.consent;
|
|
if (!msg) msg = text.consent;
|
|
}
|
|
say(msg);
|
|
if (focus) {
|
|
focus.focus();
|
|
return;
|
|
}
|
|
var button = form.querySelector('[type="submit"]');
|
|
button.disabled = true;
|
|
VinoAltcha.arm(guard);
|
|
VinoAltcha.armed(guard)
|
|
.then(function (verified) {
|
|
if (!verified) throw new Error('guard');
|
|
return BX.ajax.runAction('jinove:vinoteca.lead.subscribe', {
|
|
data: {
|
|
email: f.email.value,
|
|
consent: 'Y',
|
|
website: f.website.value,
|
|
altcha: f.altcha ? f.altcha.value : '',
|
|
page: window.location.pathname
|
|
}
|
|
});
|
|
})
|
|
.then(function (res) {
|
|
if (res && res.data && res.data.ok) {
|
|
form.hidden = true;
|
|
done.textContent = form.getAttribute('data-ok');
|
|
done.hidden = false;
|
|
} else {
|
|
say(text.fail);
|
|
}
|
|
})
|
|
.catch(function (res) {
|
|
VinoAltcha.reset(guard);
|
|
var code = res && res.errors && res.errors[0] ? res.errors[0].code : '';
|
|
say(code === 'email' ? form.getAttribute('data-error') : code === 'consent' ? text.consent : text.fail);
|
|
})
|
|
.then(function () { button.disabled = false; });
|
|
});
|
|
})();
|
|
});
|