[object Object]

← back to Mylarwallpaper

/api/facets: total + aesthetics counts now reflect filtered list

e1f6a3cc84c598e75f8c5e9a23f4895f7e934872 · 2026-05-25 21:21:07 -0700 · Steve

Was always returning PRODUCTS_NICHE.length regardless of ?q= or
?aesthetic=, so the header count drifted whenever the user narrowed the
grid. Now applies the same filters /api/products does before counting.

Author: steve@designerwallcoverings.com

Files touched

Diff

commit e1f6a3cc84c598e75f8c5e9a23f4895f7e934872
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon May 25 21:21:07 2026 -0700

    /api/facets: total + aesthetics counts now reflect filtered list
    
    Was always returning PRODUCTS_NICHE.length regardless of ?q= or
    ?aesthetic=, so the header count drifted whenever the user narrowed the
    grid. Now applies the same filters /api/products does before counting.
    
    Author: steve@designerwallcoverings.com
---
 server.js | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/server.js b/server.js
index 4495047..5632d42 100644
--- a/server.js
+++ b/server.js
@@ -195,12 +195,22 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  // Apply the same q + aesthetic filters /api/products uses so `total` reflects
+  // the currently visible slice — not the entire niche universe. Otherwise the
+  // header count drifts from the grid count whenever the user filters.
+  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 }));

← cee03af wire api-vendor-redact middleware as first app.use()  ·  back to Mylarwallpaper  ·  scrub vendor leak from Ideas rail + add noreferrer to Facebo a2dc3c5 →