Skip to content

Commit

Permalink
add clear cart
Browse files Browse the repository at this point in the history
  • Loading branch information
encryptedDegen committed Oct 28, 2024
1 parent 86b01bf commit f77fef0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/contexts/cart-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ export const CartProvider: React.FC<Props> = ({ children }: Props) => {
const [loadingCartItems, setLoadingCartItems] = useState<number>(0);
const { address } = useAccount();

const { set, has, delete: deleteKey, entries, forceUpdate, setBulk, values } = useCartMap();
const {
set,
has,
delete: deleteKey,
entries,
forceUpdate,
setBulk,
values,
clear,
} = useCartMap();

useEffect(() => {
if (typeof window === "undefined") return;
Expand Down Expand Up @@ -306,7 +315,7 @@ export const CartProvider: React.FC<Props> = ({ children }: Props) => {

// Resets the cart items
const resetCart = () => {
setBulk([]);
clear();
};

const totalCartItems = values().length + loadingCartItems;
Expand Down
7 changes: 6 additions & 1 deletion src/hooks/use-cart-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const useCartMap = (initialData = []) => {
return Array.from(mapRef.current.values())
}, [])

const clear = useCallback(() => {
mapRef.current.clear()
}, [])

const forceUpdate = useCallback(() => {
setTick(tick => tick + 1)
}, [])
Expand All @@ -47,6 +51,7 @@ export const useCartMap = (initialData = []) => {
entries,
forceUpdate,
setBulk,
values
values,
clear,
}
}

0 comments on commit f77fef0

Please sign in to comment.