From 5daaa5ea78ed3a6e5000853c7de36cd193a62da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 18 May 2023 13:44:59 -0400 Subject: [PATCH] fix: edge case in category filter selecting a category and then clicking on all categories was breaking selection icons --- public/src/modules/categoryFilter.js | 14 +++++++++++--- src/controllers/search.js | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/public/src/modules/categoryFilter.js b/public/src/modules/categoryFilter.js index 284c735448..271bf83448 100644 --- a/public/src/modules/categoryFilter.js +++ b/public/src/modules/categoryFilter.js @@ -70,6 +70,9 @@ define('categoryFilter', ['categorySearch', 'api', 'hooks'], function (categoryS const icon = categoryEl.find('[component="category/select/icon"]'); if (cid !== 'all') { + if (selectedCids.includes('all')) { + selectedCids.splice(selectedCids.indexOf('all'), 1); + } if (selectedCids.includes(cid)) { selectedCids.splice(selectedCids.indexOf(cid), 1); } else { @@ -78,14 +81,19 @@ define('categoryFilter', ['categorySearch', 'api', 'hooks'], function (categoryS selectedCids.sort(function (a, b) { return a - b; }); + icon.toggleClass('invisible'); + listEl.find('[data-cid="all"] i').toggleClass('invisible', !!selectedCids.length); + if (!selectedCids.length) { + selectedCids.push('all'); + } } else { el.find('[component="category/select/icon"]').addClass('invisible'); - selectedCids = [cid]; + listEl.find('[data-cid="all"] i').removeClass('invisible'); + selectedCids = ['all']; } options.selectedCids = selectedCids; - listEl.find('[data-cid="all"] i').toggleClass('invisible', !!selectedCids.length); - icon.toggleClass('invisible'); + if (options.onSelect) { options.onSelect({ cid: cid, selectedCids: selectedCids.slice() }); } diff --git a/src/controllers/search.js b/src/controllers/search.js index 49fd58a6dc..a12c433edd 100644 --- a/src/controllers/search.js +++ b/src/controllers/search.js @@ -129,7 +129,8 @@ searchController.search = async function (req, res, next) { ), }, categories: { - active: !!(Array.isArray(data.categories) && data.categories.length), + active: !!(Array.isArray(data.categories) && data.categories.length && + (data.categories.length > 1 || data.categories[0] !== 'all')), label: await buildSelectedCategoryLabel(searchData.selectedCids), }, };