Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | "use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import Image from "next/image"; import { Icon } from "@/components/ui/icons"; import { DemoBadge } from "@/components/ui/DemoBadge"; interface OrderItem { id: number; quantity: number; price: number; product: { id: number; title: string; imgs?: { thumbnails?: string[] }; }; } interface Order { id: number; status: string; total: number; createdAt: string; items: OrderItem[]; isDemo?: boolean; } export default function AccountOrders() { const [orders, setOrders] = useState<Order[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); useEffect(() => { fetchOrders(); }, []); const fetchOrders = async () => { setLoading(true); setError(null); try { const res = await fetch("/api/orders"); const result = await res.json(); if (!res.ok) { throw new Error(result.error || "Failed to fetch orders"); } // Handle API response format: { success: true, data: orders } const orders = result?.data ?? result; setOrders(Array.isArray(orders) ? orders : []); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch orders"); } finally { setLoading(false); } }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric"}); }; const formatCurrency = (amount: number) => { return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD"}).format(amount); }; const getStatusColor = (status: string) => { switch (status.toUpperCase()) { case "PROCESSING": return "bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-400"; case "SHIPPED": return "bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400"; case "DELIVERED": return "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400"; case "CANCELLED": return "bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-400"; default: return "bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400"; } }; if (loading) { return ( <div className="flex items-center justify-center h-64"> <div className="text-center"> <Icon name="reload" className="w-8 h-8 animate-spin text-indigo-600 mx-auto mb-2" /> <p className="text-gray-500">Loading your orders...</p> </div> </div> ); } if (error) { return ( <div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-6 text-center"> <Icon name="alert-circle" className="w-12 h-12 text-red-500 mx-auto mb-4" /> <p className="text-red-700 dark:text-red-400 mb-4">{error}</p> <button onClick={fetchOrders} className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700" > Try Again </button> </div> ); } if (orders.length === 0) { return ( <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-8 text-center"> <Icon name="shopping-bag" className="w-16 h-16 text-gray-300 dark:text-gray-600 mx-auto mb-4" /> <h2 className="text-xl font-semibold text-gray-900 dark:text-gray-100 mb-2"> No orders yet </h2> <p className="text-gray-600 dark:text-gray-400 mb-6"> You haven't placed any orders yet. Start shopping to see your orders here. </p> <Link href="/shop" className="inline-flex items-center gap-2 px-6 py-3 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors" > <Icon name="shopping-bag" className="w-5 h-5" /> Browse Products </Link> </div> ); } return ( <div className="space-y-6"> {/* Header */} <div> <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">My Orders</h1> <p className="text-gray-600 dark:text-gray-400 mt-1"> View and track your order history </p> </div> {/* Orders List - Cards for Mobile, Table for Desktop */} {/* Mobile View: Cards */} <div className="space-y-4 md:hidden"> {(Array.isArray(orders) ? orders : []).map((order) => ( <div key={order.id} className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4" > <div className="flex items-center justify-between mb-3"> <div> <div className="flex items-center gap-2"> <p className="font-semibold text-gray-900 dark:text-gray-100"> Order #{order.id} </p> {order.isDemo && <DemoBadge />} </div> <p className="text-sm text-gray-500">{formatDate(order.createdAt)}</p> </div> <span className={`px-2 py-1 text-xs font-medium rounded-full ${getStatusColor( order.status )}`} > {order.status} </span> </div> {/* Items Preview */} <div className="flex items-center gap-2 mb-3"> {order.items.slice(0, 3).map((item) => ( <div key={item.id} className="w-12 h-12 bg-gray-100 dark:bg-gray-700 rounded overflow-hidden" > {item.product.imgs?.thumbnails?.[0] ? ( <Image src={item.product.imgs.thumbnails[0]} alt={item.product.title} width={48} height={48} className="w-full h-full object-cover" /> ) : ( <div className="w-full h-full flex items-center justify-center"> <Icon name="package" className="w-6 h-6 text-gray-400" /> </div> )} </div> ))} {order.items.length > 3 && ( <span className="text-sm text-gray-500"> +{order.items.length - 3} more </span> )} </div> <div className="flex items-center justify-between pt-3 border-t border-gray-200 dark:border-gray-700"> <p className="font-semibold text-gray-900 dark:text-gray-100"> {formatCurrency(order.total)} </p> <Link href={`/account/orders/${order.id}`} className="text-indigo-600 dark:text-indigo-400 hover:underline text-sm font-medium" > View Details </Link> </div> </div> ))} </div> {/* Desktop View: Table */} <div className="hidden md:block bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden"> <div className="overflow-x-auto"> <table className="w-full"> <thead className="bg-gray-50 dark:bg-gray-900"> <tr> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400"> Order </th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400"> Date </th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400"> Status </th> <th className="px-4 py-3 text-center text-sm font-medium text-gray-600 dark:text-gray-400"> Items </th> <th className="px-4 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Total </th> <th className="px-4 py-3 text-center text-sm font-medium text-gray-600 dark:text-gray-400"> Actions </th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {(Array.isArray(orders) ? orders : []).map((order) => ( <tr key={order.id} className="hover:bg-gray-50 dark:hover:bg-gray-700" > <td className="px-4 py-4"> <div className="flex items-center gap-2"> <p className="font-semibold text-gray-900 dark:text-gray-100"> #{order.id} </p> {order.isDemo && <DemoBadge />} </div> </td> <td className="px-4 py-4 text-gray-600 dark:text-gray-400"> {formatDate(order.createdAt)} </td> <td className="px-4 py-4"> <span className={`px-2 py-1 text-xs font-medium rounded-full ${getStatusColor( order.status )}`} > {order.status} </span> </td> <td className="px-4 py-4 text-center"> <div className="flex items-center justify-center gap-1"> {order.items.slice(0, 3).map((item) => ( <div key={item.id} className="w-8 h-8 bg-gray-100 dark:bg-gray-700 rounded overflow-hidden" > {item.product.imgs?.thumbnails?.[0] ? ( <Image src={item.product.imgs.thumbnails[0]} alt={item.product.title} width={32} height={32} className="w-full h-full object-cover" /> ) : ( <div className="w-full h-full flex items-center justify-center"> <Icon name="package" className="w-4 h-4 text-gray-400" /> </div> )} </div> ))} {order.items.length > 3 && ( <span className="text-xs text-gray-500 ml-1"> +{order.items.length - 3} </span> )} </div> </td> <td className="px-4 py-4 text-right font-semibold text-gray-900 dark:text-gray-100"> {formatCurrency(order.total)} </td> <td className="px-4 py-4 text-center"> <Link href={`/account/orders/${order.id}`} className="inline-flex items-center gap-1 px-3 py-1 text-sm text-indigo-600 dark:text-indigo-400 hover:bg-indigo-50 dark:hover:bg-indigo-900/20 rounded" > <Icon name="eye" className="w-4 h-4" /> View </Link> </td> </tr> ))} </tbody> </table> </div> </div> </div> ); } |