Comments, appointments adjustments, fixed some issues
This commit is contained in:
@@ -15,10 +15,14 @@ import {
|
||||
apiCancelCheckout,
|
||||
} from "@/lib/cartApi";
|
||||
|
||||
//Cart context
|
||||
//Holds the user's cart and all cart actions
|
||||
const CartContext = createContext(null);
|
||||
|
||||
//Key used to save the selected store in localStorage
|
||||
const STORE_KEY = "selected_store_id";
|
||||
|
||||
//Provides cart state to all child components
|
||||
export function CartProvider({ children }) {
|
||||
const { user, token } = useAuth();
|
||||
const [cart, setCart] = useState(null);
|
||||
@@ -26,6 +30,7 @@ export function CartProvider({ children }) {
|
||||
const [cartLoading, setCartLoading] = useState(false);
|
||||
const [cartError, setCartError] = useState(null);
|
||||
|
||||
//Saves the selected store in state and localStorage
|
||||
const setStoreId = useCallback((id) => {
|
||||
const parsed = id ? Number(id) : null;
|
||||
setSelectedStoreIdState(parsed);
|
||||
@@ -51,6 +56,7 @@ export function CartProvider({ children }) {
|
||||
}
|
||||
}, [user, setStoreId]);
|
||||
|
||||
//Fetches the latest cart from the backend
|
||||
const refreshCart = useCallback(async () => {
|
||||
if (!token || !selectedStoreId) {
|
||||
setCart(null);
|
||||
@@ -173,6 +179,7 @@ export function CartProvider({ children }) {
|
||||
[token, selectedStoreId, refreshCart]
|
||||
);
|
||||
|
||||
//Total number of items across all cart rows, used for the cart badge in the nav
|
||||
const itemCount = cart?.items?.reduce((sum, i) => sum + i.quantity, 0) ?? 0;
|
||||
|
||||
return (
|
||||
@@ -201,6 +208,8 @@ export function CartProvider({ children }) {
|
||||
);
|
||||
}
|
||||
|
||||
//Hook to access cart state
|
||||
//Must be used inside a CartProvider
|
||||
export function useCart() {
|
||||
const ctx = useContext(CartContext);
|
||||
if (!ctx) throw new Error("useCart must be used within a CartProvider");
|
||||
|
||||
Reference in New Issue
Block a user