forked from crowetic/commerce
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import { SWRHook } from '@commerce/utils/types'
|
|
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
|
|
|
import { getAllProductsQuery, normalizeProduct } from '../utils'
|
|
|
|
import { Product } from '@commerce/types'
|
|
|
|
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: getAllProductsQuery,
|
|
},
|
|
async fetcher({ input, options, fetch }) {
|
|
const { categoryId, search } = input
|
|
const { results, count: found } = await fetch({
|
|
query: 'products',
|
|
method: 'list',
|
|
variables: { category: categoryId, search },
|
|
})
|
|
|
|
const products = results.map((product) => normalizeProduct(product))
|
|
|
|
return {
|
|
products,
|
|
found,
|
|
}
|
|
},
|
|
useHook: ({ useData }) => (input = {}) => {
|
|
return useData({
|
|
input: [
|
|
['search', input.search],
|
|
['categoryId', input.categoryId],
|
|
['brandId', input.brandId],
|
|
['sort', input.sort],
|
|
],
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|