|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { motion, AnimatePresence } from 'framer-motion'; |
| 3 | +import { X, ChevronRight, MapPin, CreditCard, ShoppingBag, Check } from 'lucide-react'; |
| 4 | + |
| 5 | +const STEPS = ['MY BAG', 'ADDRESS', 'PAYMENT']; |
| 6 | + |
| 7 | +const Checkout = ({ cartItems, onClose, onRemoveFromCart }) => { |
| 8 | + const [step, setStep] = useState(0); |
| 9 | + const [address, setAddress] = useState({ name: '', phone: '', pincode: '', city: '', state: '', street: '' }); |
| 10 | + const [paymentMethod, setPaymentMethod] = useState('cod'); |
| 11 | + const [orderPlaced, setOrderPlaced] = useState(false); |
| 12 | + |
| 13 | + const cartTotal = cartItems.reduce((sum, i) => sum + i.price * i.qty, 0); |
| 14 | + const gst = Math.round(cartTotal * 0.05); |
| 15 | + const shipping = cartTotal > 999 ? 0 : 99; |
| 16 | + |
| 17 | + const handlePlaceOrder = () => { |
| 18 | + setOrderPlaced(true); |
| 19 | + }; |
| 20 | + |
| 21 | + if (orderPlaced) { |
| 22 | + return ( |
| 23 | + <div className="checkout-overlay"> |
| 24 | + <div className="checkout-modal"> |
| 25 | + <motion.div |
| 26 | + className="order-success" |
| 27 | + initial={{ scale: 0.8, opacity: 0 }} |
| 28 | + animate={{ scale: 1, opacity: 1 }} |
| 29 | + > |
| 30 | + <div className="success-icon"><Check size={40} /></div> |
| 31 | + <h2>Order Placed!</h2> |
| 32 | + <p>Your order has been placed successfully. You'll receive a confirmation soon.</p> |
| 33 | + <button className="btn-primary" onClick={onClose}>CONTINUE SHOPPING</button> |
| 34 | + </motion.div> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="checkout-overlay"> |
| 42 | + <motion.div |
| 43 | + className="checkout-modal" |
| 44 | + initial={{ opacity: 0, y: 40 }} |
| 45 | + animate={{ opacity: 1, y: 0 }} |
| 46 | + exit={{ opacity: 0, y: 40 }} |
| 47 | + > |
| 48 | + {/* Header */} |
| 49 | + <div className="checkout-header"> |
| 50 | + <h2>CHECKOUT</h2> |
| 51 | + <button className="close-btn" onClick={onClose}><X size={22} /></button> |
| 52 | + </div> |
| 53 | + |
| 54 | + {/* Steps */} |
| 55 | + <div className="checkout-steps"> |
| 56 | + {STEPS.map((s, i) => ( |
| 57 | + <React.Fragment key={s}> |
| 58 | + <div className={`step-item ${i <= step ? 'active' : ''} ${i < step ? 'done' : ''}`}> |
| 59 | + <div className="step-circle">{i < step ? <Check size={14} /> : i + 1}</div> |
| 60 | + <span>{s}</span> |
| 61 | + </div> |
| 62 | + {i < STEPS.length - 1 && <div className={`step-line ${i < step ? 'done' : ''}`} />} |
| 63 | + </React.Fragment> |
| 64 | + ))} |
| 65 | + </div> |
| 66 | + |
| 67 | + {/* Step Content */} |
| 68 | + <div className="checkout-body"> |
| 69 | + <AnimatePresence mode="wait"> |
| 70 | + |
| 71 | + {/* STEP 1: MY BAG */} |
| 72 | + {step === 0 && ( |
| 73 | + <motion.div key="bag" initial={{ opacity: 0, x: 20 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -20 }}> |
| 74 | + <div className="co-items"> |
| 75 | + {cartItems.map((item, i) => ( |
| 76 | + <div key={i} className="co-item"> |
| 77 | + <img src={item.image} alt={item.name} /> |
| 78 | + <div className="co-item-info"> |
| 79 | + <p className="co-item-name">{item.name}</p> |
| 80 | + <p className="co-item-meta">{item.category}</p> |
| 81 | + <div className="co-item-tags"> |
| 82 | + <span>Size: UK {item.size}</span> |
| 83 | + <span>Qty: {item.qty}</span> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + <div className="co-item-price"> |
| 87 | + <p>₹ {(item.price * item.qty).toLocaleString('en-IN')}</p> |
| 88 | + <span>MRP incl. of all taxes</span> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + <div className="co-billing"> |
| 94 | + <h4>BILLING DETAILS</h4> |
| 95 | + <div className="billing-row"><span>Cart Total <small>(Incl. of all taxes)</small></span><span>₹ {cartTotal.toLocaleString('en-IN')}</span></div> |
| 96 | + <div className="billing-row"><span>Shipping Charges</span><span className="free-ship">{shipping === 0 ? 'FREE' : `₹ ${shipping}`}</span></div> |
| 97 | + <div className="billing-row total-row"><span>Total Amount <small>(Incl. of ₹{gst} GST)</small></span><span>₹ {(cartTotal + shipping).toLocaleString('en-IN')}</span></div> |
| 98 | + </div> |
| 99 | + <button className="btn-primary co-next-btn" onClick={() => setStep(1)}> |
| 100 | + PROCEED TO ADDRESS <ChevronRight size={18} /> |
| 101 | + </button> |
| 102 | + </motion.div> |
| 103 | + )} |
| 104 | + |
| 105 | + {/* STEP 2: ADDRESS */} |
| 106 | + {step === 1 && ( |
| 107 | + <motion.div key="address" initial={{ opacity: 0, x: 20 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -20 }}> |
| 108 | + <div className="co-address-form"> |
| 109 | + <h4><MapPin size={16} /> DELIVERY ADDRESS</h4> |
| 110 | + <div className="form-row-2"> |
| 111 | + <input placeholder="Full Name *" value={address.name} onChange={e => setAddress({ ...address, name: e.target.value })} /> |
| 112 | + <input placeholder="Phone Number *" value={address.phone} onChange={e => setAddress({ ...address, phone: e.target.value })} /> |
| 113 | + </div> |
| 114 | + <input placeholder="Street Address *" value={address.street} onChange={e => setAddress({ ...address, street: e.target.value })} /> |
| 115 | + <div className="form-row-3"> |
| 116 | + <input placeholder="Pincode *" value={address.pincode} onChange={e => setAddress({ ...address, pincode: e.target.value })} /> |
| 117 | + <input placeholder="City *" value={address.city} onChange={e => setAddress({ ...address, city: e.target.value })} /> |
| 118 | + <input placeholder="State *" value={address.state} onChange={e => setAddress({ ...address, state: e.target.value })} /> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + <div className="co-btn-row"> |
| 122 | + <button className="btn-outline co-back-btn" onClick={() => setStep(0)}>BACK</button> |
| 123 | + <button |
| 124 | + className="btn-primary co-next-btn" |
| 125 | + onClick={() => { |
| 126 | + if (!address.name || !address.phone || !address.street || !address.city || !address.pincode) { |
| 127 | + alert('Please fill all required fields'); |
| 128 | + return; |
| 129 | + } |
| 130 | + setStep(2); |
| 131 | + }} |
| 132 | + > |
| 133 | + PROCEED TO PAYMENT <ChevronRight size={18} /> |
| 134 | + </button> |
| 135 | + </div> |
| 136 | + </motion.div> |
| 137 | + )} |
| 138 | + |
| 139 | + {/* STEP 3: PAYMENT */} |
| 140 | + {step === 2 && ( |
| 141 | + <motion.div key="payment" initial={{ opacity: 0, x: 20 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -20 }}> |
| 142 | + <div className="co-payment"> |
| 143 | + <h4><CreditCard size={16} /> PAYMENT METHOD</h4> |
| 144 | + <div className="payment-options"> |
| 145 | + {[ |
| 146 | + { id: 'cod', label: 'Cash on Delivery' }, |
| 147 | + { id: 'upi', label: 'UPI / GPay / PhonePe' }, |
| 148 | + { id: 'card', label: 'Credit / Debit Card' }, |
| 149 | + ].map(opt => ( |
| 150 | + <label key={opt.id} className={`payment-option ${paymentMethod === opt.id ? 'selected' : ''}`}> |
| 151 | + <input type="radio" name="payment" value={opt.id} checked={paymentMethod === opt.id} onChange={() => setPaymentMethod(opt.id)} /> |
| 152 | + <span>{opt.label}</span> |
| 153 | + </label> |
| 154 | + ))} |
| 155 | + </div> |
| 156 | + |
| 157 | + <div className="co-billing"> |
| 158 | + <h4>ORDER SUMMARY</h4> |
| 159 | + <div className="billing-row"><span>Cart Total</span><span>₹ {cartTotal.toLocaleString('en-IN')}</span></div> |
| 160 | + <div className="billing-row"><span>Shipping</span><span className="free-ship">{shipping === 0 ? 'FREE' : `₹ ${shipping}`}</span></div> |
| 161 | + <div className="billing-row total-row"><span>Total Amount</span><span>₹ {(cartTotal + shipping).toLocaleString('en-IN')}</span></div> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + <div className="co-btn-row"> |
| 165 | + <button className="btn-outline co-back-btn" onClick={() => setStep(1)}>BACK</button> |
| 166 | + <button className="btn-primary co-next-btn place-order-btn" onClick={handlePlaceOrder}> |
| 167 | + PLACE ORDER |
| 168 | + </button> |
| 169 | + </div> |
| 170 | + </motion.div> |
| 171 | + )} |
| 172 | + |
| 173 | + </AnimatePresence> |
| 174 | + </div> |
| 175 | + </motion.div> |
| 176 | + </div> |
| 177 | + ); |
| 178 | +}; |
| 179 | + |
| 180 | +export default Checkout; |
0 commit comments