Skip to content

Commit

Permalink
Update CartProvider.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
w3bdesign committed Oct 23, 2024
1 parent a85c84c commit 368ac7a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/stores/CartProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ interface ICartContext {
cart: RootObject | null | undefined;
setCart: React.Dispatch<React.SetStateAction<TRootObjectNull>>;
updateCart: (newCart: RootObject) => void;
isLoading: boolean;
}

const CartState: ICartContext = {
cart: null,
setCart: () => {},
updateCart: () => {},
isLoading: true,
};

export const CartContext = createContext<ICartContext>(CartState);
Expand All @@ -66,6 +68,7 @@ export const CartContext = createContext<ICartContext>(CartState);
*/
export const CartProvider = ({ children }: ICartProviderProps) => {
const [cart, setCart] = useState<RootObject | null>();
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
// Check if we are client-side before we access the localStorage
Expand All @@ -75,6 +78,7 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
const cartData: RootObject = JSON.parse(localCartData);
setCart(cartData);
}
setIsLoading(false);
}
}, []);

Expand All @@ -86,8 +90,8 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
};

const contextValue = useMemo(() => {
return { cart, setCart, updateCart };
}, [cart]);
return { cart, setCart, updateCart, isLoading };
}, [cart, isLoading]);

return (
<CartContext.Provider value={contextValue}>
Expand Down

0 comments on commit 368ac7a

Please sign in to comment.