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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | "use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { Icon } from "@/components/ui/icons"; import { Button } from "@/components/ui"; import { DemoBadge } from "@/components/ui/DemoBadge"; interface OrderItem { quantity: number; price: number; product: { title: string }; } interface Order { id: number; userId: number; status: string; total: number; isDemo: boolean; createdAt: string; updatedAt: string; user: { name: string | null; email: string }; items: OrderItem[]; } interface OrdersResponse { success: boolean; data: { orders: Order[]; pagination: { page: number; limit: number; total: number; pages: number; }; }; } type DemoFilter = "all" | "real" | "demo"; type StatusFilter = "" | "PROCESSING" | "SHIPPED" | "DELIVERED" | "CANCELLED"; export default function AdminOrdersPage() { const [orders, setOrders] = useState<Order[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [demoFilter, setDemoFilter] = useState<DemoFilter>("all"); const [statusFilter, setStatusFilter] = useState<StatusFilter>(""); useEffect(() => { fetchOrders(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [page, demoFilter, statusFilter]); const fetchOrders = async () => { setLoading(true); setError(null); try { const params = new URLSearchParams(); params.set("page", page.toString()); params.set("limit", "20"); if (statusFilter) { params.set("status", statusFilter); } if (demoFilter === "real") { params.set("showDemo", "false"); } else if (demoFilter === "demo") { params.set("demoOnly", "true"); } const res = await fetch(`/api/admin/orders?${params.toString()}`); const data: OrdersResponse = await res.json(); if (!res.ok) { throw new Error("Failed to fetch orders"); } setOrders(data.data?.orders || []); setTotalPages(data.data?.pagination?.pages || 1); } 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: "short", day: "numeric", hour: "2-digit", minute: "2-digit"}); }; 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"; } }; return ( <div className="p-6"> {/* Header */} <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6"> <div> <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">Orders</h1> <p className="text-gray-600 dark:text-gray-400 mt-1"> Manage and track all customer orders </p> </div> </div> {/* Filters */} <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4 mb-6"> <div className="flex flex-wrap gap-4"> {/* Demo Filter */} <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Order Type </label> <select value={demoFilter} onChange={(e) => { setDemoFilter(e.target.value as DemoFilter); setPage(1); }} className="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > <option value="all">All Orders</option> <option value="real">Real Orders Only</option> <option value="demo">Demo Orders Only</option> </select> </div> {/* Status Filter */} <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Status </label> <select value={statusFilter} onChange={(e) => { setStatusFilter(e.target.value as StatusFilter); setPage(1); }} className="px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > <option value="">All Statuses</option> <option value="PROCESSING">Processing</option> <option value="SHIPPED">Shipped</option> <option value="DELIVERED">Delivered</option> <option value="CANCELLED">Cancelled</option> </select> </div> {/* Refresh Button */} <div className="flex items-end"> <Button variant="outline" size="sm" onClick={() => fetchOrders()} disabled={loading} > <Icon name="reload" className={`w-4 h-4 mr-2 ${loading ? "animate-spin" : ""}`} /> Refresh </Button> </div> </div> </div> {/* Error State */} {error && ( <div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4 mb-6"> <p className="text-red-700 dark:text-red-400">{error}</p> <Button variant="outline" size="sm" onClick={fetchOrders} className="mt-2"> Try Again </Button> </div> )} {/* Loading State */} {loading && ( <div className="flex items-center justify-center h-64"> <Icon name="reload" className="w-8 h-8 animate-spin text-indigo-600" /> </div> )} {/* Orders Table */} {!loading && !error && ( <> {orders.length === 0 ? ( <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" /> <p className="text-gray-600 dark:text-gray-400">No orders found</p> </div> ) : ( <div className="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"> Customer </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"> {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"> <span className="font-semibold text-gray-900 dark:text-gray-100"> #{order.id} </span> {order.isDemo && <DemoBadge />} </div> </td> <td className="px-4 py-4"> <div> <p className="font-medium text-gray-900 dark:text-gray-100"> {order.user.name || "Guest"} </p> <p className="text-sm text-gray-500 dark:text-gray-400"> {order.user.email} </p> </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 text-gray-600 dark:text-gray-400"> {order.items.reduce((sum, item) => sum + item.quantity, 0)} </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={`/admin/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> {/* Pagination */} {totalPages > 1 && ( <div className="flex items-center justify-between px-4 py-3 border-t border-gray-200 dark:border-gray-700"> <div className="text-sm text-gray-600 dark:text-gray-400"> Page {page} of {totalPages} </div> <div className="flex gap-2"> <Button variant="outline" size="sm" onClick={() => setPage((p) => Math.max(1, p - 1))} disabled={page === 1} > Previous </Button> <Button variant="outline" size="sm" onClick={() => setPage((p) => Math.min(totalPages, p + 1))} disabled={page === totalPages} > Next </Button> </div> </div> )} </div> )} </> )} </div> ); } |