-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (65 loc) · 2.12 KB
/
app.js
File metadata and controls
77 lines (65 loc) · 2.12 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
/**
* Par Not Far Waitlist Form
* Replace YOUR_FORM_ID with your Formspree form ID from https://formspree.io
*/
(function () {
const FORM_ID = 'YOUR_FORM_ID';
const form = document.getElementById('waitlist-form');
const submitBtn = document.getElementById('submit-btn');
const messageEl = document.getElementById('form-message');
if (!form || !FORM_ID || FORM_ID === 'YOUR_FORM_ID') {
if (messageEl) {
messageEl.textContent = 'Form not configured. Replace YOUR_FORM_ID in app.js with your Formspree form ID.';
messageEl.className = 'form-message error';
}
return;
}
form.setAttribute('action', `https://formspree.io/f/${FORM_ID}`);
form.addEventListener('submit', async function (e) {
e.preventDefault();
const email = form.querySelector('#email');
if (!email || !email.value.trim()) {
showMessage('Please enter your email.', 'error');
return;
}
if (!isValidEmail(email.value)) {
showMessage('Please enter a valid email address.', 'error');
return;
}
setLoading(true);
showMessage('');
try {
const response = await fetch(form.action, {
method: 'POST',
body: new FormData(form),
headers: { Accept: 'application/json' },
});
const data = await response.json();
if (response.ok) {
showMessage('Thanks! You\'re on the list. We\'ll be in touch.', 'success');
form.reset();
} else {
const msg = data.error || 'Something went wrong. Please try again.';
showMessage(msg, 'error');
}
} catch (err) {
showMessage('Something went wrong. Please try again.', 'error');
} finally {
setLoading(false);
}
});
function showMessage(text, type) {
if (!messageEl) return;
messageEl.textContent = text;
messageEl.className = 'form-message' + (type ? ' ' + type : '');
}
function setLoading(loading) {
if (submitBtn) {
submitBtn.disabled = loading;
submitBtn.textContent = loading ? 'Sending…' : 'Get Early Access';
}
}
function isValidEmail(value) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
}
})();