-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoin.js
More file actions
110 lines (96 loc) · 3.66 KB
/
Copy pathjoin.js
File metadata and controls
110 lines (96 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(function () {
const DISCORD_INVITE = 'https://discord.gg/5ayHMr8khv';
const UWO_EMAIL = /^[a-z0-9](?:[a-z0-9._-]{1,62}[a-z0-9])?@uwo\.ca$/i;
const JOIN_PENDING_KEY = 'wemars_join_pending';
const form = document.getElementById('join-form');
const emailInput = document.getElementById('join-email');
const autoresponseInput = document.getElementById('join-autoresponse');
const nextInput = document.getElementById('join-next');
const errorEl = document.getElementById('join-error');
const successEl = document.getElementById('join-success');
const successMessageEl = document.getElementById('join-success-message');
const successEmailEl = document.getElementById('join-success-email');
const noticeEl = document.getElementById('join-notice');
const submitBtn = document.getElementById('join-submit');
if (!form) return;
function showError(message) {
if (!errorEl) return;
errorEl.textContent = message;
errorEl.hidden = false;
}
function showSuccess(email) {
if (errorEl) errorEl.hidden = true;
if (successEl) successEl.hidden = false;
if (form) form.hidden = true;
if (successMessageEl) {
successMessageEl.textContent =
'Check your inbox for the Discord invite link. If you do not see it, check your junk or spam folder.';
}
if (successEmailEl && email) {
successEmailEl.textContent = 'We sent it to ' + email + '.';
}
}
function isUwoEmail(value) {
return UWO_EMAIL.test(value.trim());
}
function clearSentQuery() {
if (window.history.replaceState) {
window.history.replaceState({}, document.title, window.location.pathname);
}
}
function setAutoresponseMessage() {
if (!autoresponseInput) return;
autoresponseInput.value = [
'Hi,',
'',
'Thanks for your interest in WEMARS! Join our Discord server here:',
'',
DISCORD_INVITE,
'',
'See you in the server!',
'WEMARS Team',
].join('\n');
}
function setNextUrl() {
if (!nextInput) return;
const returnUrl = window.location.href.split('?')[0].split('#')[0];
if (returnUrl.indexOf('http') === 0) {
nextInput.value = returnUrl + '?sent=1';
}
}
setAutoresponseMessage();
setNextUrl();
if (window.location.search.indexOf('sent=1') !== -1) {
const pendingEmail = sessionStorage.getItem(JOIN_PENDING_KEY);
if (pendingEmail && isUwoEmail(pendingEmail)) {
showSuccess(pendingEmail);
sessionStorage.removeItem(JOIN_PENDING_KEY);
} else {
showError('Something went wrong. Please submit the form again with your @uwo.ca email.');
}
clearSentQuery();
}
if (window.location.protocol === 'file:') {
if (noticeEl) noticeEl.hidden = false;
if (submitBtn) submitBtn.disabled = true;
return;
}
form.addEventListener('submit', function (event) {
const email = emailInput.value.trim().toLowerCase();
if (!isUwoEmail(email)) {
event.preventDefault();
showError('Please enter your Western-assigned @uwo.ca email address.');
emailInput.focus();
return;
}
emailInput.value = email;
setAutoresponseMessage();
setNextUrl();
sessionStorage.setItem(JOIN_PENDING_KEY, email);
if (errorEl) errorEl.hidden = true;
if (submitBtn) {
submitBtn.disabled = true;
submitBtn.textContent = 'Sending…';
}
});
})();