55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
(() => {
|
|
const setMessage = (form, text) => {
|
|
const target = form.querySelector("[data-newsletter-message]");
|
|
if (target) {
|
|
target.textContent = text;
|
|
}
|
|
};
|
|
|
|
const bindNewsletterForms = () => {
|
|
const forms = document.querySelectorAll("form[data-newsletter-form]");
|
|
forms.forEach((form) => {
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const formData = new FormData(form);
|
|
try {
|
|
const response = await fetch(form.action, {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
if (!response.ok) {
|
|
setMessage(form, "Please enter a valid email.");
|
|
return;
|
|
}
|
|
setMessage(form, "Check your email to confirm your subscription.");
|
|
form.reset();
|
|
} catch (error) {
|
|
setMessage(form, "Subscription failed. Please try again.");
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
const bindCopyLink = () => {
|
|
const button = document.querySelector("[data-copy-link]");
|
|
if (!button) {
|
|
return;
|
|
}
|
|
button.addEventListener("click", async () => {
|
|
const url = button.getAttribute("data-copy-url");
|
|
if (!url) {
|
|
return;
|
|
}
|
|
try {
|
|
await navigator.clipboard.writeText(url);
|
|
button.textContent = "Copied";
|
|
} catch (error) {
|
|
button.textContent = "Copy failed";
|
|
}
|
|
});
|
|
};
|
|
|
|
bindNewsletterForms();
|
|
bindCopyLink();
|
|
})();
|