[object Object]

← back to Agedwallpaper

Wire sort select to API: send sort param + hydrate saved value before first grid load

de7b563b769853ec628f5b0686db69cac7a92364 · 2026-05-19 07:55:56 -0700 · SteveStudio2

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

Files touched

Diff

commit de7b563b769853ec628f5b0686db69cac7a92364
Author: SteveStudio2 <steve@designerwallcoverings.com>
Date:   Tue May 19 07:55:56 2026 -0700

    Wire sort select to API: send sort param + hydrate saved value before first grid load
    
    Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---
 public/index.html | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/public/index.html b/public/index.html
index 114658d..87de996 100644
--- a/public/index.html
+++ b/public/index.html
@@ -430,7 +430,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])); }
@@ -543,6 +543,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);
@@ -600,6 +601,20 @@ 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 the first grid load so the
+// initial fetch already carries the persisted sort param.
+(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) {
+    const ok = Array.from(sortSel.options).some(o => o.value === saved);
+    if (ok) { sortSel.value = saved; state.sort = saved; }
+  }
+})();
+
 loadFacets();
 loadGridPage();
 </script>
@@ -900,14 +915,11 @@ loadGridPage();
 })();
 
 
-// DW STANDARD sort-select wire-up (added by fleet patcher)
+// DW STANDARD sort-select change handler (hydration runs earlier, pre-first-load)
 (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){}

← 65a8728 hero-4grid: relocate json from data/ to public/ for Express  ·  back to Agedwallpaper  ·  /api/facets total now counts the niche-filtered list actuall 4808043 →