-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(algolia): add
getAlgoliaFacetHits
preset (#451)
- Loading branch information
1 parent
6b0c0ca
commit 8876fd3
Showing
18 changed files
with
530 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** @jsx h */ | ||
import { | ||
AutocompletePlugin, | ||
getAlgoliaFacetHits, | ||
highlightHit, | ||
} from '@algolia/autocomplete-js'; | ||
import { Hit } from '@algolia/client-search'; | ||
import { SearchClient } from 'algoliasearch/lite'; | ||
import { h, Fragment } from 'preact'; | ||
|
||
type CategoryItem = { | ||
label: string; | ||
count: number; | ||
}; | ||
|
||
type CreateCategoriesPluginProps = { | ||
searchClient: SearchClient; | ||
}; | ||
|
||
export function createCategoriesPlugin({ | ||
searchClient, | ||
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryItem, undefined> { | ||
return { | ||
getSources({ query }) { | ||
return [ | ||
{ | ||
sourceId: 'categoriesPlugin', | ||
getItems() { | ||
return getAlgoliaFacetHits({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName: 'instant_search', | ||
params: { | ||
facetName: 'categories', | ||
facetQuery: query, | ||
maxFacetHits: query ? 3 : 10, | ||
}, | ||
}, | ||
], | ||
}); | ||
}, | ||
templates: { | ||
header() { | ||
return ( | ||
<Fragment> | ||
<span className="aa-SourceHeaderTitle">Categories</span> | ||
<div className="aa-SourceHeaderLine" /> | ||
</Fragment> | ||
); | ||
}, | ||
item({ item }) { | ||
return ( | ||
<Fragment> | ||
<div className="aa-ItemIcon aa-ItemIcon--no-border"> | ||
<svg | ||
viewBox="0 0 24 24" | ||
width="18" | ||
height="18" | ||
fill="none" | ||
stroke="currentColor" | ||
strokeWidth="2" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
> | ||
<path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" /> | ||
<polyline points="3.27 6.96 12 12.01 20.73 6.96" /> | ||
<line x1="12" y1="22.08" x2="12" y2="12" /> | ||
</svg> | ||
</div> | ||
<div className="aa-ItemContent"> | ||
<div className="aa-ItemContentTitle"> | ||
{highlightHit<Hit<CategoryItem>>({ | ||
hit: item as any, | ||
attribute: 'label', | ||
})} | ||
</div> | ||
</div> | ||
</Fragment> | ||
); | ||
}, | ||
}, | ||
}, | ||
]; | ||
}, | ||
}; | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/autocomplete-js/src/__tests__/getAlgoliaFacetHits.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import algoliaPreset from '@algolia/autocomplete-preset-algolia'; | ||
|
||
import { createSearchClient } from '../../../../test/utils'; | ||
import { getAlgoliaFacetHits } from '../getAlgoliaFacetHits'; | ||
import { version } from '../version'; | ||
|
||
jest.mock('@algolia/autocomplete-preset-algolia', () => { | ||
const module = jest.requireActual('@algolia/autocomplete-preset-algolia'); | ||
|
||
return { | ||
...module, | ||
getAlgoliaFacetHits: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('getAlgoliaFacetHits', () => { | ||
test('forwards params to the preset function', () => { | ||
const searchClient = createSearchClient(); | ||
|
||
getAlgoliaFacetHits({ searchClient, queries: [] }); | ||
|
||
expect(algoliaPreset.getAlgoliaFacetHits).toHaveBeenCalledTimes(1); | ||
expect(algoliaPreset.getAlgoliaFacetHits).toHaveBeenCalledWith({ | ||
searchClient, | ||
queries: [], | ||
userAgents: [{ segment: 'autocomplete-js', version }], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
getAlgoliaFacetHits as getAlgoliaFacetHitsOriginal, | ||
SearchForFacetValuesParams, | ||
} from '@algolia/autocomplete-preset-algolia'; | ||
|
||
import { version } from './version'; | ||
|
||
export function getAlgoliaFacetHits({ | ||
searchClient, | ||
queries, | ||
}: Pick<SearchForFacetValuesParams, 'searchClient' | 'queries'>) { | ||
return getAlgoliaFacetHitsOriginal({ | ||
searchClient, | ||
queries, | ||
userAgents: [{ segment: 'autocomplete-js', version }], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type UserAgent = { segment: string; version?: string }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.