|
1 | | -import React from 'react'; |
2 | | -import { motion } from 'framer-motion'; |
3 | | -import { X, Package, LogOut, User } from 'lucide-react'; |
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { motion, AnimatePresence } from 'framer-motion'; |
| 3 | +import { X, Package, LogOut, ArrowLeft, ChevronRight } from 'lucide-react'; |
| 4 | + |
| 5 | +const CANCEL_REASONS = [ |
| 6 | + 'Changed my mind', |
| 7 | + 'Ordered by mistake', |
| 8 | + 'Found a better price elsewhere', |
| 9 | + 'Delivery time is too long', |
| 10 | + 'Product no longer needed', |
| 11 | + 'Other', |
| 12 | +]; |
| 13 | + |
| 14 | +const TRACKING_STEPS = ['Order Placed', 'Shipped', 'In-Transit', 'Out For Delivery', 'Delivered']; |
| 15 | + |
| 16 | +const formatDate = (dateStr) => { |
| 17 | + const d = new Date(dateStr); |
| 18 | + return d.toLocaleDateString('en-IN', { day: '2-digit', month: 'short', year: 'numeric' }); |
| 19 | +}; |
| 20 | + |
| 21 | +const getEstDelivery = (dateStr) => { |
| 22 | + const d = new Date(dateStr); |
| 23 | + d.setDate(d.getDate() + 7); |
| 24 | + return d.toLocaleDateString('en-IN', { day: '2-digit', month: 'short', year: 'numeric' }); |
| 25 | +}; |
| 26 | + |
| 27 | +const AccountModal = ({ user, onClose, onLogout, orders, onCancelOrder }) => { |
| 28 | + const [view, setView] = useState('main'); // main | detail | cancel | cancelled |
| 29 | + const [selectedOrder, setSelectedOrder] = useState(null); |
| 30 | + const [cancelReason, setCancelReason] = useState(''); |
| 31 | + const [remarks, setRemarks] = useState(''); |
| 32 | + const [showConfirmPopup, setShowConfirmPopup] = useState(false); |
| 33 | + |
| 34 | + const handleCancelConfirm = () => { |
| 35 | + if (!cancelReason) { alert('Please select a reason'); return; } |
| 36 | + setShowConfirmPopup(true); |
| 37 | + }; |
| 38 | + |
| 39 | + const handleContinueAfterCancel = () => { |
| 40 | + onCancelOrder(selectedOrder.orderId); |
| 41 | + setShowConfirmPopup(false); |
| 42 | + setView('main'); |
| 43 | + setSelectedOrder(null); |
| 44 | + setCancelReason(''); |
| 45 | + setRemarks(''); |
| 46 | + }; |
4 | 47 |
|
5 | | -const AccountModal = ({ user, onClose, onLogout, orders }) => { |
6 | 48 | return ( |
7 | 49 | <div className="overlay-system"> |
8 | | - <motion.div |
9 | | - className="backdrop" |
10 | | - initial={{ opacity: 0 }} |
11 | | - animate={{ opacity: 1 }} |
12 | | - exit={{ opacity: 0 }} |
13 | | - onClick={onClose} |
14 | | - /> |
15 | | - <motion.div |
16 | | - className="drawer" |
17 | | - initial={{ x: '100%' }} |
18 | | - animate={{ x: 0 }} |
19 | | - exit={{ x: '100%' }} |
20 | | - transition={{ type: 'spring', damping: 25, stiffness: 200 }} |
21 | | - > |
| 50 | + <motion.div className="backdrop" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} onClick={onClose} /> |
| 51 | + <motion.div className="drawer" initial={{ x: '100%' }} animate={{ x: 0 }} exit={{ x: '100%' }} transition={{ type: 'spring', damping: 25, stiffness: 200 }}> |
| 52 | + |
22 | 53 | <div className="overlay-header"> |
23 | | - <h3>MY ACCOUNT</h3> |
| 54 | + <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}> |
| 55 | + {view !== 'main' && ( |
| 56 | + <button className="close-btn" onClick={() => setView(view === 'cancel' ? 'detail' : 'main')}> |
| 57 | + <ArrowLeft size={20} /> |
| 58 | + </button> |
| 59 | + )} |
| 60 | + <h3> |
| 61 | + {view === 'main' && 'MY ACCOUNT'} |
| 62 | + {view === 'detail' && 'ORDER DETAILS'} |
| 63 | + {view === 'cancel' && 'CANCEL ORDER'} |
| 64 | + </h3> |
| 65 | + </div> |
24 | 66 | <button className="close-btn" onClick={onClose}><X size={24} /></button> |
25 | 67 | </div> |
26 | 68 |
|
27 | 69 | <div className="account-body"> |
28 | | - {/* Profile Card */} |
29 | | - <div className="account-profile-card"> |
30 | | - <img src={user.picture} alt={user.name} className="account-avatar" /> |
31 | | - <div> |
32 | | - <p className="account-name">{user.name}</p> |
33 | | - <p className="account-email">{user.email}</p> |
34 | | - </div> |
35 | | - </div> |
36 | 70 |
|
37 | | - {/* My Orders */} |
38 | | - <div className="account-section"> |
39 | | - <h4 className="account-section-title"><Package size={16} /> MY ORDERS</h4> |
40 | | - {orders && orders.length > 0 ? ( |
41 | | - <div className="orders-list"> |
42 | | - {orders.map((order, i) => ( |
43 | | - <div key={i} className="order-item"> |
44 | | - <img src={order.image} alt={order.name} /> |
45 | | - <div className="order-info"> |
46 | | - <p className="order-name">{order.name}</p> |
47 | | - <p className="order-meta">Size: UK {order.size} · Qty: {order.qty}</p> |
48 | | - <p className="order-price">₹ {(order.price * order.qty).toLocaleString('en-IN')}</p> |
49 | | - </div> |
50 | | - <span className="order-status">Placed</span> |
| 71 | + {/* ── MAIN VIEW ── */} |
| 72 | + {view === 'main' && ( |
| 73 | + <> |
| 74 | + <div className="account-profile-card"> |
| 75 | + <img src={user.picture} alt={user.name} className="account-avatar" /> |
| 76 | + <div> |
| 77 | + <p className="account-name">{user.name}</p> |
| 78 | + <p className="account-email">{user.email}</p> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div className="account-section"> |
| 83 | + <h4 className="account-section-title"><Package size={16} /> MY ORDERS</h4> |
| 84 | + {orders && orders.length > 0 ? ( |
| 85 | + <div className="orders-list"> |
| 86 | + {orders.map((order, i) => ( |
| 87 | + <div key={i} className="order-item" onClick={() => { setSelectedOrder(order); setView('detail'); }} style={{ cursor: 'pointer' }}> |
| 88 | + <img src={order.image} alt={order.name} /> |
| 89 | + <div className="order-info"> |
| 90 | + <p className="order-name">{order.name}</p> |
| 91 | + <p className="order-meta">Size: UK {order.size} · Qty: {order.qty}</p> |
| 92 | + <p className="order-price">₹ {(order.price * order.qty).toLocaleString('en-IN')}</p> |
| 93 | + </div> |
| 94 | + <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 6 }}> |
| 95 | + <span className={`order-status ${order.status === 'Cancelled' ? 'cancelled' : ''}`}> |
| 96 | + {order.status || 'Placed'} |
| 97 | + </span> |
| 98 | + <ChevronRight size={16} color="#aaa" /> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ))} |
51 | 102 | </div> |
52 | | - ))} |
| 103 | + ) : ( |
| 104 | + <div className="orders-empty"> |
| 105 | + <Package size={36} /> |
| 106 | + <p>No orders yet</p> |
| 107 | + <span>Your placed orders will appear here</span> |
| 108 | + </div> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + |
| 112 | + <button className="account-logout-btn" onClick={() => { onLogout(); onClose(); }}> |
| 113 | + <LogOut size={16} /> LOGOUT |
| 114 | + </button> |
| 115 | + </> |
| 116 | + )} |
| 117 | + |
| 118 | + {/* ── ORDER DETAIL VIEW ── */} |
| 119 | + {view === 'detail' && selectedOrder && ( |
| 120 | + <div className="order-detail-view"> |
| 121 | + <div className="od-header"> |
| 122 | + <div><span className="od-label">Order ID :</span> <span className="od-value">{selectedOrder.orderId}</span></div> |
| 123 | + <div><span className="od-label">Date :</span> <span className="od-value">{formatDate(selectedOrder.placedAt)}</span></div> |
53 | 124 | </div> |
54 | | - ) : ( |
55 | | - <div className="orders-empty"> |
56 | | - <Package size={36} /> |
57 | | - <p>No orders yet</p> |
58 | | - <span>Your placed orders will appear here</span> |
| 125 | + |
| 126 | + <div className="od-item"> |
| 127 | + <img src={selectedOrder.image} alt={selectedOrder.name} /> |
| 128 | + <div className="od-item-info"> |
| 129 | + <p className="od-item-name">{selectedOrder.name}</p> |
| 130 | + <p className="od-item-meta">{selectedOrder.category}</p> |
| 131 | + <p className="od-item-meta">Size: UK {selectedOrder.size} · Qty: {selectedOrder.qty}</p> |
| 132 | + <p className="od-item-price">₹ {(selectedOrder.price * selectedOrder.qty).toLocaleString('en-IN')}</p> |
| 133 | + {selectedOrder.status !== 'Cancelled' && ( |
| 134 | + <button className="od-cancel-btn" onClick={() => setView('cancel')}>Cancel</button> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + |
| 139 | + {/* Tracking */} |
| 140 | + {selectedOrder.status !== 'Cancelled' ? ( |
| 141 | + <div className="od-tracking"> |
| 142 | + {TRACKING_STEPS.map((step, i) => ( |
| 143 | + <div key={step} className="track-step"> |
| 144 | + <div className={`track-dot ${i === 0 ? 'active' : ''}`} /> |
| 145 | + {i < TRACKING_STEPS.length - 1 && <div className="track-line" />} |
| 146 | + <div className="track-info"> |
| 147 | + <p className={`track-label ${i === 0 ? 'active' : ''}`}>{step}</p> |
| 148 | + {i === 0 && <p className="track-date">{formatDate(selectedOrder.placedAt)}</p>} |
| 149 | + {i === TRACKING_STEPS.length - 1 && <p className="track-date">Est. Delivery by {getEstDelivery(selectedOrder.placedAt)}</p>} |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ))} |
| 153 | + </div> |
| 154 | + ) : ( |
| 155 | + <div className="od-cancelled-badge">● Order Cancelled</div> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + )} |
| 159 | + |
| 160 | + {/* ── CANCEL VIEW ── */} |
| 161 | + {view === 'cancel' && selectedOrder && ( |
| 162 | + <div className="cancel-view"> |
| 163 | + <div className="od-header"> |
| 164 | + <div><span className="od-label">Order ID :</span> <span className="od-value">{selectedOrder.orderId}</span></div> |
| 165 | + <div><span className="od-label">Date :</span> <span className="od-value">{formatDate(selectedOrder.placedAt)}</span></div> |
59 | 166 | </div> |
60 | | - )} |
61 | | - </div> |
62 | 167 |
|
63 | | - {/* Logout */} |
64 | | - <button className="account-logout-btn" onClick={() => { onLogout(); onClose(); }}> |
65 | | - <LogOut size={16} /> LOGOUT |
66 | | - </button> |
| 168 | + <div className="od-item"> |
| 169 | + <img src={selectedOrder.image} alt={selectedOrder.name} /> |
| 170 | + <div className="od-item-info"> |
| 171 | + <p className="od-item-name">{selectedOrder.name}</p> |
| 172 | + <p className="od-item-meta">{selectedOrder.category}</p> |
| 173 | + <p className="od-item-meta">Size: UK {selectedOrder.size} · Qty: {selectedOrder.qty}</p> |
| 174 | + <p className="od-item-price">₹ {(selectedOrder.price * selectedOrder.qty).toLocaleString('en-IN')}</p> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + |
| 178 | + <div className="cancel-form"> |
| 179 | + <h4>Reason for Cancellation</h4> |
| 180 | + <p>Please tell us the reason for cancellation as it will help us serve you better in the future.</p> |
| 181 | + <select value={cancelReason} onChange={e => setCancelReason(e.target.value)}> |
| 182 | + <option value="">Select reason *</option> |
| 183 | + {CANCEL_REASONS.map(r => <option key={r} value={r}>{r}</option>)} |
| 184 | + </select> |
| 185 | + <label>Additional Remarks</label> |
| 186 | + <textarea value={remarks} onChange={e => setRemarks(e.target.value)} placeholder="Optional..." rows={3} /> |
| 187 | + </div> |
| 188 | + |
| 189 | + <div className="cancel-btns"> |
| 190 | + <button className="btn-outline cancel-back-btn" onClick={() => setView('detail')}>BACK</button> |
| 191 | + <button className="btn-primary cancel-confirm-btn" onClick={handleCancelConfirm}>CONFIRM</button> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + )} |
| 195 | + |
67 | 196 | </div> |
| 197 | + |
| 198 | + {/* Confirmation Popup */} |
| 199 | + <AnimatePresence> |
| 200 | + {showConfirmPopup && ( |
| 201 | + <motion.div className="cancel-popup-overlay" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}> |
| 202 | + <motion.div className="cancel-popup" initial={{ scale: 0.9 }} animate={{ scale: 1 }} exit={{ scale: 0.9 }}> |
| 203 | + <p>Your cancellation request has been received. You will also receive an email regarding the same with further instructions.</p> |
| 204 | + <hr /> |
| 205 | + <button className="cancel-popup-btn" onClick={handleContinueAfterCancel}>Continue</button> |
| 206 | + </motion.div> |
| 207 | + </motion.div> |
| 208 | + )} |
| 209 | + </AnimatePresence> |
| 210 | + |
68 | 211 | </motion.div> |
69 | 212 | </div> |
70 | 213 | ); |
|
0 commit comments