[object Object]

← back to Retrowalls

/api/facets honors q+aesthetic+vendor filters via shared filterProducts() — counts + total reflect filtered set, not unfiltered catalog

216a456fd6043eb6938ba5599be1844f6cb4b6b9 · 2026-06-01 07:45:08 -0700 · Steve Abrams

Files touched

Diff

commit 216a456fd6043eb6938ba5599be1844f6cb4b6b9
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:45:08 2026 -0700

    /api/facets honors q+aesthetic+vendor filters via shared filterProducts() — counts + total reflect filtered set, not unfiltered catalog
---
 server.js | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/server.js b/server.js
index 16cc15a..0fc8689 100644
--- a/server.js
+++ b/server.js
@@ -162,15 +162,23 @@ require('./_universal-auth')(app, { siteName: "retrowalls" });
 
 app.use(express.static(path.join(__dirname, 'public'), { extensions: ['html'] }));
 
-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 agree on the result set.
+// `skip` excludes one dimension (used by facets so a chosen bucket doesn't
+// zero out its own sibling counts).
+function filterProducts({ q, aesthetic, vendor }, skip) {
   let list = PRODUCTS_NICHE;
   if (q) {
     const needle = 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, 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);
@@ -190,13 +198,18 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  const { q, aesthetic, vendor } = req.query;
   const aesthetics = {};
   const vendors = {};
-  for (const p of PRODUCTS_NICHE) {
+  // Per-dimension skip so each bucket's count reflects the OTHER active filters.
+  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 });
+  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 }));

← 4e776b9 noopener,noreferrer on sister-site map window.open(_blank)  ·  back to Retrowalls  ·  Remove Big Red widget — never UX-worked, collided with Help 92b10d4 →