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.
This commit is contained in:
parent
0011fdf76a
commit
0c615043f2
3 changed files with 47 additions and 31 deletions
|
@ -1,10 +1,21 @@
|
||||||
|
function setFormStateCookie(value) {
|
||||||
|
const thirtyDays = 60 * 60 * 24 * 30;
|
||||||
|
document.cookie = `DTIItemPageUserListsFormState=open;max-age=${thirtyDays}`;
|
||||||
|
}
|
||||||
|
|
||||||
const headers = document.querySelectorAll(".item-header");
|
const headers = document.querySelectorAll(".item-header");
|
||||||
for (const header of headers) {
|
for (const header of headers) {
|
||||||
try {
|
try {
|
||||||
const form = header.querySelector(".user-lists-form");
|
const form = header.querySelector(".user-lists-form");
|
||||||
const opener = header.querySelector(".user-lists-form-opener");
|
const opener = header.querySelector(".user-lists-form-opener");
|
||||||
opener.addEventListener("click", (event) => {
|
opener.addEventListener("click", (event) => {
|
||||||
form.toggleAttribute("hidden");
|
if (form.hasAttribute("hidden")) {
|
||||||
|
form.removeAttribute("hidden");
|
||||||
|
setFormStateCookie("open");
|
||||||
|
} else {
|
||||||
|
form.setAttribute("hidden", "");
|
||||||
|
setFormStateCookie("closed");
|
||||||
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -118,5 +118,9 @@ module ItemsHelper
|
||||||
human_name = pet_type.species.name.humanize
|
human_name = pet_type.species.name.humanize
|
||||||
image_tag(src, :alt => human_name, :title => human_name)
|
image_tag(src, :alt => human_name, :title => human_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def item_header_user_lists_form_state
|
||||||
|
cookies.fetch("DTIItemPageUserListsFormState", "closed")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,8 @@
|
||||||
user_closet_hangers_path(current_user),
|
user_closet_hangers_path(current_user),
|
||||||
class: 'user-lists-form-opener'
|
class: 'user-lists-form-opener'
|
||||||
|
|
||||||
= form_tag update_quantities_user_item_closet_hangers_path(user_id: current_user, item_id: item), method: :put, class: 'user-lists-form', hidden: true do
|
- if user_signed_in?
|
||||||
|
= form_tag update_quantities_user_item_closet_hangers_path(user_id: current_user, item_id: item), method: :put, class: 'user-lists-form', hidden: item_header_user_lists_form_state != "open" do
|
||||||
%h3
|
%h3
|
||||||
= t 'items.show.closet_hangers.header_html',
|
= t 'items.show.closet_hangers.header_html',
|
||||||
user_items_link: link_to(t('your_items'),
|
user_items_link: link_to(t('your_items'),
|
||||||
|
|
Loading…
Reference in a new issue