import { Calendar } from "lucide-react"; import Link from "next/link"; import { redirect } from "next/navigation"; import { getTopups } from "@/actions/payment"; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import type { Topup } from "@/lib/backend-types"; import { cn } from "@/lib/utils"; import { tryCatch } from "@/utils/tryCatch"; import Pagination from "./pagination"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; export async function TopupsTable({ searchParams, }: { searchParams: Promise<{ [key: string]: unknown; }>; }) { const resolvedParams = await searchParams; // Build params object const apiParams: Record = {}; for (const [key, value] of Object.entries(resolvedParams)) { if (value !== undefined && value !== "") { apiParams[key] = typeof value === "number" ? value : String(value); } } const [error, topups] = await tryCatch(getTopups(apiParams)); if (error) { if (error.message.includes("Unauthorized")) { redirect("/auth/signin"); } else { return
{JSON.stringify(error, null, 2)}
; } } const { data, meta } = topups; return (
{data?.length === 0 ? (

No topups yet.

) : ( <>
Table of all topups. Details Status Amount {topups?.data?.map((topup) => (
{new Date(topup.created_at).toLocaleDateString( "en-US", { month: "short", day: "2-digit", year: "numeric", minute: "2-digit", hour: "2-digit", }, )}
{!topup.is_expired && ( {topup.paid ? "Paid" : "Unpaid"} )}
{topup.is_expired ? Expired : {topup.status}} {topup.amount.toFixed(2)} MVR
))}
{meta?.total === 1 ? (

Total {meta?.total} topup.

) : (

Total {meta?.total} topups.

)}
{data.map((topup) => ( ))}
)}
); } function MobileTopupDetails({ topup }: { topup: Topup }) { return (
{new Date(topup.created_at).toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", })}
{topup.paid ? "Paid" : "Unpaid"}

Amount

{topup.amount.toFixed(2)} MVR
); }