File size: 2,834 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import React from 'react';
import {
Configure,
Highlight,
Hits,
Index,
InstantSearch,
InstantSearchSSRProvider,
Pagination,
RefinementList,
SearchBox,
} from 'react-instantsearch-hooks-web';
// because this is ran on node without type: "module" set in the package.json
// we need to use commonjs instead of esm.
// If you use ESM in Node, you can rely on these import statements instead:
// import { simple } from 'instantsearch.js/es/lib/stateMappings';
// import { history } from 'instantsearch.js/es/lib/routers';
import { simple } from 'instantsearch.js/cjs/lib/stateMappings';
import { history } from 'instantsearch.js/cjs/lib/routers';
import { searchClient } from './searchClient';
function Hit({ hit }) {
return <Highlight hit={hit} attribute="name" />;
}
function App({ serverState, location }) {
return (
<InstantSearchSSRProvider {...serverState}>
<InstantSearch
indexName="instant_search"
searchClient={searchClient}
routing={{
stateMapping: simple(),
router: history({
getLocation() {
if (typeof window === 'undefined') {
return location;
}
return window.location;
},
}),
}}
>
<Configure hitsPerPage={10} />
<div
style={{
display: 'grid',
alignItems: 'flex-start',
gridTemplateColumns: '200px 1fr',
gap: '0.5rem',
maxWidth: 1000,
margin: 'auto',
}}
>
<div>
<RefinementList
attribute="brand"
searchable={true}
searchablePlaceholder="Search brands"
showMore={true}
/>
</div>
<div style={{ display: 'grid', gap: '0.5rem' }}>
<SearchBox placeholder="Search" />
<Hits hitComponent={Hit} />
<Index indexName="instant_search_price_asc">
<div
style={{
display: 'grid',
alignItems: 'flex-start',
gridTemplateColumns: '200px 1fr',
gap: '0.5rem',
}}
>
<div>
<RefinementList
attribute="categories"
searchable={true}
searchablePlaceholder="Search categories"
showMore={true}
/>
</div>
<div style={{ display: 'grid', gap: '0.5rem' }}>
<Hits hitComponent={Hit} />
</div>
</div>
</Index>
<Pagination />
</div>
</div>
</InstantSearch>
</InstantSearchSSRProvider>
);
}
export default App;
|