feat: add customer cart with expiry, shipping addresses with USPS validation, and multi-item PayPal checkout
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import React, { createContext, useContext, useEffect, useState, useCallback } from 'react';
|
||||
import { CartItem, fetchCart } from './cartApi';
|
||||
import { useCustomerAuth } from '../customer/CustomerAuthContext';
|
||||
|
||||
interface CartContextValue {
|
||||
items: CartItem[];
|
||||
itemIds: Set<number>;
|
||||
refresh: () => void;
|
||||
}
|
||||
|
||||
const CartContext = createContext<CartContextValue>({ items: [], itemIds: new Set(), refresh: () => {} });
|
||||
|
||||
export function useCart() {
|
||||
return useContext(CartContext);
|
||||
}
|
||||
|
||||
export function CartProvider({ children }: { children: React.ReactNode }) {
|
||||
const [items, setItems] = useState<CartItem[]>([]);
|
||||
const { customer } = useCustomerAuth();
|
||||
|
||||
const refresh = useCallback(() => {
|
||||
if (!customer) { setItems([]); return; }
|
||||
fetchCart().then(data => setItems(data.items)).catch(() => setItems([]));
|
||||
}, [customer]);
|
||||
|
||||
useEffect(() => { refresh(); }, [refresh]);
|
||||
|
||||
const itemIds = new Set(items.map(i => i.item_id));
|
||||
|
||||
return (
|
||||
<CartContext.Provider value={{ items, itemIds, refresh }}>
|
||||
{children}
|
||||
</CartContext.Provider>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user