Skip to content

Commit

Permalink
feat(facets): allow hiding values through renderingContent (#6239)
Browse files Browse the repository at this point in the history
[EMERCH-1438]
  • Loading branch information
thomasbritton committed Jun 17, 2024
1 parent 7b4cc3f commit b7eef90
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/algoliasearch-helper/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,10 @@ declare namespace algoliasearchHelper {
*/
values?: {
[facet: string]: {
/**
* Hide facet values
*/
hide?: string[];
/**
* Ordered facet values
*/
Expand Down
8 changes: 5 additions & 3 deletions packages/algoliasearch-helper/src/SearchResults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,9 @@ function vanillaSortFn(order, data) {
function sortViaFacetOrdering(facetValues, facetOrdering) {
var orderedFacets = [];
var remainingFacets = [];

var hide = facetOrdering.hide || [];
var order = facetOrdering.order || [];

/**
* an object with the keys being the values in order, the values their index:
* ['one', 'two'] -> { one: 0, two: 1 }
Expand All @@ -828,9 +829,10 @@ function sortViaFacetOrdering(facetValues, facetOrdering) {
facetValues.forEach(function (item) {
// hierarchical facets get sorted using their raw name
var name = item.path || item.name;
if (reverseOrder[name] !== undefined) {
var hidden = hide.indexOf(name) > -1;
if (!hidden && reverseOrder[name] !== undefined) {
orderedFacets[reverseOrder[name]] = item;
} else {
} else if (!hidden) {
remainingFacets.push(item);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,44 @@ describe('disjunctive facet', function () {
expect(facetValues).toEqual(expected);
});

test('facetOrdering: hiding facet value', function () {
var data = require('./getFacetValues/disjunctive.json');
var order = {
renderingContent: {
facetOrdering: {
values: {
brand: {
hide: ['Samsung'],
order: ['Samsung', 'Apple', 'Insignia™'],
},
},
},
},
};

var results = data.content.results.slice();
results[0] = Object.assign(order, results[0]);

var searchParams = new SearchParameters(data.state);
var result = new SearchResults(searchParams, results);

var facetValues = result.getFacetValues('brand', {
facetOrdering: true,
});

var expected = [
{ count: 386, isRefined: true, name: 'Apple', escapedValue: 'Apple' },
{
count: 551,
isRefined: false,
name: 'Insignia™',
escapedValue: 'Insignia™',
},
];

expect(facetValues).toEqual(expected);
});

test('without facetOrdering, nor sortBy', function () {
var data = require('./getFacetValues/disjunctive.json');
var order = {
Expand Down

0 comments on commit b7eef90

Please sign in to comment.