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 | "use client"; import { useState, useEffect, useCallback } from "react"; interface RewardEntry { id: number; referralCode: string; type: "referrer" | "referee"; userId: number; user: { id: number; email: string; name: string } | null; reward: { type: string; value: number; discountType?: string }; status: string; programName: string; createdAt: string; } interface PaginationInfo { page: number; limit: number; total: number; totalPages: number; } export default function ReferralRewards() { const [rewards, setRewards] = useState<RewardEntry[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [pagination, setPagination] = useState<PaginationInfo>({ page: 1, limit: 20, total: 0, totalPages: 0}); const [typeFilter, setTypeFilter] = useState(""); const fetchRewards = useCallback(async () => { setLoading(true); setError(null); try { const params = new URLSearchParams({ page: pagination.page.toString(), limit: pagination.limit.toString()}); if (typeFilter) params.append("type", typeFilter); const res = await fetch(`/api/admin/referrals/rewards?${params}`); const data = await res.json(); if (!res.ok || data.success === false) { throw new Error(data.error || "Failed to fetch rewards"); } setRewards(data.data || []); setPagination((prev) => ({ ...prev, total: data.pagination?.total || 0, totalPages: data.pagination?.totalPages || 0})); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch rewards"); } finally { setLoading(false); } }, [pagination.page, pagination.limit, typeFilter]); useEffect(() => { fetchRewards(); }, [fetchRewards]); const formatReward = (reward: { type: string; value: number; discountType?: string }) => { switch (reward.type) { case "points": return `${reward.value} points`; case "credit": return `$${reward.value} credit`; case "discount": return reward.discountType === "percentage" ? `${reward.value}% off` : `$${reward.value} off`; default: return `${reward.value}`; } }; return ( <div className="space-y-6"> <div> <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">Referral Rewards</h1> <p className="text-gray-600 dark:text-gray-400 mt-1">View reward history</p> </div> <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4"> <select value={typeFilter} onChange={(e) => { setTypeFilter(e.target.value); setPagination((prev) => ({ ...prev, page: 1 })); }} className="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > <option value="">All Types</option> <option value="referrer">Referrer Rewards</option> <option value="referee">Referee Rewards</option> </select> </div> {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>} <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">Date</th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">User</th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">Type</th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">Reward</th> <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">Campaign</th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {loading ? ( <tr><td colSpan={5} className="px-4 py-8 text-center text-gray-500">Loading...</td></tr> ) : rewards.length === 0 ? ( <tr><td colSpan={5} className="px-4 py-8 text-center text-gray-500">No rewards found</td></tr> ) : ( rewards.map((reward) => ( <tr key={`${reward.id}-${reward.type}`} className="hover:bg-gray-50 dark:hover:bg-gray-700"> <td className="px-4 py-3 text-gray-600 dark:text-gray-400"> {new Date(reward.createdAt).toLocaleDateString()} </td> <td className="px-4 py-3"> {reward.user ? ( <div> <p className="font-medium text-gray-900 dark:text-gray-100 text-sm">{reward.user.name}</p> <p className="text-xs text-gray-500">{reward.user.email}</p> </div> ) : <span className="text-gray-400">—</span>} </td> <td className="px-4 py-3"> <span className={`px-2 py-1 text-xs font-medium rounded-full capitalize ${ reward.type === "referrer" ? "bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400" : "bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-400" }`}> {reward.type} </span> </td> <td className="px-4 py-3 text-gray-900 dark:text-gray-100">{formatReward(reward.reward)}</td> <td className="px-4 py-3 text-gray-600 dark:text-gray-400">{reward.programName}</td> </tr> )) )} </tbody> </table> </div> {pagination.totalPages > 1 && ( <div className="px-4 py-3 border-t border-gray-200 dark:border-gray-700 flex items-center justify-between"> <p className="text-sm text-gray-600 dark:text-gray-400"> Showing {(pagination.page - 1) * pagination.limit + 1} to {Math.min(pagination.page * pagination.limit, pagination.total)} of {pagination.total} </p> <div className="flex gap-2"> <button onClick={() => setPagination((prev) => ({ ...prev, page: prev.page - 1 }))} disabled={pagination.page === 1} className="px-3 py-1 border border-gray-300 dark:border-gray-600 rounded text-gray-700 dark:text-gray-300 disabled:opacity-50">Previous</button> <button onClick={() => setPagination((prev) => ({ ...prev, page: prev.page + 1 }))} disabled={pagination.page === pagination.totalPages} className="px-3 py-1 border border-gray-300 dark:border-gray-600 rounded text-gray-700 dark:text-gray-300 disabled:opacity-50">Next</button> </div> </div> )} </div> </div> ); } |