Files
commerce/framework/ordercloud/product/use-search.tsx
2021-10-11 10:13:46 -05:00

40 lines
1.2 KiB
TypeScript

import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
import { SearchProductsHook } from '@commerce/types/product'
export default useSearch as UseSearch<typeof handler>
export const handler: SWRHook<SearchProductsHook> = {
fetchOptions: {
url: '/api/catalog/products',
method: 'GET',
},
fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
// Use a dummy base as we only care about the relative path
const url = new URL(options.url!, 'http://a')
url.searchParams.set('search', String(search))
url.searchParams.set('categoryId', String(categoryId))
url.searchParams.set('brandId', String(brandId))
url.searchParams.set('sort', String(sort))
return fetch({
url: url.pathname + url.search,
method: options.method,
})
},
useHook: ({ useData }) => (input = {}) => {
return useData({
input: [
['search', input.search],
['categoryId', input.categoryId],
['brandId', input.brandId],
['sort', input.sort]
],
swrOptions: {
revalidateOnFocus: false,
...input.swrOptions,
},
})
},
}