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 | "use client"; import { useState, useEffect } from "react"; import { Icon } from "@/components/ui/icons"; interface LoyaltyOverview { totalMembers: number; activeMembers: number; pointsIssued: number; pointsRedeemed: number; } interface TierDistribution { tierId: number; name: string; minPoints: number; count: number; } interface MonthlyTrend { month: string; earned: number; redeemed: number; } interface TopEarner { userId: number; name: string; email: string; totalPoints: number; lifetimePoints: number; } interface ReportsData { overview: LoyaltyOverview; tierDistribution: TierDistribution[]; monthlyTrends: MonthlyTrend[]; topEarners: TopEarner[]; } export default function LoyaltyReports() { const [data, setData] = useState<ReportsData | null>(null); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); useEffect(() => { fetchReports(); }, []); const fetchReports = async () => { setLoading(true); setError(null); try { const res = await fetch("/api/admin/loyalty/reports"); const result = await res.json(); if (!res.ok || result.success === false) { throw new Error(result.error || "Failed to fetch reports"); } setData(result.data); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch reports"); } finally { setLoading(false); } }; if (loading) { return ( <div className="flex items-center justify-center h-64"> <p className="text-gray-500">Loading reports...</p> </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-4 text-red-700 dark:text-red-400"> {error} </div> ); } if (!data) return null; const formatNumber = (num: number) => num.toLocaleString(); return ( <div className="space-y-6"> {/* Header */} <div className="flex items-center justify-between"> <div> <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100"> Loyalty Reports </h1> <p className="text-gray-600 dark:text-gray-400 mt-1"> Analytics and insights for your loyalty program </p> </div> <button onClick={fetchReports} className="flex items-center gap-2 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700" > <Icon name="reload" className="w-5 h-5" /> Refresh </button> </div> {/* Stats Cards */} <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"> <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4"> <div className="flex items-center gap-3"> <div className="p-2 bg-blue-100 dark:bg-blue-900/30 rounded-lg"> <Icon name="users" className="w-5 h-5 text-blue-600 dark:text-blue-400" /> </div> <div> <p className="text-sm text-gray-600 dark:text-gray-400">Total Members</p> <p className="text-2xl font-bold text-gray-900 dark:text-gray-100"> {formatNumber(data.overview.totalMembers)} </p> </div> </div> </div> <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4"> <div className="flex items-center gap-3"> <div className="p-2 bg-green-100 dark:bg-green-900/30 rounded-lg"> <Icon name="check-circle" className="w-5 h-5 text-green-600 dark:text-green-400" /> </div> <div> <p className="text-sm text-gray-600 dark:text-gray-400">Active (90 days)</p> <p className="text-2xl font-bold text-gray-900 dark:text-gray-100"> {formatNumber(data.overview.activeMembers)} </p> </div> </div> </div> <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4"> <div className="flex items-center gap-3"> <div className="p-2 bg-purple-100 dark:bg-purple-900/30 rounded-lg"> <Icon name="star" className="w-5 h-5 text-purple-600 dark:text-purple-400" /> </div> <div> <p className="text-sm text-gray-600 dark:text-gray-400">Points Issued</p> <p className="text-2xl font-bold text-gray-900 dark:text-gray-100"> {formatNumber(data.overview.pointsIssued)} </p> </div> </div> </div> <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4"> <div className="flex items-center gap-3"> <div className="p-2 bg-orange-100 dark:bg-orange-900/30 rounded-lg"> <Icon name="tag" className="w-5 h-5 text-orange-600 dark:text-orange-400" /> </div> <div> <p className="text-sm text-gray-600 dark:text-gray-400">Points Redeemed</p> <p className="text-2xl font-bold text-gray-900 dark:text-gray-100"> {formatNumber(data.overview.pointsRedeemed)} </p> </div> </div> </div> </div> {/* Main Content */} <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> {/* Tier Distribution */} <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700"> <div className="p-4 border-b border-gray-200 dark:border-gray-700"> <h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100"> Tier Distribution </h2> </div> <div className="p-4"> <div className="space-y-3"> {data.tierDistribution.map((tier) => { const percentage = data.overview.totalMembers > 0 ? (tier.count / data.overview.totalMembers) * 100 : 0; return ( <div key={tier.tierId} className="flex items-center gap-4"> <div className="w-24 text-sm font-medium text-gray-900 dark:text-gray-100"> {tier.name} </div> <div className="flex-1"> <div className="h-4 bg-gray-100 dark:bg-gray-700 rounded-full overflow-hidden"> <div className="h-full bg-indigo-500 rounded-full" style={{ width: `${percentage}%` }} /> </div> </div> <div className="w-20 text-right text-sm text-gray-600 dark:text-gray-400"> {tier.count} ({percentage.toFixed(1)}%) </div> </div> ); })} </div> </div> </div> {/* Top Earners */} <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700"> <div className="p-4 border-b border-gray-200 dark:border-gray-700"> <h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100"> Top Earners </h2> </div> <div className="overflow-x-auto"> <table className="w-full"> <thead className="bg-gray-50 dark:bg-gray-900"> <tr> <th className="px-4 py-2 text-left text-xs font-medium text-gray-600 dark:text-gray-400"> Member </th> <th className="px-4 py-2 text-right text-xs font-medium text-gray-600 dark:text-gray-400"> Current </th> <th className="px-4 py-2 text-right text-xs font-medium text-gray-600 dark:text-gray-400"> Lifetime </th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {data.topEarners.length === 0 ? ( <tr> <td colSpan={3} className="px-4 py-4 text-center text-gray-500"> No members yet </td> </tr> ) : ( data.topEarners.map((member, index) => ( <tr key={member.userId}> <td className="px-4 py-2"> <div className="flex items-center gap-2"> <span className="w-5 h-5 flex items-center justify-center text-xs font-bold text-gray-500"> {index + 1} </span> <div> <p className="font-medium text-gray-900 dark:text-gray-100 text-sm"> {member.name} </p> <p className="text-xs text-gray-500">{member.email}</p> </div> </div> </td> <td className="px-4 py-2 text-right font-medium text-gray-900 dark:text-gray-100 text-sm"> {formatNumber(member.totalPoints)} </td> <td className="px-4 py-2 text-right text-gray-600 dark:text-gray-400 text-sm"> {formatNumber(member.lifetimePoints)} </td> </tr> )) )} </tbody> </table> </div> </div> </div> {/* Monthly Trends */} <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700"> <div className="p-4 border-b border-gray-200 dark:border-gray-700"> <h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100"> Monthly Activity (Last 6 Months) </h2> </div> <div className="p-4 overflow-x-auto"> <table className="w-full"> <thead> <tr> <th className="px-4 py-2 text-left text-sm font-medium text-gray-600 dark:text-gray-400"> Month </th> <th className="px-4 py-2 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Points Earned </th> <th className="px-4 py-2 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Points Redeemed </th> <th className="px-4 py-2 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Net </th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {data.monthlyTrends.length === 0 ? ( <tr> <td colSpan={4} className="px-4 py-4 text-center text-gray-500"> No data available </td> </tr> ) : ( data.monthlyTrends.map((month) => ( <tr key={month.month} className="hover:bg-gray-50 dark:hover:bg-gray-700"> <td className="px-4 py-2 font-medium text-gray-900 dark:text-gray-100"> {new Date(month.month + "-01").toLocaleDateString("en-US", { year: "numeric", month: "long"})} </td> <td className="px-4 py-2 text-right text-green-600 dark:text-green-400"> +{formatNumber(month.earned)} </td> <td className="px-4 py-2 text-right text-red-600 dark:text-red-400"> -{formatNumber(month.redeemed)} </td> <td className="px-4 py-2 text-right font-medium text-gray-900 dark:text-gray-100"> {formatNumber(month.earned - month.redeemed)} </td> </tr> )) )} </tbody> </table> </div> </div> </div> ); } |