|
| 1 | +import { compositionClient } from '@algolia/composition'; |
| 2 | +import instantsearch from 'instantsearch.js'; |
| 3 | +import { |
| 4 | + configure, |
| 5 | + EXPERIMENTAL_autocomplete, |
| 6 | + hits, |
| 7 | + pagination, |
| 8 | +} from 'instantsearch.js/es/widgets'; |
| 9 | + |
| 10 | +import 'instantsearch.css/themes/satellite.css'; |
| 11 | + |
| 12 | +const searchClient = compositionClient( |
| 13 | + '9HILZG6EJK', |
| 14 | + '65b3e0bb064c4172c4810fb2459bebd1' |
| 15 | +); |
| 16 | + |
| 17 | +const compositionID = 'comp1774447423386___products'; |
| 18 | + |
| 19 | +const search = instantsearch({ |
| 20 | + // @ts-expect-error compositionClient return type doesn't fully match SearchClient yet |
| 21 | + searchClient, |
| 22 | + compositionID, |
| 23 | + insights: true, |
| 24 | +}); |
| 25 | + |
| 26 | +search.addWidgets([ |
| 27 | + configure({ hitsPerPage: 8 }), |
| 28 | + EXPERIMENTAL_autocomplete({ |
| 29 | + container: '#autocomplete', |
| 30 | + placeholder: 'Search for products', |
| 31 | + feeds: [ |
| 32 | + { |
| 33 | + feedID: 'products', |
| 34 | + templates: { |
| 35 | + header: (_, { html }) => html` |
| 36 | + <span class="ais-AutocompleteIndexHeaderTitle">Products</span> |
| 37 | + <span class="ais-AutocompleteIndexHeaderLine" /> |
| 38 | + `, |
| 39 | + item: ({ item, onSelect }, { html }) => html` |
| 40 | + <div onClick=${onSelect}>${(item as any).title}</div> |
| 41 | + `, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + feedID: 'Fashion', |
| 46 | + templates: { |
| 47 | + header: (_, { html }) => html` |
| 48 | + <span class="ais-AutocompleteIndexHeaderTitle">Fashion</span> |
| 49 | + <span class="ais-AutocompleteIndexHeaderLine" /> |
| 50 | + `, |
| 51 | + item: ({ item, onSelect }, { html }) => html` |
| 52 | + <div onClick=${onSelect}>${(item as any).name}</div> |
| 53 | + `, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + feedID: 'Amazon', |
| 58 | + templates: { |
| 59 | + header: (_, { html }) => html` |
| 60 | + <span class="ais-AutocompleteIndexHeaderTitle">Amazon</span> |
| 61 | + <span class="ais-AutocompleteIndexHeaderLine" /> |
| 62 | + `, |
| 63 | + item: ({ item, onSelect }, { html }) => html` |
| 64 | + <div onClick=${onSelect}>${(item as any).product_title}</div> |
| 65 | + `, |
| 66 | + }, |
| 67 | + }, |
| 68 | + ], |
| 69 | + showRecent: { |
| 70 | + templates: { |
| 71 | + header: (_, { html }) => html` |
| 72 | + <span class="ais-AutocompleteIndexHeaderTitle">Recent Searches</span> |
| 73 | + <span class="ais-AutocompleteIndexHeaderLine" /> |
| 74 | + `, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }), |
| 78 | + hits({ |
| 79 | + container: '#hits', |
| 80 | + templates: { |
| 81 | + item: (hit, { html, components }) => html` |
| 82 | + <article> |
| 83 | + <h1>${components.Highlight({ hit, attribute: 'title' })}</h1> |
| 84 | + <p>${(hit as any).author?.join?.(', ')}</p> |
| 85 | + </article> |
| 86 | + `, |
| 87 | + }, |
| 88 | + }), |
| 89 | + pagination({ |
| 90 | + container: '#pagination', |
| 91 | + }), |
| 92 | +]); |
| 93 | + |
| 94 | +search.start(); |
0 commit comments