Files
season/sections/recently-viewed.liquid
T
2026-10-09 23:02:05 -06:00

70 lines
2.1 KiB
Plaintext

<section class="recent full-width" data-recent data-root="{{ routes.root_url }}">
<div class="recent__inner">
<h2 class="recent__title">{{ 'collection_page.recent' | t }}</h2>
<div class="recent__grid" data-recent-list>
{% if collection != blank %}
{% for product in collection.products limit: 4 %}
{% assign card_suffix = section.id | append: '-recent-' | append: forloop.index %}
{% render 'collection-card', product: product, form_suffix: card_suffix %}
{% endfor %}
{% endif %}
</div>
</div>
</section>
{% javascript %}
(() => {
if (window.__seasonRecent) return;
window.__seasonRecent = true;
const storageKey = 'seasons-recent';
const boot = async (root) => {
if (!root || root.dataset.ready === 'true') return;
root.dataset.ready = 'true';
let handles = [];
try {
const stored = JSON.parse(window.localStorage.getItem(storageKey));
if (Array.isArray(stored)) handles = stored.map(String).filter(Boolean);
} catch (error) {
handles = [];
}
handles = handles.slice(0, 4);
const list = root.querySelector('[data-recent-list]');
if (!list) return;
if (handles.length) {
const prefix = `${root.dataset.root || '/'}`.replace(/\/?$/, '/');
const cards = await Promise.all(handles.map(async (handle) => {
try {
const response = await fetch(`${prefix}products/${encodeURIComponent(handle)}?view=card`, { headers: { Accept: 'text/html' } });
if (!response.ok) return null;
const holder = document.createElement('div');
holder.innerHTML = (await response.text()).trim();
return holder.querySelector('.collection-card');
} catch (error) {
return null;
}
}));
const ready = cards.filter(Boolean);
if (ready.length) list.replaceChildren(...ready);
}
root.hidden = list.children.length === 0;
};
document.querySelectorAll('[data-recent]').forEach(boot);
})();
{% endjavascript %}
{% schema %}
{
"name": "t:collection_page.recent_name",
"tag": "section",
"class": "recent-section",
"settings": []
}
{% endschema %}