application stuff
This commit is contained in:
1 parent
aa7ef06858
commit
e3e291b8cf
4 files changed
+97
-5
No files matched your search
@@ -0,0 +1,47 @@
|
||||
const message = document.querySelector('[data-status-msg]');
|
||||
const box = document.querySelector('[data-invite]');
|
||||
const link = box.querySelector('a');
|
||||
const code = decodeURIComponent(location.hash.slice(1));
|
||||
const INVITE = /^https:\/\/discord\.(gg|com\/invite)\/[A-Za-z0-9-]+$/;
|
||||
const POLL_MS = 15000;
|
||||
|
||||
const TEXT = {
|
||||
pending: 'Your application has been received and is waiting for a moderator. This page updates on its own, so you can keep it open, or bookmark it and come back later.',
|
||||
accepted: 'Good news: your application was approved! Use the invite below to join the server. It works once and expires 7 days after approval.',
|
||||
declined: 'Your application was not approved at this time.',
|
||||
unknown: 'We couldn’t find this application. It may be more than 14 days old, or the link may be incomplete.',
|
||||
};
|
||||
|
||||
function show(state) {
|
||||
message.textContent = TEXT[state];
|
||||
}
|
||||
|
||||
async function check() {
|
||||
try {
|
||||
const res = await fetch('/api/join/status', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ code }),
|
||||
});
|
||||
if (res.status === 404) return show('unknown');
|
||||
if (res.ok) {
|
||||
const body = await res.json();
|
||||
if (body.status === 'accepted' && INVITE.test(body.invite || '')) {
|
||||
link.href = body.invite;
|
||||
box.hidden = false;
|
||||
return show('accepted');
|
||||
}
|
||||
if (body.status === 'declined') return show('declined');
|
||||
show('pending');
|
||||
}
|
||||
} catch {
|
||||
// Network hiccup: keep the last message and try again.
|
||||
}
|
||||
setTimeout(check, POLL_MS);
|
||||
}
|
||||
|
||||
if (code) {
|
||||
check();
|
||||
} else {
|
||||
show('unknown');
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user