Skip to content

Commit 72a5168

Browse files
authored
fix: don't require recommend for brands plugin (#32)
If you currently run the demo without Recommend, the popup doesn't display because the recommend index isn't found. Since Recommend is complex to setup, I propose to not rely on it by default, so anyone can run the demo more easily. This PR uses the same approach than #31 to fetch the most popular `brand` facets from the search results. I'm also leaving the recommend code commented to give pointer about how to integrate Recommend. Note: since our demo index is not fully trained, this approach actually works better: - the current demo displays `Lacy-D`, `Arlene` and `Lacy-S` - locally I now get `Apple`, `Sony`, `Lacy-D` and `Garmin` --- SFCC-390
1 parent 0debc6d commit 72a5168

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

overrides/app/components/algolia/autocomplete/plugins/brandsPlugin.tsx

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/** @jsxRuntime classic */
22
/** @jsx React.createElement */
3-
import {AutocompletePlugin, getAlgoliaFacets} from '@algolia/autocomplete-js'
3+
import {AutocompletePlugin, getAlgoliaResults} from '@algolia/autocomplete-js'
44
import React, {createElement, Fragment} from 'react'
55
import {ALGOLIA_PRODUCTS_INDEX_NAME} from '../constants'
66
import {BrandHit} from './../types'
77
import {BrandItem} from './../components/BrandItem'
8-
import {recommendClient} from '../recommendClient'
8+
import {searchClient} from '../searchClient'
99

1010
/**
1111
* An Autocomplete Plugin that provides brand results from Algolia.
@@ -20,14 +20,44 @@ export const brandsPlugin: AutocompletePlugin<BrandHit, {}> = {
2020
{
2121
sourceId: 'brandsPlugin',
2222
async getItems() {
23-
var response = await recommendClient.getTrendingFacets([
24-
{
25-
indexName: ALGOLIA_PRODUCTS_INDEX_NAME,
26-
facetName: 'brand'
23+
return getAlgoliaResults({
24+
searchClient,
25+
queries: [
26+
{
27+
indexName: ALGOLIA_PRODUCTS_INDEX_NAME,
28+
query: '',
29+
params: {
30+
facets: ['brand'],
31+
hitsPerPage: 1,
32+
}
33+
}
34+
],
35+
transformResponse({ results }) {
36+
const brandFacets = results[0].facets['brand'];
37+
if (!brandFacets) {
38+
return [];
39+
}
40+
const res = [];
41+
for (let [key, value] of Object.entries(brandFacets)) {
42+
res.push({ facetValue: key, count: value });
43+
}
44+
// Sort brand facets by their count and return only the first 4
45+
res.sort((a, b) => {
46+
return b.count - a.count;
47+
});
48+
return [res.slice(0, 4)];
2749
},
28-
])
29-
30-
return response.results[0].hits
50+
})
51+
// Once you have Recommend setup, you may want to use it instead of `getAlgoliaResults`
52+
// to display the trending brands. This can be achieved like that:
53+
// var response = await recommendClient.getTrendingFacets([
54+
// {
55+
// indexName: ALGOLIA_PRODUCTS_INDEX_NAME,
56+
// facetName: 'brand',
57+
// },
58+
// ])
59+
//
60+
// return response.results[0].hits
3161
},
3262
renderer: {createElement, Fragment, render: () => {}},
3363
templates: {
@@ -41,4 +71,3 @@ export const brandsPlugin: AutocompletePlugin<BrandHit, {}> = {
4171
]
4272
}
4373
}
44-

0 commit comments

Comments
 (0)