[object Object]

← back to Corkwallcovering

fix /api/facets to count over the active filtered set (drill-down) via shared filterProducts() instead of the raw niche catalog

4d7b5b515d90c6f92088948dc509afdcce70363f · 2026-06-01 07:24:22 -0700 · Steve Abrams

Files touched

Diff

commit 4d7b5b515d90c6f92088948dc509afdcce70363f
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:24:22 2026 -0700

    fix /api/facets to count over the active filtered set (drill-down) via shared filterProducts() instead of the raw niche catalog
---
 public/index.html |  4 +++-
 server.js         | 29 ++++++++++++++++++++++-------
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/public/index.html b/public/index.html
index 1d9989e..9ba15f2 100644
--- a/public/index.html
+++ b/public/index.html
@@ -663,7 +663,9 @@ function openDetails(p) {
 async function loadFacets() {
   let f;
   try {
-    const r = await fetch('/api/facets');
+    const fp = new URLSearchParams();
+    if (state.q) fp.set('q', state.q);
+    const r = await fetch('/api/facets' + (fp.toString() ? '?' + fp : ''));
     if (!r.ok) throw new Error('HTTP ' + r.status);
     f = await r.json();
   } catch (e) { console.error('loadFacets failed:', e); return; }
diff --git a/server.js b/server.js
index 9d89ffd..dba1025 100644
--- a/server.js
+++ b/server.js
@@ -150,15 +150,24 @@ function sortProducts(list, mode) {
 }
 
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, sort = 'newest', page = 1, limit = 24 } = req.query;
+// Shared filter — applies the active query + facet selections to the niche
+// catalog. Used by both /api/products (the grid) and /api/facets (the chip
+// counts) so the counts always reflect the FILTERED result set, not the raw
+// catalog. `skip` lets each facet exclude its own dimension for drill-down.
+function filterProducts({ q, aesthetic, vendor } = {}, skip = null) {
   let list = PRODUCTS_NICHE;
   if (q) {
-    const needle = q.toLowerCase();
+    const needle = String(q).toLowerCase();
     list = list.filter(p => (p.title || '').toLowerCase().includes(needle) || (p.tags || []).some(t => t.toLowerCase().includes(needle)));
   }
-  if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
-  if (vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+  if (skip !== 'aesthetic' && aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
+  if (skip !== 'vendor' && vendor && vendor !== 'all') list = list.filter(p => p.vendor === vendor);
+  return list;
+}
+
+app.get('/api/products', (req, res) => {
+  const { q, aesthetic, vendor, sort = 'newest', page = 1, limit = 24 } = req.query;
+  let list = filterProducts({ q, aesthetic, vendor });
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page) || 1);
@@ -177,12 +186,18 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  const { q, aesthetic, vendor } = req.query;
+  // Each facet dimension is counted over the set filtered by the OTHER active
+  // selections (drill-down), so counts reflect what selecting that value would
+  // yield rather than the unfiltered catalog. `total` honors ALL active filters.
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  for (const p of filterProducts({ q, aesthetic, vendor }, 'aesthetic')) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
+  }
+  for (const p of filterProducts({ q, aesthetic, vendor }, 'vendor')) {
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  res.json({ aesthetics, vendors, total: filterProducts({ q, aesthetic, vendor }).length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← a99cbe8 fix: remove public/index.html.bak-* leak and add *.bak-* to  ·  back to Corkwallcovering  ·  add 301 redirect from legacy /*.html to clean URLs (before e 4f70040 →