forked from crowetic/commerce
* Minimal list/detail views working with Vendure * Implement useCart/useAddItem * Implement useUpdateItem & useRemoveItem * Implement useSearch * Add operations codegen, tidy up * Dummy checkout page * Implement auth/customer hooks * Use env var for Shop API url * Add some documentation * Improve error handling * Optimize preview image size * Fix accidental change * Update Vendure provider to latest changes * Vendure provider: split out gql operations, remove unused files * Update Vendure provider readme * Add local next.config to Vendure provider, update docs * Update to use demo server * Fix build errors * Use proxy for vendure api * Simplify instructions in Vendure readme * Refactor Vendure checkout api handler * Improve image quality
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
import { SWRHook } from '@commerce/utils/types'
|
|
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
|
import { Product } from '@commerce/types'
|
|
import { SearchQuery, SearchQueryVariables } from '../schema'
|
|
import { normalizeSearchResult } from '../lib/normalize'
|
|
import { searchQuery } from '../lib/queries/search-query'
|
|
|
|
export default useSearch as UseSearch<typeof handler>
|
|
|
|
export type SearchProductsInput = {
|
|
search?: string
|
|
categoryId?: string
|
|
brandId?: string
|
|
sort?: string
|
|
}
|
|
|
|
export type SearchProductsData = {
|
|
products: Product[]
|
|
found: boolean
|
|
}
|
|
|
|
export const handler: SWRHook<
|
|
SearchProductsData,
|
|
SearchProductsInput,
|
|
SearchProductsInput
|
|
> = {
|
|
fetchOptions: {
|
|
query: searchQuery,
|
|
},
|
|
async fetcher({ input, options, fetch }) {
|
|
const { categoryId, brandId } = input
|
|
|
|
const variables: SearchQueryVariables = {
|
|
input: {
|
|
term: input.search,
|
|
collectionId: input.categoryId,
|
|
groupByProduct: true,
|
|
// TODO: what is the "sort" value?
|
|
},
|
|
}
|
|
const { search } = await fetch<SearchQuery>({
|
|
query: searchQuery,
|
|
variables,
|
|
})
|
|
|
|
return {
|
|
found: search.totalItems > 0,
|
|
products: search.items.map((item) => normalizeSearchResult(item)) ?? [],
|
|
}
|
|
},
|
|
useHook: ({ useData }) => (input = {}) => {
|
|
return useData({
|
|
input: [
|
|
['search', input.search],
|
|
['categoryId', input.categoryId],
|
|
['brandId', input.brandId],
|
|
['sort', input.sort],
|
|
],
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|