Open History to see the last commit
1import { useState } from 'react'
2import { useCartStore } from '~/e-Commerce/store/cartStore'
3β
4// Product info passed from parent
5const product = {
6 id: 'tanstack-ukulele',
7 name: 'TanStack Ukulele',
8 price: 149.99,
9 image: '/example-ukelele-tanstack.jpg',
10}
11β
12export function AddToCartButton() {
13 const [quantity, setQuantity] = useState(1)
14 const [added, setAdded] = useState(false)
15 const [favorite, setFavorite] = useState(false)
16 const addItem = useCartStore((state) => state.addItem)
17β
18 const handleAddToCart = () => {
19 // Add item(s) to cart
20 for (let i = 0; i < quantity; i++) {
21 addItem(product)
22 }
23 setAdded(true)
24 setQuantity(1) // Reset quantity after adding
25 setTimeout(() => setAdded(false), 2000)
26 }
27β
28 return (
29 <div className="border-6 border-dashed border-green-500 rounded-lg p-4 space-y-4">
30 {/* Quantity Selector */}
31 <div className="flex items-center gap-4">
32 <span className="text-gray-700 font-medium">Quantity:</span>
33 <div className="flex items-center border border-gray-300 rounded-lg">
34 <button
35 onClick={() => setQuantity(Math.max(1, quantity - 1))}
36 className="px-3 py-2 hover:bg-gray-100 transition-colors"
37 >
38 -
39 </button>
40 <span className="px-4 py-2 border-x border-gray-300 min-w-[50px] text-center">
41 {quantity}
42 </span>
43 <button
44 onClick={() => setQuantity(quantity + 1)}
45 className="px-3 py-2 hover:bg-gray-100 transition-colors"
46 >
47 +
48 </button>
49 </div>
50 </div>
51β
52 {/* Action Buttons */}
53 <div className="flex gap-3">
54 <button
55 onClick={handleAddToCart}
56 className={`flex-1 py-3 px-6 rounded-lg font-semibold transition-all ${
57 added
58 ? 'bg-green-600 text-white'
59 : 'bg-gray-900 text-white hover:bg-gray-800'
60 }`}
61 >
62 {added
63 ? 'β Added to Bag!'
64 : `Add to Bag${quantity > 1 ? ` (${quantity})` : ''}`}
65 </button>
66β
67 <button
68 onClick={() => setFavorite(!favorite)}
69 className={`px-4 py-3 rounded-lg border-2 transition-all ${
70 favorite
71 ? 'border-red-500 bg-red-50 text-red-500'
72 : 'border-gray-300 hover:border-gray-400'
73 }`}
74 aria-label={favorite ? 'Remove from favorites' : 'Add to favorites'}
75 >
76 {favorite ? 'β€οΈ' : 'π€'}
77 </button>
78 </div>
79β
80 {/* Info text */}
81 <p className="text-sm text-gray-500 text-center">
82 Free shipping on orders over $100
83 </p>
84 </div>
85 )
86}