mirror of
https://github.com/vercel/commerce.git
synced 2025-05-21 17:06:58 +00:00
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { SWRHook } from '@commerce/utils/types'
|
|
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
|
import data from '../data.json'
|
|
export default useSearch as UseSearch<typeof handler>
|
|
|
|
const productsFinder = (s: string, c?: string, b?: string) => {
|
|
const { products } = data
|
|
let p = products
|
|
if (s)
|
|
p = p.filter((p) => p.name.toLowerCase().search(s.toLowerCase()) !== -1)
|
|
if (c)
|
|
p = p.filter(
|
|
(p) => p.categoryId.toLowerCase().search(c.toLowerCase()) !== -1
|
|
)
|
|
if (b)
|
|
p = p.filter((p) => p.brandId.toLowerCase().search(b.toLowerCase()) !== -1)
|
|
return p
|
|
}
|
|
|
|
export const handler: SWRHook<any> = {
|
|
fetchOptions: {
|
|
query: '',
|
|
},
|
|
async fetcher({ input, options, fetch }) {},
|
|
useHook:
|
|
({ useData }) =>
|
|
({ search, categoryId, brandId }) => {
|
|
const products = productsFinder(search, categoryId, brandId)
|
|
return {
|
|
data:
|
|
products.length > 0
|
|
? {
|
|
products,
|
|
found: true,
|
|
}
|
|
: data,
|
|
}
|
|
},
|
|
}
|