Open History to see the last commit
1import useAlgolia from './useAlgolia'
2β
3type Product = {
4 name: string
5 shortDescription: string
6 salePrice: number
7}
8β
9type SearchResultsProps = {
10 query: string
11}
12β
13export default function SearchResults({ query = '' }: SearchResultsProps) {
14 const {
15 hits,
16 isLoading,
17 isFetching,
18 status,
19 hasNextPage,
20 isFetchingNextPage,
21 fetchNextPage,
22 } = useAlgolia<Product>({
23 indexName: 'bestbuy',
24 query,
25 hitsPerPage: 5,
26 staleTime: 1000 * 30, // 30s
27 gcTime: 1000 * 60 * 15, // 15m
28 enabled: !!query,
29 })
30β
31 if (!query) return null
32β
33 if (isLoading) return <div className="loading">Loading...</div>
34β
35 return (
36 <div>
37 <div className="search-status">
38 Satus: {status} {isFetching && <span>fetching...</span>}
39 </div>
40 <div>
41 <div className="search-result">
42 {hits && hits.length > 0 ? (
43 hits.map((product) => (
44 <li key={product.objectID} className="product">
45 <span className="product-name">{product.name}</span>
46 {product.shortDescription && (
47 <>
48 <br />
49 <span className="product-description">
50 {product.shortDescription}
51 </span>
52 </>
53 )}
54 <br />
55 <span className="product-price">${product.salePrice}</span>
56 </li>
57 ))
58 ) : (
59 <h3>No products found!</h3>
60 )}
61 </div>
62 {hasNextPage && (
63 <div className="search-more" onClick={() => fetchNextPage()}>
64 more
65 </div>
66 )}
67 {isFetchingNextPage && (
68 <div className="search-status">Fetching next page...</div>
69 )}
70 </div>
71 </div>
72 )
73}