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 | "use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { Icon } from "@/components/ui/icons"; interface LoyaltyTier { id: number; name: string; minPoints: number; multiplier: number; benefits: string[]; memberCount?: number; } export default function LoyaltyTiers() { const [tiers, setTiers] = useState<LoyaltyTier[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [deletingId, setDeletingId] = useState<number | null>(null); useEffect(() => { fetchTiers(); }, []); const fetchTiers = async () => { setLoading(true); setError(null); try { const res = await fetch("/api/admin/loyalty/tiers"); const data = await res.json(); if (!res.ok || data.success === false) { throw new Error(data.error || "Failed to fetch tiers"); } // Fetch member counts for each tier const tiersWithCounts = await Promise.all( (Array.isArray(data) ? data : data.data || []).map(async (tier: LoyaltyTier) => { try { const countRes = await fetch(`/api/admin/loyalty/tiers/${tier.id}`); const countData = await countRes.json(); return { ...tier, memberCount: countData.data?.memberCount || 0}; } catch { return { ...tier, memberCount: 0 }; } }) ); setTiers(tiersWithCounts); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch tiers"); } finally { setLoading(false); } }; const handleDelete = async (tierId: number) => { if (!confirm("Are you sure you want to delete this tier?")) return; setDeletingId(tierId); try { const res = await fetch(`/api/admin/loyalty/tiers/${tierId}`, { method: "DELETE"}); const data = await res.json(); if (!res.ok || data.success === false) { throw new Error(data.error || "Failed to delete tier"); } fetchTiers(); } catch (err) { alert(err instanceof Error ? err.message : "Failed to delete tier"); } finally { setDeletingId(null); } }; 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 Tiers </h1> <p className="text-gray-600 dark:text-gray-400 mt-1"> Manage loyalty program tiers and benefits </p> </div> <Link href="/admin/loyalty/tiers/new" className="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors flex items-center gap-2" > <Icon name="plus" className="w-5 h-5" /> New Tier </Link> </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 text-red-700 dark:text-red-400"> {error} </div> )} {/* Tiers Table */} <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"> Name </th> <th className="px-4 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Min Points </th> <th className="px-4 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Multiplier </th> <th className="px-4 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400"> Members </th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400"> Benefits </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"> {loading ? ( <tr> <td colSpan={6} className="px-4 py-8 text-center text-gray-500"> Loading... </td> </tr> ) : tiers.length === 0 ? ( <tr> <td colSpan={6} className="px-4 py-8 text-center text-gray-500"> No tiers found. Create your first tier to get started. </td> </tr> ) : ( tiers.map((tier) => ( <tr key={tier.id} className="hover:bg-gray-50 dark:hover:bg-gray-700" > <td className="px-4 py-3"> <span className="font-medium text-gray-900 dark:text-gray-100"> {tier.name} </span> </td> <td className="px-4 py-3 text-right text-gray-900 dark:text-gray-100"> {tier.minPoints.toLocaleString()} </td> <td className="px-4 py-3 text-right"> <span className="px-2 py-1 text-xs font-medium rounded-full bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400"> {tier.multiplier}x </span> </td> <td className="px-4 py-3 text-right text-gray-600 dark:text-gray-400"> {(tier.memberCount || 0).toLocaleString()} </td> <td className="px-4 py-3"> {tier.benefits && tier.benefits.length > 0 ? ( <div className="flex flex-wrap gap-1"> {tier.benefits.slice(0, 2).map((benefit, idx) => ( <span key={idx} className="px-2 py-0.5 text-xs bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400 rounded" > {benefit} </span> ))} {tier.benefits.length > 2 && ( <span className="px-2 py-0.5 text-xs text-gray-500"> +{tier.benefits.length - 2} more </span> )} </div> ) : ( <span className="text-gray-400">—</span> )} </td> <td className="px-4 py-3"> <div className="flex items-center justify-center gap-2"> <Link href={`/admin/loyalty/tiers/${tier.id}/edit`} className="p-1 text-gray-600 dark:text-gray-400 hover:text-indigo-600 dark:hover:text-indigo-400" title="Edit" > <Icon name="edit" className="w-5 h-5" /> </Link> <button onClick={() => handleDelete(tier.id)} disabled={deletingId === tier.id || (tier.memberCount || 0) > 0} className="p-1 text-gray-600 dark:text-gray-400 hover:text-red-600 dark:hover:text-red-400 disabled:opacity-50 disabled:cursor-not-allowed" title={ (tier.memberCount || 0) > 0 ? "Cannot delete tier with members" : "Delete" } > <Icon name="trash" className="w-5 h-5" /> </button> </div> </td> </tr> )) )} </tbody> </table> </div> </div> {/* Info Box */} <div className="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-4"> <div className="flex gap-3"> <Icon name="info-circle" className="w-5 h-5 text-blue-600 dark:text-blue-400 flex-shrink-0 mt-0.5" /> <div className="text-sm text-blue-700 dark:text-blue-300"> <p className="font-medium mb-1">About Loyalty Tiers</p> <p> Tiers are assigned based on lifetime points. Members automatically move to higher tiers when they reach the minimum points threshold. The multiplier increases points earned for members in that tier. </p> </div> </div> </div> </div> ); } |