Open History to see the last commit
1import { useInfiniteQuery } from '@tanstack/solid-query'
2import { For, Match, Show, Switch } from 'solid-js'
3import { productsQueries } from './services/products/queries'
4β
5type Product = {
6 name: string
7 shortDescription: string
8 salePrice: number
9}
10β
11type SearchResultsProps = {
12 query: string
13}
14β
15export default function SearchResults(props: SearchResultsProps) {
16 const query = useInfiniteQuery(() => ({
17 ...productsQueries.search<Product>({
18 indexName: 'bestbuy',
19 query: props.query,
20 hitsPerPage: 5,
21 }),
22 staleTime: 30 * 1000, // 30s
23 gcTime: 15 * 60 * 1000, // 15m
24 }))
25β
26 const hits = () => query.data?.pages.flatMap((page) => page.hits)
27β
28 return (
29 <Show when={props.query}>
30 <Switch>
31 <Match when={query.isLoading}>
32 <div class="loading">Loading...</div>
33 </Match>
34 <Match when={query.status === 'error'}>
35 <div>Error: {query.error?.message}</div>
36 </Match>
37 <Match when={true}>
38 <div>
39 <div class="search-status">
40 Status: {query.status}{' '}
41 <Show when={query.isFetching}>
42 <span>fetching...</span>
43 </Show>
44 </div>
45 <div>
46 <div class="search-result">
47 <Show
48 when={hits()?.length}
49 fallback={<h3>No products found!</h3>}
50 >
51 <For each={hits()}>
52 {(product) => (
53 <li class="product">
54 <span class="product-name">{product.name}</span>
55 <Show when={product.shortDescription}>
56 <br />
57 <span class="product-description">
58 {product.shortDescription}
59 </span>
60 </Show>
61 <br />
62 <span class="product-price">${product.salePrice}</span>
63 </li>
64 )}
65 </For>
66 </Show>
67 </div>
68 <Show when={query.hasNextPage}>
69 <button
70 type="button"
71 class="search-more"
72 onClick={() => query.fetchNextPage()}
73 disabled={query.isFetching}
74 >
75 more
76 </button>
77 </Show>
78 <Show when={query.isFetchingNextPage}>
79 <div class="search-status">Fetching next page...</div>
80 </Show>
81 </div>
82 </div>
83 </Match>
84 </Switch>
85 </Show>
86 )
87}