feat: Add search products by name

This commit is contained in:
Alessandro Casazza 2021-10-12 16:33:28 +02:00
parent f33cb3fc86
commit d1ea2354ee
No known key found for this signature in database
GPG Key ID: 3AF41B06C6495D3D
2 changed files with 24 additions and 8 deletions

View File

@ -2,6 +2,7 @@
"provider": "commercelayer", "provider": "commercelayer",
"features": { "features": {
"customerAuth": true, "customerAuth": true,
"cart": true "cart": true,
"wishlist": true
} }
} }

View File

@ -1,17 +1,32 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search' import useSearch, { UseSearch } from '@commerce/product/use-search'
import data from '../data.json'
export default useSearch as UseSearch<typeof handler> export default useSearch as UseSearch<typeof handler>
const productsFinder = (s: string) => {
const { products } = data
return s
? products.filter(
(p) => p.name.toLowerCase().search(s.toLowerCase()) !== -1
)
: []
}
export const handler: SWRHook<any> = { export const handler: SWRHook<any> = {
fetchOptions: { fetchOptions: {
query: '', query: '',
}, },
async fetcher({ input, options, fetch }) {}, async fetcher({ input, options, fetch }) {},
useHook: () => () => { useHook:
return { ({ useData }) =>
data: { ({ search }) => {
products: [], const { mutate } = useData()
}, const products = productsFinder(search)
} mutate()
}, return {
data: {
products,
},
}
},
} }