impress/app/assets/javascripts/items/item_header.js
Emi Matchu 0c615043f2 Persist item user lists form open/closed state across pageloads
Using good ol'-fashioned cookies! The JS sets it, and then Rails reads
it on pageload. That way, there's no flash of content for it to load in
after JS loads.
2024-01-23 04:30:23 -08:00

24 lines
752 B
JavaScript

function setFormStateCookie(value) {
const thirtyDays = 60 * 60 * 24 * 30;
document.cookie = `DTIItemPageUserListsFormState=open;max-age=${thirtyDays}`;
}
const headers = document.querySelectorAll(".item-header");
for (const header of headers) {
try {
const form = header.querySelector(".user-lists-form");
const opener = header.querySelector(".user-lists-form-opener");
opener.addEventListener("click", (event) => {
if (form.hasAttribute("hidden")) {
form.removeAttribute("hidden");
setFormStateCookie("open");
} else {
form.setAttribute("hidden", "");
setFormStateCookie("closed");
}
event.preventDefault();
});
} catch (error) {
console.error(`Error applying dialog behavior to item header:`, error);
}
}