[object Object]

← back to Embroideredwallpaper

facets: counts honor q/aesthetic/vendor filters (shared filterProducts, per-dimension skip)

c58134a50940fd1eb946561ee14cbb6c7c184fad · 2026-06-01 07:25:10 -0700 · Steve

Files touched

Diff

commit c58134a50940fd1eb946561ee14cbb6c7c184fad
Author: Steve <steve@designerwallcoverings.com>
Date:   Mon Jun 1 07:25:10 2026 -0700

    facets: counts honor q/aesthetic/vendor filters (shared filterProducts, per-dimension skip)
---
 server.js | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/server.js b/server.js
index 517922f..8de5722 100644
--- a/server.js
+++ b/server.js
@@ -162,15 +162,23 @@ require("./_universal-contact")(app, cfg.contact);
 require("./_universal-auth")(app, cfg.auth);
 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 product filter — honors q / aesthetic / vendor. `skip` lets the facet
+// endpoint exclude one dimension so each facet's counts reflect the rest of the
+// active query (the standard cross-filtered facet model).
+function filterProducts({ q, aesthetic, vendor }, skip) {
   let list = PRODUCTS_NICHE;
   if (q) {
-    const needle = q.toLowerCase();
+    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 (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,12 +198,18 @@ app.get('/api/sliders', (req, res) => {
 });
 
 app.get('/api/facets', (req, res) => {
+  const { q, aesthetic, vendor } = req.query;
+  // Each facet dimension counts against the result set filtered by the OTHER
+  // active dimensions (skip self) so counts reflect what's actually selectable.
   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: PRODUCTS_NICHE.length });
+  // `total` = fully-filtered result count (all active dimensions applied).
+  res.json({ aesthetics, vendors, total: filterProducts({ q, aesthetic, vendor }).length });
 });
 
 app.get('/api/health', (req, res) => res.json({ status: 'ok', count: PRODUCTS_NICHE.length, dropped: DROPPED }));

← b8c8930 feat(fleet): theme1/theme2 query-param-gated toggle on catal  ·  back to Embroideredwallpaper  ·  noopener: sister-site map window.open gets noopener,noreferr e4914e2 →