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 | "use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { Button, Card, Badge } from "@/components/ui"; import { Icon } from "@/components/ui/icons"; import { clientLogger } from "@/lib/logging/clientLogger"; import type { PromotionType } from "@prisma/client"; interface Promotion { id: number; name: string; displayName: string | null; type: PromotionType; discountType: string; discountValue: number; startDate: string; endDate: string; isActive: boolean; usageCount: number; usageLimit: number | null; computedStatus: "active" | "inactive" | "expired" | "scheduled"; codesCount: number; } const promotionTypeLabels: Record<string, string> = { PERCENTAGE_OFF: "% Off", FIXED_AMOUNT_OFF: "$ Off", BOGO: "BOGO", FREE_SHIPPING: "Free Shipping", BUNDLE: "Bundle", FREE_GIFT: "Free Gift", TIERED: "Tiered", FLASH_SALE: "Flash Sale", SEASONAL: "Seasonal", FIRST_PURCHASE: "First Purchase"}; const statusColors: Record<string, string> = { active: "bg-green-100 dark:bg-green-900/30 text-green-800 dark:text-green-300", inactive: "bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-300", expired: "bg-red-100 dark:bg-red-900/30 text-red-800 dark:text-red-300", scheduled: "bg-blue-100 dark:bg-blue-900/30 text-blue-800 dark:text-blue-300"}; export default function PromotionsPage() { const [promotions, setPromotions] = useState<Promotion[]>([]); const [loading, setLoading] = useState(true); const [filter, setFilter] = useState<string>("all"); const [search, setSearch] = useState(""); useEffect(() => { fetchPromotions(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [filter, search]); const fetchPromotions = async () => { try { setLoading(true); const params = new URLSearchParams(); if (filter !== "all") params.set("status", filter); if (search) params.set("search", search); const response = await fetch(`/api/admin/promotions?${params}`); const data = await response.json(); if (data.success && data.data?.promotions && Array.isArray(data.data.promotions)) { setPromotions(data.data.promotions); } } catch (error) { clientLogger.error("Error fetching promotions", error instanceof Error ? error : new Error(String(error))); } finally { setLoading(false); } }; const handleToggle = async (id: number) => { try { const response = await fetch(`/api/admin/promotions/${id}/toggle`, { method: "POST"}); const data = await response.json(); if (data.success) { setPromotions((prev) => prev.map((p) => p.id === id ? { ...p, isActive: data.data.isActive, computedStatus: data.data.isActive ? "active" : "inactive" } : p ) ); } } catch (error) { clientLogger.error("Error toggling promotion", error instanceof Error ? error : new Error(String(error))); } }; const formatDiscount = (type: string, value: number) => { if (type === "PERCENTAGE") { return `${value}%`; } return `$${value.toFixed(2)}`; }; const formatDate = (dateStr: string) => { return new Date(dateStr).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric"}); }; return ( <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"> <div className="flex items-center justify-between mb-6"> <div> <h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Promotions</h1> <p className="mt-1 text-sm text-gray-500 dark:text-gray-400"> Manage discounts, promo codes, and special offers </p> </div> <div className="flex gap-2"> <Link href="/admin/promotions/analytics"> <Button variant="secondary"> <Icon name="dashboard" size={16} className="mr-2" /> Analytics </Button> </Link> <Link href="/admin/promotions/new"> <Button> <Icon name="plus" size={16} className="mr-2" /> Create Promotion </Button> </Link> </div> </div> {/* Filters */} <Card className="mb-6 p-4 bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700"> <div className="flex flex-col sm:flex-row gap-4"> <div className="flex-1"> <input type="text" placeholder="Search promotions..." value={search} onChange={(e) => setSearch(e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 placeholder:text-gray-400 dark:placeholder:text-gray-500" /> </div> <div className="flex gap-2"> {["all", "active", "scheduled", "inactive", "expired"].map((status) => ( <Button key={status} variant={filter === status ? "primary" : "ghost"} size="sm" onClick={() => setFilter(status)} > {status.charAt(0).toUpperCase() + status.slice(1)} </Button> ))} </div> </div> </Card> {/* Promotions List */} {loading ? ( <div className="flex justify-center py-12"> <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div> </div> ) : promotions.length === 0 ? ( <Card className="p-12 text-center bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700"> <Icon name="tag" size={48} className="mx-auto text-gray-400 dark:text-gray-500 mb-4" /> <h3 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-2">No promotions found</h3> <p className="text-gray-500 dark:text-gray-400 mb-4"> {filter !== "all" || search ? "Try adjusting your filters or search" : "Create your first promotion to get started"} </p> {filter === "all" && !search && ( <Link href="/admin/promotions/new"> <Button>Create Promotion</Button> </Link> )} </Card> ) : ( <div className="space-y-4"> {promotions.map((promotion) => ( <Card key={promotion.id} className="p-4 bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700"> <div className="flex items-start justify-between"> <div className="flex-1"> <div className="flex items-center gap-3 mb-2"> <h3 className="text-lg font-medium text-gray-900 dark:text-gray-100"> {promotion.displayName || promotion.name} </h3> <Badge className={statusColors[promotion.computedStatus]}> {promotion.computedStatus} </Badge> <Badge variant="secondary"> {promotionTypeLabels[promotion.type] || promotion.type} </Badge> </div> <div className="flex flex-wrap gap-4 text-sm text-gray-500 dark:text-gray-400"> <span className="flex items-center gap-1"> <Icon name="tag" size={14} /> {formatDiscount(promotion.discountType, promotion.discountValue)} </span> <span className="flex items-center gap-1"> <Icon name="clock" size={14} /> {formatDate(promotion.startDate)} - {formatDate(promotion.endDate)} </span> <span className="flex items-center gap-1"> <Icon name="users" size={14} /> {promotion.usageCount} {promotion.usageLimit ? ` / ${promotion.usageLimit}` : ""} uses </span> {promotion.codesCount > 0 && ( <span className="flex items-center gap-1"> <Icon name="tag" size={14} /> {promotion.codesCount} codes </span> )} </div> </div> <div className="flex items-center gap-2"> <Button variant="ghost" size="sm" onClick={() => handleToggle(promotion.id)} title={promotion.isActive ? "Deactivate" : "Activate"} > <Icon name="eye" size={18} className={promotion.isActive ? "text-green-600" : "text-gray-400"} /> </Button> <Link href={`/admin/promotions/${promotion.id}`}> <Button variant="ghost" size="sm"> <Icon name="edit" size={18} /> </Button> </Link> </div> </div> </Card> ))} </div> )} </div> ); } |