From 9973065fc35114a58af5b4556eb0fd630aa42171 Mon Sep 17 00:00:00 2001 From: Sylvain Bellone Date: Mon, 9 Dec 2024 14:47:11 +0100 Subject: [PATCH] fix(frontend): hidding logic wasn't working for hierachicalFacets panel (#209) InstantSearch panel permits to implement a `hidden` function to determine if the panel should be hidden. It wasn't working for hierarchicalFacets, and a workaround had been done in https://github.com/algolia/algoliasearch-sfcc-b2c/pull/146 to try to replicate the logic. The problem is that when the panel is hidden by this workaround, if the selected filters are later reset, **it's not becoming visible anymore**. ### Changes - fix the `hidden` function: the hierarchicalFacets are always present in `options.results.hierarchicalFacets`. But when there are no results matching, the `data` field is `null`. Checking the presence of `data` fixes the behaviour - remove the workaround --- SFCC-410 --- .../static/default/js/algolia/instantsearch-config.js | 11 ++--------- .../static/default/js/algolia/instantsearch-config.js | 9 ++------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/cartridges/int_algolia_controllers/cartridge/static/default/js/algolia/instantsearch-config.js b/cartridges/int_algolia_controllers/cartridge/static/default/js/algolia/instantsearch-config.js index 45566797..d8c3c833 100644 --- a/cartridges/int_algolia_controllers/cartridge/static/default/js/algolia/instantsearch-config.js +++ b/cartridges/int_algolia_controllers/cartridge/static/default/js/algolia/instantsearch-config.js @@ -316,13 +316,6 @@ function enableInstantSearch(config) { search.start(); - search.on('render', function () { - var emptyFacetSelector = '.ais-HierarchicalMenu--noRefinement'; - $(emptyFacetSelector).each(function () { - $(this).parents().eq(2).hide(); - }); - }); - /** * Generates a menu with the Panel widget * @param {Object} options Options object @@ -369,8 +362,8 @@ function enableInstantSearch(config) { return instantsearch.widgets.panel({ hidden: function(options) { var facets = [].concat(options.results.disjunctiveFacets, options.results.hierarchicalFacets) - var facet = facets.find(function(facet) { return facet.name === attribute }); // eslint-disable-line no-shadow - var facetExists = !!facet; + var attributeFacet = facets.find(function(facet) { return facet.name === attribute }); + var facetExists = attributeFacet && attributeFacet.data; return !facetExists; // hides panel if not facets selectable }, templates: { diff --git a/cartridges/int_algolia_sfra/cartridge/static/default/js/algolia/instantsearch-config.js b/cartridges/int_algolia_sfra/cartridge/static/default/js/algolia/instantsearch-config.js index 79119d34..1597a96d 100644 --- a/cartridges/int_algolia_sfra/cartridge/static/default/js/algolia/instantsearch-config.js +++ b/cartridges/int_algolia_sfra/cartridge/static/default/js/algolia/instantsearch-config.js @@ -547,11 +547,6 @@ function enableInstantSearch(config) { updateAllProductPrices(); }); } - - var emptyFacetSelector = '.ais-HierarchicalMenu--noRefinement'; - $(emptyFacetSelector).each(function () { - $(this).parents().eq(2).hide(); - }); }); /** @@ -600,8 +595,8 @@ function enableInstantSearch(config) { return instantsearch.widgets.panel({ hidden: function(options) { var facets = [].concat(options.results.disjunctiveFacets, options.results.hierarchicalFacets) - var facet = facets.find(function(facet) { return facet.name === attribute }); // eslint-disable-line no-shadow - var facetExists = !!facet; + var attributeFacet = facets.find(function(facet) { return facet.name === attribute }); + var facetExists = attributeFacet && attributeFacet.data; return !facetExists; // hides panel if not facets selectable }, templates: {