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 | "use client"; import { useState, useEffect } from "react"; import { clientLogger } from "@/lib/logging/clientLogger"; import { Icon } from "@/components/ui/icons"; import { SaleBadge } from "@/components/features/promotions/SaleBadge"; import { CountdownTimer } from "@/components/features/promotions/CountdownTimer"; import type { PromotionWithRelations } from "@/lib/promotions/types"; type FilterType = "all" | "percentage" | "fixed" | "free_shipping" | "bogo" | "flash"; export default function PromotionsPage() { const [promotions, setPromotions] = useState<PromotionWithRelations[]>([]); const [loading, setLoading] = useState(true); const [filter, setFilter] = useState<FilterType>("all"); useEffect(() => { async function fetchPromotions() { try { const response = await fetch("/api/promotions/active"); const data = await response.json(); if (data.success) { setPromotions(data.data || []); } } catch (error) { clientLogger.error("Failed to fetch promotions", error instanceof Error ? error : undefined); } finally { setLoading(false); } } fetchPromotions(); }, []); const formatCurrency = (amount: number) => { return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(amount); }; const filteredPromotions = promotions.filter((promo) => { if (filter === "all") return true; if (filter === "percentage") return promo.type === "PERCENTAGE_OFF"; if (filter === "fixed") return promo.type === "FIXED_AMOUNT_OFF"; if (filter === "free_shipping") return promo.type === "FREE_SHIPPING"; if (filter === "bogo") return promo.type === "BOGO"; if (filter === "flash") return promo.type === "FLASH_SALE"; return true; }); const filterButtons: { value: FilterType; label: string; icon: "grid" | "tag" | "package" | "star" | "clock" }[] = [ { value: "all", label: "All", icon: "grid" }, { value: "percentage", label: "% Off", icon: "tag" }, { value: "fixed", label: "$ Off", icon: "tag" }, { value: "free_shipping", label: "Free Shipping", icon: "package" }, { value: "bogo", label: "BOGO", icon: "star" }, { value: "flash", label: "Flash Sales", icon: "clock" }, ]; const getPromotionBadgeVariant = (type: string) => { switch (type) { case "FLASH_SALE": return "flash"; case "BOGO": return "bogo"; case "FREE_SHIPPING": return "new"; default: return "sale"; } }; const getPromotionDescription = (promo: PromotionWithRelations) => { switch (promo.type) { case "PERCENTAGE_OFF": return `${promo.discountValue}% off`; case "FIXED_AMOUNT_OFF": return `${formatCurrency(promo.discountValue)} off`; case "FREE_SHIPPING": return "Free shipping"; case "BOGO": return "Buy one, get one"; case "BUNDLE": return "Bundle deal"; case "FLASH_SALE": return `Flash sale - ${promo.discountValue}% off`; default: return promo.displayName || promo.name; } }; if (loading) { return ( <div className="container mx-auto px-4 py-8"> <div className="animate-pulse space-y-4"> <div className="h-8 w-48 bg-gray-200 rounded" /> <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> {[1, 2, 3, 4, 5, 6].map((i) => ( <div key={i} className="h-48 bg-gray-200 rounded-lg" /> ))} </div> </div> </div> ); } return ( <div className="container mx-auto px-4 py-8"> {/* Header */} <div className="mb-8"> <h1 className="text-3xl font-bold text-gray-900 mb-2"> Current Promotions </h1> <p className="text-gray-600"> Don't miss out on these amazing deals! </p> </div> {/* Filters */} <div className="flex flex-wrap gap-2 mb-6"> {filterButtons.map((btn) => ( <button key={btn.value} onClick={() => setFilter(btn.value)} className={`flex items-center gap-2 px-4 py-2 rounded-lg transition-colors ${filter === btn.value ? "bg-blue-600 text-white" : "bg-gray-100 text-gray-700 hover:bg-gray-200" }`} > <Icon name={btn.icon} size={16} /> <span className="text-sm font-medium">{btn.label}</span> </button> ))} </div> {/* Promotions Grid */} {filteredPromotions.length === 0 ? ( <div className="text-center py-12"> <Icon name="tag" size={48} className="mx-auto text-gray-300 mb-4" /> <h3 className="text-lg font-medium text-gray-600 mb-2"> No promotions found </h3> <p className="text-gray-500"> {filter === "all" ? "Check back soon for new deals!" : "Try selecting a different category."} </p> </div> ) : ( <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> {filteredPromotions.map((promo) => ( <div key={promo.id} className="bg-white border border-gray-200 rounded-xl overflow-hidden hover:shadow-lg transition-shadow" > {/* Header with badge */} <div className="p-4 bg-gradient-to-r from-gray-50 to-gray-100 border-b border-gray-200"> <div className="flex items-center justify-between"> <SaleBadge variant={getPromotionBadgeVariant(promo.type)} size="md" /> {promo.type === "FLASH_SALE" && promo.endDate && ( <CountdownTimer endDate={promo.endDate} variant="compact" showLabels={false} /> )} </div> </div> {/* Content */} <div className="p-4"> <h3 className="text-lg font-bold text-gray-900 mb-2"> {promo.displayName || promo.name} </h3> <p className="text-2xl font-bold text-green-600 mb-2"> {getPromotionDescription(promo)} </p> {promo.description && ( <p className="text-sm text-gray-600 mb-4"> {promo.description} </p> )} {/* Requirements */} <div className="space-y-2 text-sm text-gray-500"> {promo.minimumPurchase && promo.minimumPurchase > 0 && ( <div className="flex items-center gap-2"> <Icon name="info-circle" size={14} /> <span> Min. purchase: {formatCurrency(promo.minimumPurchase)} </span> </div> )} {promo.usageLimit && ( <div className="flex items-center gap-2"> <Icon name="users" size={14} /> <span> {promo.usageLimit - promo.usageCount} uses remaining </span> </div> )} {promo.endDate && promo.type !== "FLASH_SALE" && ( <div className="flex items-center gap-2"> <Icon name="clock" size={14} /> <span> Expires:{" "} {new Date(promo.endDate).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })} </span> </div> )} </div> {/* Promo codes */} {promo.codes && promo.codes.length > 0 && ( <div className="mt-4 pt-4 border-t border-gray-200"> <p className="text-xs text-gray-500 mb-1">Use code:</p> <code className="inline-block px-3 py-1 bg-gray-100 text-gray-800 font-mono font-bold rounded"> {promo.codes[0].code} </code> </div> )} {/* Target products/categories */} {promo.targetType !== "ALL_PRODUCTS" && ( <div className="mt-4 pt-4 border-t border-gray-200"> {promo.targetCategories && promo.targetCategories.length > 0 && ( <p className="text-xs text-gray-500"> Applies to:{" "} {promo.targetCategories .map((tc) => tc.category.title) .join(", ")} </p> )} </div> )} </div> </div> ))} </div> )} </div> ); } |