From 25805e85546e723f04693ee8106a094f68e5ecbb Mon Sep 17 00:00:00 2001 From: Shai Yallin Date: Thu, 5 Sep 2024 10:47:20 +0300 Subject: [PATCH] simplify --- packages/client/src/hooks/products.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/client/src/hooks/products.ts b/packages/client/src/hooks/products.ts index f87e765..d50f589 100644 --- a/packages/client/src/hooks/products.ts +++ b/packages/client/src/hooks/products.ts @@ -9,19 +9,11 @@ type ProductQuery = { export const useProducts = ({freeTextSearch}: ProductQuery) => { const {productCatalog} = useContext(IOContext); - const searchProducts = async () => { - const res = await productCatalog.get(`/products/search?query=${freeTextSearch}`); + const {data, isLoading, error} = useQuery(["products", freeTextSearch], async () => { + const url = freeTextSearch?.length > 0 ? `/products/search?query=${freeTextSearch}` : `/products`; + const res = await productCatalog.get(url); return res.data.map(p => Product.parse(p)); - }; - - const findAllProducts = async () => { - const res = await productCatalog.get(`/products`); - return res.data.map(p => Product.parse(p)); - } - - const {data, isLoading, error} = useQuery(["products", freeTextSearch], () => freeTextSearch?.length > 0 ? - searchProducts() : - findAllProducts()); + }); return { products: data,