[object Object]

← back to Hospitalitywallpaper

fix: sort select had no effect — pass state.sort to /api/products and re-render on restored sort

255937b47cc9ce0cd7e1049c9fb1f6d576536804 · 2026-05-18 20:38:25 -0700 · Steve Abrams

Files touched

Diff

commit 255937b47cc9ce0cd7e1049c9fb1f6d576536804
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon May 18 20:38:25 2026 -0700

    fix: sort select had no effect — pass state.sort to /api/products and re-render on restored sort
---
 public/index.html | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/public/index.html b/public/index.html
index fdcac35..985042b 100644
--- a/public/index.html
+++ b/public/index.html
@@ -427,7 +427,7 @@ textarea:focus-visible,
 </script>
 
 <script>
-const state = { q:'', facet:'all', page:1, pages:1, total:0, loading:false, exhausted:false };
+const state = { q:'', facet:'all', sort:'newest', page:1, pages:1, total:0, loading:false, exhausted:false };
 const LABELS = {"all":"All"};
 
 function escAttr(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, c => ({ '&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;' }[c])); }
@@ -540,6 +540,7 @@ async function loadGridPage() {
   const params = new URLSearchParams({ page:state.page, limit:24 });
   if (state.q) params.set('q', state.q);
   if (state.facet !== 'all') params.set('aesthetic', state.facet);
+  if (state.sort) params.set('sort', state.sort);
   let data;
   try {
     const r = await fetch('/api/products?' + params);
@@ -1002,7 +1003,15 @@ loadGridPage();
   if (!sortSel) return;
   const KEY = location.hostname.replace(/\./g, '_') + '_sort';
   const saved = localStorage.getItem(KEY);
-  if (saved) { try { sortSel.value = saved; } catch(e){} }
+  if (saved && [...sortSel.options].some(o => o.value === saved)) {
+    sortSel.value = saved;
+    // The initial grid load already ran with the default sort — re-render
+    // if the restored choice differs so the persisted sort actually applies.
+    if (typeof state !== 'undefined' && state.sort !== saved) {
+      state.sort = saved;
+      if (typeof resetGrid === 'function') resetGrid();
+    }
+  }
   if (typeof state !== 'undefined') state.sort = sortSel.value;
   sortSel.addEventListener('change', e => {
     const v = e.target.value;

← aa887e8 hero-4grid: relocate json from data/ to public/ for Express  ·  back to Hospitalitywallpaper  ·  untrack 19 stale .bak/.pre- snapshots (16 served from public f9ebe51 →