From d1ea2354ee6baa82d0a0a12812476e1ffdcd5fc9 Mon Sep 17 00:00:00 2001 From: Alessandro Casazza Date: Tue, 12 Oct 2021 16:33:28 +0200 Subject: [PATCH] feat: Add search products by name --- framework/commercelayer/commerce.config.json | 3 +- .../commercelayer/product/use-search.tsx | 29 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/framework/commercelayer/commerce.config.json b/framework/commercelayer/commerce.config.json index 5a06682ff..1d95d9997 100644 --- a/framework/commercelayer/commerce.config.json +++ b/framework/commercelayer/commerce.config.json @@ -2,6 +2,7 @@ "provider": "commercelayer", "features": { "customerAuth": true, - "cart": true + "cart": true, + "wishlist": true } } diff --git a/framework/commercelayer/product/use-search.tsx b/framework/commercelayer/product/use-search.tsx index 30e699537..111f00d8b 100644 --- a/framework/commercelayer/product/use-search.tsx +++ b/framework/commercelayer/product/use-search.tsx @@ -1,17 +1,32 @@ import { SWRHook } from '@commerce/utils/types' import useSearch, { UseSearch } from '@commerce/product/use-search' +import data from '../data.json' export default useSearch as UseSearch +const productsFinder = (s: string) => { + const { products } = data + return s + ? products.filter( + (p) => p.name.toLowerCase().search(s.toLowerCase()) !== -1 + ) + : [] +} + export const handler: SWRHook = { fetchOptions: { query: '', }, async fetcher({ input, options, fetch }) {}, - useHook: () => () => { - return { - data: { - products: [], - }, - } - }, + useHook: + ({ useData }) => + ({ search }) => { + const { mutate } = useData() + const products = productsFinder(search) + mutate() + return { + data: { + products, + }, + } + }, }