33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
const form = document.querySelector('form[data-join]');
|
|
|
|
if (form) {
|
|
const status = form.querySelector('[data-status]');
|
|
const button = form.querySelector('button[type="submit"]');
|
|
|
|
form.addEventListener('submit', async (event) => {
|
|
event.preventDefault();
|
|
status.textContent = 'Sending…';
|
|
button.disabled = true;
|
|
|
|
try {
|
|
const res = await fetch('/api/join', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(Object.fromEntries(new FormData(form))),
|
|
});
|
|
const body = await res.json().catch(() => ({}));
|
|
|
|
if (res.ok && body.code) {
|
|
location.href = `/join-status/#${encodeURIComponent(body.code)}`;
|
|
return;
|
|
}
|
|
status.textContent = body.error ||
|
|
(res.status === 429
|
|
? 'Too many attempts. Please wait a few minutes and try again.'
|
|
: 'Something went wrong. Please try again, or email us instead.');
|
|
} catch {
|
|
status.textContent = 'Could not reach the server. Check your connection and try again.';
|
|
}
|
|
button.disabled = false;
|
|
});
|
|
} |