[object Object]

← back to Raffiawallcoverings

/api/facets total counts FILTERED list (respects q + aesthetic)

2aee93a567fc9ebd48c9bbb75c8eedced8c67899 · 2026-05-25 21:25:27 -0700 · Steve Abrams

Files touched

Diff

commit 2aee93a567fc9ebd48c9bbb75c8eedced8c67899
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon May 25 21:25:27 2026 -0700

    /api/facets total counts FILTERED list (respects q + aesthetic)
---
 server.js | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/server.js b/server.js
index 2b98c87..f55c879 100644
--- a/server.js
+++ b/server.js
@@ -172,12 +172,21 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  // total counts the FILTERED list (same filters as /api/products) so the
+  // header "N Patterns." reflects the current search/facet, not the catalog total.
+  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);
   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 }));

← 1023fdd sort + density: hydrate saved sort BEFORE first grid load (l  ·  back to Raffiawallcoverings  ·  add 404 catch-all guard (JSON for /api/*, text otherwise) — 82ccdbc →