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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import React, { useEffect, useState } from "react";
import { Icon } from "@/components/ui/icons";
import type { IconName } from "@/components/ui/icons/registry";
import { Button } from "@/components/ui";
import toast from "react-hot-toast";
interface LoyaltyTier {
id: number;
name: string;
minPoints: number;
pointsMultiplier: number;
perks: string[] | null;
isCurrentTier: boolean;
isAchieved: boolean;
}
interface LoyaltyTransaction {
id: number;
type: string;
points: number;
description: string | null;
createdAt: string;
}
interface BonusRule {
id: number;
name: string;
triggerType: string;
bonusPoints: number;
}
interface LoyaltyData {
enrolled: boolean;
message?: string;
program?: {
id: number;
name: string;
pointsPerDollar: number;
redemptionRate: number;
};
points?: {
current: number;
lifetime: number;
value: number;
};
tier?: {
id: number;
name: string;
minPoints: number;
pointsMultiplier: number;
perks: string[] | null;
} | null;
nextTier?: {
id: number;
name: string;
minPoints: number;
pointsToReach: number;
progressPercent: number;
} | null;
allTiers?: LoyaltyTier[];
bonusRules?: BonusRule[];
transactions?: LoyaltyTransaction[];
referralCode?: string | null;
}
export default function LoyaltyTab() {
const [data, setData] = useState<LoyaltyData | null>(null);
const [loading, setLoading] = useState(true);
const [copied, setCopied] = useState(false);
useEffect(() => {
fetchLoyaltyData();
}, []);
const fetchLoyaltyData = async () => {
try {
const res = await fetch("/api/user/loyalty");
const result = await res.json();
if (result.success) {
setData(result.data);
} else {
toast.error("Failed to load loyalty data");
}
} catch {
toast.error("Failed to load loyalty data");
} finally {
setLoading(false);
}
};
const copyReferralCode = () => {
if (data?.referralCode) {
navigator.clipboard.writeText(
`${window.location.origin}?ref=${data.referralCode}`
);
setCopied(true);
toast.success("Referral link copied!");
setTimeout(() => setCopied(false), 2000);
}
};
const getTierIcon = (tierName: string): IconName => {
const name = tierName.toLowerCase();
if (name.includes("platinum") || name.includes("diamond")) return "star";
if (name.includes("gold")) return "star";
if (name.includes("silver")) return "star";
return "star";
};
const getTierColor = (tierName: string, isAchieved: boolean) => {
if (!isAchieved) return "text-gray-400 dark:text-gray-600";
const name = tierName.toLowerCase();
if (name.includes("platinum") || name.includes("diamond"))
return "text-purple-500";
if (name.includes("gold")) return "text-yellow-500";
if (name.includes("silver")) return "text-gray-400";
return "text-orange-400";
};
const getTransactionIcon = (type: string): IconName => {
switch (type) {
case "earn":
return "plus";
case "redeem":
return "minus";
case "bonus":
return "gift";
case "expire":
return "clock";
default:
return "refresh";
}
};
const getTransactionColor = (type: string) => {
switch (type) {
case "earn":
case "bonus":
return "text-green-500";
case "redeem":
case "expire":
return "text-red-500";
default:
return "text-gray-500";
}
};
if (loading) {
return (
<div className="xl:max-w-[770px] w-full bg-white dark:bg-gray-800 rounded-xl shadow-1 p-8">
<div className="flex items-center justify-center py-12">
<Icon name="reload" size={32} className="animate-spin text-primary" />
</div>
</div>
);
}
if (!data?.enrolled) {
return (
<div className="xl:max-w-[770px] w-full bg-white dark:bg-gray-800 rounded-xl shadow-1 p-8">
<div className="text-center py-12">
<Icon
name="gift"
size={48}
className="mx-auto mb-4 text-gray-400 dark:text-gray-600"
/>
<h3 className="text-xl font-semibold text-dark dark:text-gray-100 mb-2">
No Active Loyalty Program
</h3>
<p className="text-gray-600 dark:text-gray-400">
{data?.message || "Check back later for rewards!"}
</p>
</div>
</div>
);
}
return (
<div className="xl:max-w-[770px] w-full space-y-6">
{/* Points Overview Card */}
<div className="bg-gradient-to-br from-primary to-primary/80 rounded-xl shadow-1 p-6 text-white">
<div className="flex items-center justify-between mb-6">
<div>
<h2 className="text-2xl font-bold">{data.program?.name}</h2>
<p className="text-white/80">
Earn {data.program?.pointsPerDollar} point
{data.program?.pointsPerDollar !== 1 ? "s" : ""} per $1 spent
</p>
</div>
{data.tier && (
<div className="text-right">
<div className="flex items-center gap-2 justify-end">
<Icon
name={getTierIcon(data.tier.name)}
size={24}
className="text-yellow-300"
/>
<span className="text-xl font-bold">{data.tier.name}</span>
</div>
{data.tier.pointsMultiplier > 1 && (
<p className="text-white/80 text-sm">
{data.tier.pointsMultiplier}x points multiplier
</p>
)}
</div>
)}
</div>
<div className="grid grid-cols-3 gap-4">
<div className="bg-white/10 rounded-lg p-4 text-center">
<div className="text-3xl font-bold">
{data.points?.current.toLocaleString()}
</div>
<div className="text-sm text-white/80">Available Points</div>
</div>
<div className="bg-white/10 rounded-lg p-4 text-center">
<div className="text-3xl font-bold">
${data.points?.value.toFixed(2)}
</div>
<div className="text-sm text-white/80">Points Value</div>
</div>
<div className="bg-white/10 rounded-lg p-4 text-center">
<div className="text-3xl font-bold">
{data.points?.lifetime.toLocaleString()}
</div>
<div className="text-sm text-white/80">Lifetime Points</div>
</div>
</div>
{/* Progress to next tier */}
{data.nextTier && (
<div className="mt-6">
<div className="flex justify-between text-sm mb-2">
<span>Progress to {data.nextTier.name}</span>
<span>
{data.nextTier.pointsToReach.toLocaleString()} points to go
</span>
</div>
<div className="h-3 bg-white/20 rounded-full overflow-hidden">
<div
className="h-full bg-white rounded-full transition-all duration-500"
style={{ width: `${data.nextTier.progressPercent}%` }}
/>
</div>
</div>
)}
</div>
{/* Tiers Section */}
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-1 p-6">
<h3 className="text-lg font-semibold text-dark dark:text-gray-100 mb-4">
Membership Tiers
</h3>
<div className="grid grid-cols-2 sm:grid-cols-4 gap-4">
{data.allTiers?.map((tier) => (
<div
key={tier.id}
className={`relative p-4 rounded-lg border-2 transition-all ${
tier.isCurrentTier
? "border-primary bg-primary/5 dark:bg-primary/10"
: tier.isAchieved
? "border-green-500 bg-green-50 dark:bg-green-900/20"
: "border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900"
}`}
>
{tier.isCurrentTier && (
<span className="absolute -top-2 -right-2 bg-primary text-white text-xs px-2 py-0.5 rounded-full">
Current
</span>
)}
<div className="text-center">
<Icon
name={getTierIcon(tier.name)}
size={32}
className={`mx-auto mb-2 ${getTierColor(
tier.name,
tier.isAchieved
)}`}
/>
<h4 className="font-semibold text-dark dark:text-gray-100">
{tier.name}
</h4>
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
{tier.minPoints.toLocaleString()} pts
</p>
{tier.pointsMultiplier > 1 && (
<p className="text-xs text-primary mt-1">
{tier.pointsMultiplier}x points
</p>
)}
</div>
</div>
))}
</div>
</div>
{/* Current Tier Perks */}
{data.tier?.perks && Array.isArray(data.tier.perks) && data.tier.perks.length > 0 && (
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-1 p-6">
<h3 className="text-lg font-semibold text-dark dark:text-gray-100 mb-4">
Your {data.tier.name} Benefits
</h3>
<div className="grid sm:grid-cols-2 gap-3">
{data.tier.perks.map((perk, index) => (
<div
key={index}
className="flex items-center gap-3 p-3 bg-green-50 dark:bg-green-900/20 rounded-lg"
>
<Icon
name="check-circle"
size={20}
className="text-green-500 flex-shrink-0"
/>
<span className="text-sm text-dark dark:text-gray-200">
{perk}
</span>
</div>
))}
</div>
</div>
)}
{/* Ways to Earn Points */}
{data.bonusRules && data.bonusRules.length > 0 && (
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-1 p-6">
<h3 className="text-lg font-semibold text-dark dark:text-gray-100 mb-4">
Ways to Earn Bonus Points
</h3>
<div className="space-y-3">
{data.bonusRules.map((rule) => (
<div
key={rule.id}
className="flex items-center justify-between p-3 bg-gray-50 dark:bg-gray-900 rounded-lg"
>
<div className="flex items-center gap-3">
<Icon
name="gift"
size={20}
className="text-primary"
/>
<span className="text-dark dark:text-gray-200">
{rule.name}
</span>
</div>
<span className="font-bold text-primary">
+{rule.bonusPoints} pts
</span>
</div>
))}
</div>
</div>
)}
{/* Referral Section */}
{data.referralCode && (
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-1 p-6">
<h3 className="text-lg font-semibold text-dark dark:text-gray-100 mb-4">
Refer Friends & Earn
</h3>
<p className="text-gray-600 dark:text-gray-400 mb-4">
Share your referral link and earn bonus points when friends make
their first purchase!
</p>
<div className="flex gap-3">
<div className="flex-1 bg-gray-100 dark:bg-gray-900 rounded-lg px-4 py-3 font-mono text-sm text-dark dark:text-gray-200 truncate">
{typeof window !== "undefined"
? `${window.location.origin}?ref=${data.referralCode}`
: `?ref=${data.referralCode}`}
</div>
<Button
variant="primary"
onClick={copyReferralCode}
leftIcon={
<Icon name={(copied ? "check" : "copy") as IconName} size={18} />
}
>
{copied ? "Copied!" : "Copy"}
</Button>
</div>
</div>
)}
{/* Recent Transactions */}
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-1 p-6">
<h3 className="text-lg font-semibold text-dark dark:text-gray-100 mb-4">
Recent Activity
</h3>
{data.transactions && data.transactions.length > 0 ? (
<div className="space-y-3">
{data.transactions.map((tx) => (
<div
key={tx.id}
className="flex items-center justify-between p-3 bg-gray-50 dark:bg-gray-900 rounded-lg"
>
<div className="flex items-center gap-3">
<div
className={`w-8 h-8 rounded-full flex items-center justify-center ${
tx.points > 0
? "bg-green-100 dark:bg-green-900/30"
: "bg-red-100 dark:bg-red-900/30"
}`}
>
<Icon
name={getTransactionIcon(tx.type)}
size={16}
className={getTransactionColor(tx.type)}
/>
</div>
<div>
<p className="text-sm font-medium text-dark dark:text-gray-200">
{tx.description || tx.type.charAt(0).toUpperCase() + tx.type.slice(1)}
</p>
<p className="text-xs text-gray-500 dark:text-gray-400">
{new Date(tx.createdAt).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric"})}
</p>
</div>
</div>
<span
className={`font-bold ${
tx.points > 0
? "text-green-500"
: "text-red-500"
}`}
>
{tx.points > 0 ? "+" : ""}
{tx.points.toLocaleString()} pts
</span>
</div>
))}
</div>
) : (
<div className="text-center py-8 text-gray-500 dark:text-gray-400">
<Icon
name="clock"
size={32}
className="mx-auto mb-2 text-gray-400 dark:text-gray-600"
/>
<p>No activity yet. Start shopping to earn points!</p>
</div>
)}
</div>
</div>
);
}
|