184 lines
4.6 KiB
JavaScript
184 lines
4.6 KiB
JavaScript
(() => {
|
|
const dialog = document.getElementById('booking');
|
|
if (!dialog || typeof dialog.showModal !== 'function') {
|
|
return;
|
|
}
|
|
|
|
const form = dialog.querySelector('[data-booking-form]');
|
|
const done = dialog.querySelector('[data-booking-done]');
|
|
const doneText = dialog.querySelector('[data-booking-done-text]');
|
|
const message = dialog.querySelector('[data-booking-message]');
|
|
const submit = form.querySelector('.booking__submit');
|
|
const texts = JSON.parse(form.dataset.messages || '{}');
|
|
const phone = form.elements.namedItem('phone');
|
|
const guard = form.querySelector('altcha-widget');
|
|
|
|
const arm = () => {
|
|
if (!guard) {
|
|
return;
|
|
}
|
|
if (!document.querySelector('script[data-altcha]')) {
|
|
const script = document.createElement('script');
|
|
script.type = 'module';
|
|
script.src = form.dataset.altchaScript || '';
|
|
script.setAttribute('data-altcha', '');
|
|
document.head.appendChild(script);
|
|
return;
|
|
}
|
|
if (typeof guard.getState === 'function' && !['verified', 'verifying'].includes(guard.getState())) {
|
|
guard.verify();
|
|
}
|
|
};
|
|
|
|
const ready = () =>
|
|
new Promise((resolve) => {
|
|
let tick = 0;
|
|
const poll = () => {
|
|
const state = guard && typeof guard.getState === 'function' ? guard.getState() : '';
|
|
if (state === 'verified') {
|
|
resolve(guard.value || form.elements.namedItem('altcha')?.value || '');
|
|
return;
|
|
}
|
|
const failed = state === 'error' || state === 'expired';
|
|
const idle = state === 'unverified' && tick >= 30;
|
|
if ((failed || idle) && tick % 30 === 0) {
|
|
guard.verify();
|
|
}
|
|
if (++tick > 150) {
|
|
resolve('');
|
|
return;
|
|
}
|
|
setTimeout(poll, 100);
|
|
};
|
|
poll();
|
|
});
|
|
|
|
const rearm = () => {
|
|
if (guard && typeof guard.reset === 'function') {
|
|
guard.reset();
|
|
guard.verify();
|
|
}
|
|
};
|
|
|
|
const clearErrors = () => {
|
|
message.textContent = '';
|
|
form.querySelectorAll('[aria-invalid="true"]').forEach((field) => {
|
|
field.removeAttribute('aria-invalid');
|
|
field.removeAttribute('aria-describedby');
|
|
});
|
|
};
|
|
|
|
const showError = (fieldName, text) => {
|
|
message.textContent = text;
|
|
const field = fieldName ? form.elements.namedItem(fieldName) : null;
|
|
if (field instanceof HTMLElement) {
|
|
field.setAttribute('aria-invalid', 'true');
|
|
field.setAttribute('aria-describedby', message.id);
|
|
field.focus();
|
|
}
|
|
};
|
|
|
|
const validate = () => {
|
|
const name = form.elements.namedItem('name').value.trim();
|
|
if (name.length < 2) {
|
|
return 'name';
|
|
}
|
|
if (phone.smiliaPhone ? !phone.smiliaPhone.complete() : phone.value.replace(/\D/g, '').length < 9) {
|
|
return 'phone';
|
|
}
|
|
if (!form.elements.namedItem('consent').checked) {
|
|
return 'consent';
|
|
}
|
|
return '';
|
|
};
|
|
|
|
const open = () => {
|
|
form.hidden = false;
|
|
done.hidden = true;
|
|
clearErrors();
|
|
form.elements.namedItem('page').value = window.location.href;
|
|
arm();
|
|
dialog.showModal();
|
|
};
|
|
|
|
document.addEventListener('click', (event) => {
|
|
if (event.target.closest('[data-booking-open]')) {
|
|
event.preventDefault();
|
|
open();
|
|
}
|
|
});
|
|
|
|
dialog.addEventListener('click', (event) => {
|
|
if (event.target === dialog || event.target.closest('[data-booking-close]')) {
|
|
dialog.close();
|
|
}
|
|
});
|
|
|
|
form.addEventListener('input', (event) => {
|
|
if (event.target.getAttribute('aria-invalid') === 'true') {
|
|
event.target.removeAttribute('aria-invalid');
|
|
message.textContent = '';
|
|
}
|
|
});
|
|
|
|
form.addEventListener('submit', async (event) => {
|
|
event.preventDefault();
|
|
clearErrors();
|
|
|
|
const invalid = validate();
|
|
if (invalid) {
|
|
showError(invalid, texts[invalid]);
|
|
return;
|
|
}
|
|
|
|
submit.disabled = true;
|
|
submit.setAttribute('aria-busy', 'true');
|
|
|
|
try {
|
|
const proof = await ready();
|
|
if (!proof) {
|
|
showError('', texts.guard);
|
|
rearm();
|
|
return;
|
|
}
|
|
const body = new FormData(form);
|
|
body.set('altcha', proof);
|
|
if (phone.smiliaPhone) {
|
|
body.set('phone', phone.smiliaPhone.value());
|
|
}
|
|
const response = await fetch(form.action, {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'X-WP-Nonce': form.dataset.restNonce || '',
|
|
},
|
|
body,
|
|
});
|
|
|
|
rearm();
|
|
if (response.status === 429) {
|
|
showError('', texts.limit);
|
|
return;
|
|
}
|
|
|
|
const result = await response.json().catch(() => ({}));
|
|
if (!response.ok || !result.ok) {
|
|
showError(result.field || '', result.message || texts.network);
|
|
return;
|
|
}
|
|
|
|
form.reset();
|
|
phone.smiliaPhone?.reset();
|
|
form.hidden = true;
|
|
doneText.textContent = result.message;
|
|
done.hidden = false;
|
|
} catch (error) {
|
|
showError('', texts.network);
|
|
} finally {
|
|
submit.disabled = false;
|
|
submit.removeAttribute('aria-busy');
|
|
}
|
|
});
|
|
})();
|