-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-handler.js
More file actions
63 lines (53 loc) · 1.78 KB
/
email-handler.js
File metadata and controls
63 lines (53 loc) · 1.78 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
// email handler module
const form = document.querySelector('#email-form'),
emailInput = document.querySelector('.email-input'),
popup = document.querySelector('#popup'),
popupClose = document.querySelector('#popup-close');
if (localStorage.getItem('email-aquired')) form.style.display = 'none';
form.addEventListener('submit', async (event) => {
event.preventDefault();
if (localStorage.getItem('email-aquired')) return;
const email = emailInput.value;
if (!email?.length) return
try {
const response = await fetch(form.action, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email: email })
});
if (response.ok) {
// email submitted successfully; reset form and show popup
form.reset();
localStorage.setItem('email-squired', true)
showPopup();
} else {
showPopup(true);
console.log(await response.text());
console.error('submission error');
}
} catch (error) {
// handle network errors
console.error('network error', error);
}
});
const showPopup = (isError) => {
if (isError) popup.classList.add('error');
popup.classList.remove('hidden');
// add a class to fade in the popup
popup.classList.add('show');
// automatically hide popup after 3 seconds
setTimeout(() => {
hidePopup();
}, 3000);
};
const hidePopup = () => {
popup.classList.remove('show');
// delay hiding the element until the transition ends
setTimeout(() => {
popup.classList.add('hidden');
}, 300);
};
// allow user to close popup manually
popupClose.addEventListener('click', hidePopup);