From 61eddbb2bc3f2144f81e6af0bcf75c2d4a9fa9ad Mon Sep 17 00:00:00 2001 From: Nuwan Date: Sun, 22 Dec 2024 21:59:28 +0530 Subject: [PATCH] prevent calling /shopping_carts api multiple times in jamtracks listing --- jam-ui/src/hooks/useShoppingCart.js | 62 ++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/jam-ui/src/hooks/useShoppingCart.js b/jam-ui/src/hooks/useShoppingCart.js index 3e09b8a16..7dfb5a1c8 100644 --- a/jam-ui/src/hooks/useShoppingCart.js +++ b/jam-ui/src/hooks/useShoppingCart.js @@ -1,5 +1,5 @@ import { getShoppingCart, addJamtrackToShoppingCart, removeShoppingCart } from "../helpers/rest" -import { useState, useEffect, useMemo } from "react"; +import { useState, useEffect, useMemo, useCallback } from "react"; export const useShoppingCart = () => { const [loading, setLoading] = useState(false); @@ -19,7 +19,21 @@ export const useShoppingCart = () => { - const getCartItems = async () => { + // const getCartItems = async () => { + // try { + // setLoading(true); + // const resp = await getShoppingCart(); + // const data = await resp.json(); + // setShoppingCart(data); + // } catch (error) { + // console.log(error); + // setError(error); + // }finally{ + // setLoading(false); + // } + // } + + const getCartItems = useCallback(async () => { try { setLoading(true); const resp = await getShoppingCart(); @@ -31,9 +45,21 @@ export const useShoppingCart = () => { }finally{ setLoading(false); } - } + }, []); - const addCartItem = async (options) => { + // const addCartItem = async (options) => { + // try { + // const resp = await addJamtrackToShoppingCart(options); + // const data = await resp.json(); + // setShoppingCart([...shoppingCart, data]); + // return data; + // } catch (error) { + // console.log(error); + // return false; + // } + // } + + const addCartItem = useCallback(async (options) => { try { const resp = await addJamtrackToShoppingCart(options); const data = await resp.json(); @@ -43,9 +69,21 @@ export const useShoppingCart = () => { console.log(error); return false; } - } + }, [shoppingCart]); - const removeCartItem = async (id) => { + // const removeCartItem = async (id) => { + // try { + // await removeShoppingCart({id}); + // setShoppingCart(shoppingCart.filter(item => item.id !== id)); + // return true; + // } catch (error) { + // console.log(error); + // return false; + // } + + // } + + const removeCartItem = useCallback(async (id) => { try { await removeShoppingCart({id}); setShoppingCart(shoppingCart.filter(item => item.id !== id)); @@ -55,11 +93,17 @@ export const useShoppingCart = () => { return false; } - } + }, []); + + - const hasOnlyFreeItemsInShoppingCart = () => { + // const hasOnlyFreeItemsInShoppingCart = () => { + // return shoppingCart.length === 0 || shoppingCart.every(item => item.product_info.free); + // } + + const hasOnlyFreeItemsInShoppingCart = useCallback(() => { return shoppingCart.length === 0 || shoppingCart.every(item => item.product_info.free); - } + }, [shoppingCart]); return{ shoppingCart, error, loading, removeCartItem, addCartItem, cartTotal, hasOnlyFreeItemsInShoppingCart