I'm trying to create a paginated table using Nextjs, Prisma and SWR. The table will show a list of invoices ordered by their ID. This is an example of how it would look:
I'm using Prisma to fetch all the data to the api route and SWR to display the data on the UI and revalidate.This is the code for the UI so far:
{InvoiceData.map((invoice, key) => {
return (
<tr key={key}>
<td className="font-medium">
#
{invoice.nro_factura &&
Number(invoice.nro_factura).toString()}
</td>
<td className="whitespace-pre-wrap">
{invoice.fecha_emision &&
moment(invoice.fecha_emision).format("MMM DD, YYYY")}
</td>
<td className="font-medium">
${invoice.total && Number(invoice.total).toLocaleString()}
</td>
<td>
<Badge
status={
invoice.total === invoice.pagos
? 0
: invoice.anulada
? 2
: 1
}
title={
invoice.total === invoice.pagos
? "Pagado"
: invoice.anulada
? "Borrada"
: "Pendiente"
}
/>
</td>
<td className="customer-column">
{CustomersData.map((customer) => {
if (customer.cliente_id === invoice.cliente_id) {
return customer.nombre_clte;
}
return null;
})}
</td>
</tr>
);
})}
This is the code inside the api route:
import { prisma } from "../../../prisma/index.ts";
export default async function getInvoicesData(req, res) {
let InvoiceData = await prisma.factura.findMany({
orderBy: {
nro_factura: "desc",
},
});
return res.status(200).json(InvoiceData);
}


