import { Calendar } from "lucide-react"; import Link from "next/link"; import { redirect } from "next/navigation"; import { getPayments } from "@/actions/payment"; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import type { Payment } 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"; import { Separator } from "./ui/separator"; export async function PaymentsTable({ searchParams, }: { searchParams: Promise<{ [key: string]: unknown; }>; }) { const resolvedParams = await searchParams; 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, payments] = await tryCatch(getPayments(apiParams)); if (error) { if (error.message.includes("Unauthorized")) { redirect("/auth/signin"); } else { return
{JSON.stringify(error, null, 2)}
; } } const { data, meta } = payments; return (
{data?.length === 0 ? (

No Payments yet.

) : ( <>
Table of all devices. Details Duration Amount {payments?.data?.map((payment) => (
{new Date(payment.created_at).toLocaleDateString( "en-US", { month: "short", day: "2-digit", year: "numeric", }, )}
{payment.paid ? "Paid" : "Unpaid"}

Devices

    {payment.devices.map((device) => (
  1. {device.name}
  2. ))}
{payment.number_of_months} Months {payment.amount.toFixed(2)} MVR
))}
{meta?.total === 1 ? (

Total {meta?.total} payment.

) : (

Total {meta?.total} payments.

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

Devices

    {payment.devices.map((device) => (
  1. {device.name}
  2. ))}

Duration

{payment.number_of_months} Months

Amount

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