Complete missing UX flows and production integrity commands
All checks were successful
CI / ci (pull_request) Successful in 32s
All checks were successful
CI / ci (pull_request) Successful in 32s
This commit is contained in:
54
static/js/newsletter.js
Normal file
54
static/js/newsletter.js
Normal file
@@ -0,0 +1,54 @@
|
||||
(() => {
|
||||
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();
|
||||
})();
|
||||
Reference in New Issue
Block a user