← back to Greenwallcoverings
fix product sort: pass sort param to /api/products, honor saved sort on load, init state.sort
61905165d00c82ef0d47cefc99c9b8b837bc64c2 · 2026-05-18 20:42:22 -0700 · Steve Abrams
Files touched
M public/index.htmlM server.js
Diff
commit 61905165d00c82ef0d47cefc99c9b8b837bc64c2
Author: Steve Abrams <steve@designerwallcoverings.com>
Date: Mon May 18 20:42:22 2026 -0700
fix product sort: pass sort param to /api/products, honor saved sort on load, init state.sort
---
public/index.html | 11 +++++++++--
server.js | 2 +-
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/public/index.html b/public/index.html
index ad50dfb..9d43d99 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 => ({ '&':'&','<':'<','>':'>','"':'"',"'":''' }[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);
@@ -907,7 +908,13 @@ loadGridPage();
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;
+ if (typeof state !== 'undefined') {
+ const prev = state.sort;
+ state.sort = sortSel.value;
+ // The initial loadGridPage() ran before this wire-up; re-sync the grid
+ // if a non-default sort was restored from localStorage.
+ if (state.sort !== prev && typeof resetGrid === 'function') resetGrid();
+ }
sortSel.addEventListener('change', e => {
const v = e.target.value;
try { localStorage.setItem(KEY, v); } catch(e){}
diff --git a/server.js b/server.js
index 06a2dd1..47a3eb3 100644
--- a/server.js
+++ b/server.js
@@ -170,7 +170,7 @@ app.get('/api/facets', (req, res) => {
aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
}
- res.json({ aesthetics, vendors, total: PRODUCTS.length });
+ res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
});
app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));
← 6454f22 hero-4grid: relocate json from data/ to public/ for Express
·
back to Greenwallcoverings
·
untrack 19 stale .bak/.pre- snapshots, extend .gitignore to c0c27c8 →