import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import Link from "next/link"; import { getPayments } from "@/actions/payment"; import type { Payment } from "@/lib/backend-types"; import { cn } from "@/lib/utils"; import { tryCatch } from "@/utils/tryCatch"; import { Calendar } from "lucide-react"; import { redirect } from "next/navigation"; 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<{ query: string; page: number; sortBy: string; }>; }) { const query = (await searchParams)?.query || ""; // const session = await auth.api.getSession({ // headers: await headers(), // }); // const query = (await searchParams)?.query || ""; // const page = (await searchParams)?.page; // const totalPayments = await prisma.payment.count({ // where: { // userId: session?.session.userId, // OR: [ // { // devices: { // every: { // name: { // contains: query || "", // mode: "insensitive", // }, // }, // }, // }, // ], // }, // }); // const totalPages = Math.ceil(totalPayments / 10); // const limit = 10; // const offset = (Number(page) - 1) * limit || 0; // const payments = await prisma.payment.findMany({ // where: { // userId: session?.session.userId, // OR: [ // { // devices: { // every: { // name: { // contains: query || "", // mode: "insensitive", // }, // }, // }, // }, // ], // }, // include: { // devices: true, // }, // skip: offset, // take: limit, // orderBy: { // createdAt: "desc", // }, // }); const [error, payments] = await tryCatch(getPayments()); 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
))}
{query.length > 0 && (

Showing {payments?.data?.length} locations for " {query} "

)}
{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
); }