[object Object]

← back to Naturalwallcoverings

/api/facets: count FILTERED list (honor q + aesthetic) instead of always returning unfiltered totals

b6cf30f8c39e0483579784e0be0b28be091a7c57 · 2026-05-25 21:23:31 -0700 · Steve

Files touched

Diff

commit b6cf30f8c39e0483579784e0be0b28be091a7c57
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon May 25 21:23:31 2026 -0700

    /api/facets: count FILTERED list (honor q + aesthetic) instead of always returning unfiltered totals
---
 server.js | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/server.js b/server.js
index 1279be8..131a117 100644
--- a/server.js
+++ b/server.js
@@ -185,12 +185,23 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  // FILTERED facets — counts reflect the same filters /api/products applies
+  // (q + aesthetic) so the UI shows accurate per-bucket counts as the user
+  // narrows. `vendor` is ignored at the facet level (vendor names are
+  // redacted upstream by the api-vendor-redact middleware).
+  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 => String(t).toLowerCase().includes(needle)));
+  }
+  if (aesthetic && aesthetic !== 'all') list = list.filter(p => p.aesthetic === aesthetic);
   const aesthetics = {}; const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  for (const p of list) {
     aesthetics[p.aesthetic] = (aesthetics[p.aesthetic] || 0) + 1;
     vendors[p.vendor] = (vendors[p.vendor] || 0) + 1;
   }
-  res.json({ aesthetics, vendors, total: PRODUCTS_NICHE.length });
+  res.json({ aesthetics, vendors, total: list.length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← 6b2e780 hydrate saved sort BEFORE first grid load (was applied only  ·  back to Naturalwallcoverings  ·  404-guard .bak/.pre-* paths (defense-in-depth above .gitigno da46879 →