[object Object]

← back to Textilewallpaper

/api/facets drill-down counts via shared filterProducts() with per-dimension skip

8301be55c4032c84bbeaa990eec71db9c30804c7 · 2026-06-01 07:54:07 -0700 · Steve

Files touched

Diff

commit 8301be55c4032c84bbeaa990eec71db9c30804c7
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:54:07 2026 -0700

    /api/facets drill-down counts via shared filterProducts() with per-dimension skip
---
 server.js | 40 +++++++++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git a/server.js b/server.js
index a054cc4..57258e2 100644
--- a/server.js
+++ b/server.js
@@ -169,15 +169,23 @@ app.use((req, res, next) => {
 });
 app.use(express.static(path.join(__dirname, 'public')));
 
-app.get('/api/products', (req, res) => {
-  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+// Shared filter so /api/products and /api/facets stay in lock-step. `skip`
+// names one dimension to leave un-applied — used by /api/facets to count each
+// facet over the set narrowed by the OTHER active filters (drill-down counts).
+function filterProducts({ q, aesthetic, vendor } = {}, skip) {
   let list = PRODUCTS_NICHE;
-  if (q) {
-    const needle = q.toLowerCase();
+  if (q && skip !== 'q') {
+    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 (aesthetic && aesthetic !== 'all' && skip !== 'aesthetic') list = list.filter(p => p.aesthetic === aesthetic);
+  if (vendor && vendor !== 'all' && skip !== 'vendor') list = list.filter(p => p.vendor === vendor);
+  return list;
+}
+
+app.get('/api/products', (req, res) => {
+  const { q, aesthetic, vendor, page = 1, limit = 24, sort = 'newest'} = req.query;
+  let list = filterProducts({ q, aesthetic, vendor });
   list = sortProducts(list, sort);
   const total = list.length;
   const pageNum = Math.max(1, parseInt(page) || 1);
@@ -197,21 +205,19 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
-  const { q, aesthetic } = req.query;
-  let list = PRODUCTS_NICHE;
-  if (q) {
-    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);
-  // Aesthetic facet counts always reflect the FULL niche set so the filter rail
-  // stays useful as users navigate; `total` reflects the filtered list count.
+  const { q, aesthetic, vendor } = req.query;
+  // Drill-down counts: each facet dimension is counted over the result set
+  // narrowed by the OTHER active filters (skip its own dimension) so selecting
+  // one aesthetic doesn't zero out its sibling buckets. `total` = fully filtered.
   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: list.length });
+  const total = filterProducts({ q, aesthetic, vendor }).length;
+  res.json({ aesthetics, vendors, total });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 6ae80a7 fix: remove leaked .bak file from public/ and patch .bak-* b  ·  back to Textilewallpaper  ·  Remove Big Red widget — never UX-worked, collided with Help 24abcd6 →