const { useState, useEffect, useCallback, useRef } = React;
const CartItem = ({ item, onUpdateQuantity, onRemove }) => {
const { products } = useApp();
const latestProduct = products.find(p => p.id === item.id);
const stockQuantity = latestProduct?.stock_quantity || 0;
const currentPrice = latestProduct?.price ?? item.price;
const atLimit = item.quantity >= stockQuantity;
return (
{item.image_url ? (

) : (
)}
{item.name}
NT$ {currentPrice?.toLocaleString()}
{item.quantity}
{atLimit && (
已達庫存上限(最多可購買 {stockQuantity} 件)
)}
);
};
const CartPage = ({ onCheckout }) => {
const { cart, products, updateCartQuantity, removeFromCart } = useApp();
const subtotal = cart.reduce((sum, item) => {
const latestProduct = products.find(p => p.id === item.id);
const currentPrice = latestProduct?.price ?? item.price;
return sum + (currentPrice * item.quantity);
}, 0);
const shippingFee = subtotal >= 50000 ? 0 : 150;
const total = subtotal + shippingFee;
const animatedTotal = useAnimatedNumber(total);
if (cart.length === 0) {
return (
);
}
return (
{cart.map((item, index) => (
))}
小計
NT$ {subtotal.toLocaleString()}
運費 {subtotal >= 50000 && (滿5萬免運)}
NT$ {shippingFee}
總計
NT$ {animatedTotal.toLocaleString()}
);
};