[object Object]

← back to Wallpapercanada

sort: honor sort param — send state.sort to /api/products and hydrate saved value before first grid load

d03187df73e9da515df17ba2361b5bdce5d79563 · 2026-05-19 08:13:48 -0700 · Steve Abrams

loadGridPage never sent the sort param so the server's sort was always 'newest'. The DW-standard wire-up also ran after the initial loadGridPage(), so a saved sort was ignored on first paint. Moved hydration before first load and removed the duplicate change-listener block.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit d03187df73e9da515df17ba2361b5bdce5d79563
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Tue May 19 08:13:48 2026 -0700

    sort: honor sort param — send state.sort to /api/products and hydrate saved value before first grid load
    
    loadGridPage never sent the sort param so the server's sort was always 'newest'. The DW-standard wire-up also ran after the initial loadGridPage(), so a saved sort was ignored on first paint. Moved hydration before first load and removed the duplicate change-listener block.
    
    Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---
 public/index.html | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/public/index.html b/public/index.html
index a05b6f6..9b37a8a 100644
--- a/public/index.html
+++ b/public/index.html
@@ -561,7 +561,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])); }
@@ -682,6 +682,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 && state.sort !== 'newest') params.set('sort', state.sort);
   let data;
   try {
     const r = await fetch('/api/products?' + params);
@@ -739,6 +740,27 @@ function setTheme(t){ document.documentElement.dataset.theme = t; try { localSto
 setTheme(document.documentElement.dataset.theme || 'light');
 tb.addEventListener('click', () => setTheme(document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark'));
 
+// DW STANDARD: hydrate saved sort BEFORE first grid load so the saved value is honored.
+(function(){
+  const sortSel = document.getElementById('sortSelect');
+  if (!sortSel) return;
+  const KEY = location.hostname.replace(/\./g, '_') + '_sort';
+  let saved;
+  try { saved = localStorage.getItem(KEY); } catch(e){}
+  if (saved) {
+    if ([...sortSel.options].some(o => o.value === saved)) {
+      sortSel.value = saved;
+      state.sort = saved;
+    }
+  }
+  sortSel.addEventListener('change', e => {
+    const v = e.target.value;
+    try { localStorage.setItem(KEY, v); } catch(e){}
+    state.sort = v;
+    resetGrid();
+  });
+})();
+
 loadFacets();
 loadGridPage();
 </script>
@@ -1131,23 +1153,6 @@ loadGridPage();
 })();
 
 
-// DW STANDARD sort-select wire-up (added by fleet patcher)
-(function(){
-  const sortSel = document.getElementById('sortSelect');
-  if (!sortSel) return;
-  const KEY = location.hostname.replace(/\./g, '_') + '_sort';
-  const saved = localStorage.getItem(KEY);
-  if (saved) { try { sortSel.value = saved; } catch(e){} }
-  if (typeof state !== 'undefined') state.sort = sortSel.value;
-  sortSel.addEventListener('change', e => {
-    const v = e.target.value;
-    try { localStorage.setItem(KEY, v); } catch(e){}
-    if (typeof state !== 'undefined') state.sort = v;
-    if (typeof resetGrid === 'function') resetGrid();
-    else location.reload();
-  });
-})();
-
 // graphics-loop pass 11: hydrate Ideas rails from /api/products
 (async function hydrateIdeas() {
   try {

← 131605d hero-4grid: relocate json from data/ to public/ for Express  ·  back to Wallpapercanada  ·  chore: untrack 18 editor snapshot files (*.bak / *.pre-*) an 56a7fe7 →