[object Object]

← back to Museumwallpaper

Wire sort select to API and hydrate saved sort before first grid load

567fa5d6b6218214c6c850c4df5e3bd375374ff0 · 2026-05-19 08:04:27 -0700 · Steve Abrams

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

Files touched

Diff

commit 567fa5d6b6218214c6c850c4df5e3bd375374ff0
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Tue May 19 08:04:27 2026 -0700

    Wire sort select to API and hydrate saved sort before first grid load
    
    Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---
 public/index.html | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/public/index.html b/public/index.html
index 2cf7ea8..23d9bf2 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 && state.sort !== 'newest') params.set('sort', state.sort);
   let data;
   try {
     const r = await fetch('/api/products?' + params);
@@ -597,6 +598,19 @@ 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'));
 
+// Hydrate saved sort BEFORE the first grid load so the initial fetch is sorted.
+(function(){
+  try {
+    const sortSel = document.getElementById('sortSelect');
+    const KEY = location.hostname.replace(/\./g, '_') + '_sort';
+    const saved = localStorage.getItem(KEY);
+    if (saved) {
+      state.sort = saved;
+      if (sortSel) sortSel.value = saved;
+    }
+  } catch(e){}
+})();
+
 loadFacets();
 loadGridPage();
 </script>

← 8b88ab2 hero-4grid: relocate json from data/ to public/ for Express  ·  back to Museumwallpaper  ·  Fix /api/facets total to count niche-filtered products serve 3802fab →