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react';
|
| 2 |
+
|
| 3 |
+
export default function Cart({ isOpen, onClose, cartItems, onRemoveFromCart, onUpdateQuantity }) {
|
| 4 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 5 |
+
|
| 6 |
+
const subtotal = cartItems.reduce((acc, item) => acc + item.price * item.quantity, 0);
|
| 7 |
+
const shipping = subtotal > 35 ? 0 : 5.99;
|
| 8 |
+
const total = subtotal + shipping;
|
| 9 |
+
|
| 10 |
+
const handleCheckout = async () => {
|
| 11 |
+
setIsLoading(true);
|
| 12 |
+
// Simulate checkout
|
| 13 |
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
| 14 |
+
setIsLoading(false);
|
| 15 |
+
alert('Thank you for your order! This is a demo.');
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
if (!isOpen) return null;
|
| 19 |
+
|
| 20 |
+
return (
|
| 21 |
+
<>
|
| 22 |
+
{/* Backdrop */}
|
| 23 |
+
<div
|
| 24 |
+
className="fixed inset-0 bg-black bg-opacity-50 z-40 transition-opacity"
|
| 25 |
+
onClick={onClose}
|
| 26 |
+
/>
|
| 27 |
+
|
| 28 |
+
{/* Cart Sidebar */}
|
| 29 |
+
<div className="fixed inset-y-0 right-0 w-full max-w-md bg-white shadow-2xl z-50 flex flex-col transform transition-transform duration-300">
|
| 30 |
+
{/* Cart Header */}
|
| 31 |
+
<div className="bg-amazon-light text-white p-4 flex items-center justify-between">
|
| 32 |
+
<h2 className="text-lg font-bold">Shopping Cart</h2>
|
| 33 |
+
<button
|
| 34 |
+
onClick={onClose}
|
| 35 |
+
className="p-2 hover:bg-gray-700 rounded-full transition-colors"
|
| 36 |
+
>
|
| 37 |
+
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 38 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
| 39 |
+
</svg>
|
| 40 |
+
</button>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
{/* Cart Items */}
|
| 44 |
+
<div className="flex-1 overflow-y-auto p-4">
|
| 45 |
+
{cartItems.length === 0 ? (
|
| 46 |
+
<div className="flex flex-col items-center justify-center h-full text-center">
|
| 47 |
+
<svg className="w-20 h-20 text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
| 48 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z" />
|
| 49 |
+
</svg>
|
| 50 |
+
<h3 className="text-lg font-medium text-gray-600 mb-2">Your cart is empty</h3>
|
| 51 |
+
<p className="text-sm text-gray-500 mb-4">Looks like you haven't added anything yet</p>
|
| 52 |
+
<button
|
| 53 |
+
onClick={onClose}
|
| 54 |
+
className="text-amazon-orange hover:underline font-medium"
|
| 55 |
+
>
|
| 56 |
+
Continue Shopping
|
| 57 |
+
</button>
|
| 58 |
+
</div>
|
| 59 |
+
) : (
|
| 60 |
+
<div className="space-y-4">
|
| 61 |
+
{cartItems.map((item) => (
|
| 62 |
+
<div
|
| 63 |
+
key={item.id}
|
| 64 |
+
className="flex gap-4 bg-gray-50 p-4 rounded-lg"
|
| 65 |
+
>
|
| 66 |
+
<img
|
| 67 |
+
src={item.image}
|
| 68 |
+
alt={item.name}
|
| 69 |
+
className="w-20 h-20 object-contain bg-white rounded"
|
| 70 |
+
/>
|
| 71 |
+
<div className="flex-1">
|
| 72 |
+
<h3 className="text-sm font-medium text-gray-800 line-clamp-2">
|
| 73 |
+
{item.name}
|
| 74 |
+
</h3>
|
| 75 |
+
<p className="text-lg font-bold text-gray-900 mt-1">
|
| 76 |
+
${item.price.toFixed(2)}
|
| 77 |
+
</p>
|
| 78 |
+
<p className="text-xs text-green-600 mt-1">In Stock</p>
|
| 79 |
+
|
| 80 |
+
{/* Quantity Controls */}
|
| 81 |
+
<div className="flex items-center gap-2 mt-2">
|
| 82 |
+
<span className="text-xs text-gray-600">Qty:</span>
|
| 83 |
+
<select
|
| 84 |
+
value={item.quantity}
|
| 85 |
+
onChange={(e) => onUpdateQuantity(item.id, parseInt(e.target.value))}
|
| 86 |
+
className="border border-gray-300 rounded px-2 py-1 text-sm"
|
| 87 |
+
>
|
| 88 |
+
{[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((num) => (
|
| 89 |
+
<option key={num} value={num}>{num}</option>
|
| 90 |
+
))}
|
| 91 |
+
</select>
|
| 92 |
+
<button
|
| 93 |
+
onClick={() => onRemoveFromCart(item.id)}
|
| 94 |
+
className="text-xs text-blue-500 hover:underline ml-4"
|
| 95 |
+
>
|
| 96 |
+
Delete
|
| 97 |
+
</button>
|
| 98 |
+
</div>
|
| 99 |
+
</div>
|
| 100 |
+
</div>
|
| 101 |
+
))}
|
| 102 |
+
</div>
|
| 103 |
+
)}
|
| 104 |
+
</div>
|
| 105 |
+
|
| 106 |
+
{/* Cart Footer */}
|
| 107 |
+
{cartItems.length > 0 && (
|
| 108 |
+
<div className="border-t border-gray-200 p-4 bg-gray-50">
|
| 109 |
+
<div className="space-y-2 mb-4">
|
| 110 |
+
<div className="flex justify-between text-sm">
|
| 111 |
+
<span className="text-gray-600">Subtotal ({cartItems.length} items):</span>
|
| 112 |
+
<span className="font-bold">${subtotal.toFixed(2)}</span>
|
| 113 |
+
</div>
|
| 114 |
+
<div className="flex justify-between text-sm">
|
| 115 |
+
<span className="text-gray-600">Shipping:</span>
|
| 116 |
+
<span className={shipping === 0 ? 'text-green-600' : ''}>
|
| 117 |
+
{shipping === 0 ? 'FREE' : `$${shipping.toFixed(2)}`}
|
| 118 |
+
</span>
|
| 119 |
+
</div>
|
| 120 |
+
{subtotal < 35 && (
|
| 121 |
+
<p className="text-xs text-gray-500">
|
| 122 |
+
Add ${(35 - subtotal).toFixed(2)} more for FREE shipping
|
| 123 |
+
</p>
|
| 124 |
+
)}
|
| 125 |
+
<div className="flex justify-between text-lg border-t border-gray-300 pt-2">
|
| 126 |
+
<span className="font-bold">Total:</span>
|
| 127 |
+
<span className="font-bold">${total.toFixed(2)}</span>
|
| 128 |
+
</div>
|
| 129 |
+
</div>
|
| 130 |
+
|
| 131 |
+
<button
|
| 132 |
+
onClick={handleCheckout}
|
| 133 |
+
disabled={isLoading}
|
| 134 |
+
className="w-full bg-amazon-yellow hover:bg-yellow-500 text-gray-900 font-bold py-3 px-4 rounded transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
| 135 |
+
>
|
| 136 |
+
{isLoading ? (
|
| 137 |
+
<span className="flex items-center justify-center">
|
| 138 |
+
<svg className="animate-spin -ml-1 mr-3 h-5 w-5 text-gray-900" fill="none" viewBox="0 0 24 24">
|
| 139 |
+
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
| 140 |
+
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
| 141 |
+
</svg>
|
| 142 |
+
Processing...
|
| 143 |
+
</span>
|
| 144 |
+
) : (
|
| 145 |
+
'Proceed to Checkout'
|
| 146 |
+
)}
|
| 147 |
+
</button>
|
| 148 |
+
</div>
|
| 149 |
+
)}
|
| 150 |
+
</div>
|
| 151 |
+
</>
|
| 152 |
+
);
|
| 153 |
+
}
|