Loan Laux c2c43b76aa
refactor category and brand filtering logic
Signed-off-by: Loan Laux <loan@outgrow.io>
2021-07-07 19:16:45 +03:00

49 lines
1.3 KiB
TypeScript

import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
export default useSearch as UseSearch<typeof handler>
export const handler: SWRHook<any> = {
fetchOptions: {
url: '/api/catalog/products',
method: 'GET',
},
async 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')
if (search) url.searchParams.set('search', search)
if (categoryId)
url.searchParams.set('categoryId', String(categoryId))
if (brandId)
url.searchParams.set('brandId', String(brandId))
if (sort) url.searchParams.set('sort', sort)
const results = await fetch({
url: url.pathname + url.search,
method: options.method,
})
return {
products: results?.products ?? [],
found: results?.products?.length > 0 ?? false,
}
},
useHook:
({ useData }) =>
(input = {}) => {
return useData({
input: [
['search', input.search],
['categoryId', input.categoryId],
['brandId', input.brandId],
['sort', input.sort],
],
swrOptions: {
revalidateOnFocus: false,
...input.swrOptions,
},
})
},
}