Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
electricmonk committed Sep 5, 2024
1 parent cb636b8 commit 25805e8
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions packages/client/src/hooks/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ type ProductQuery = {
export const useProducts = ({freeTextSearch}: ProductQuery) => {
const {productCatalog} = useContext(IOContext);

const searchProducts = async () => {
const res = await productCatalog.get<unknown[]>(`/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<unknown[]>(url);
return res.data.map(p => Product.parse(p));
};

const findAllProducts = async () => {
const res = await productCatalog.get<unknown[]>(`/products`);
return res.data.map(p => Product.parse(p));
}

const {data, isLoading, error} = useQuery(["products", freeTextSearch], () => freeTextSearch?.length > 0 ?
searchProducts() :
findAllProducts());
});

return {
products: data,
Expand Down

0 comments on commit 25805e8

Please sign in to comment.