Combine data fetches for cards (#197)

This commit is contained in:
Delba de Oliveira
2023-10-05 15:18:08 +01:00
committed by GitHub
parent e4dc33e944
commit 11b0e7c69d
2 changed files with 40 additions and 40 deletions

View File

@@ -1,22 +1,21 @@
import Card from '@/app/ui/dashboard/card';
import RevenueChart from '@/app/ui/dashboard/revenue-chart';
import LatestInvoices from '@/app/ui/dashboard/latest-invoices';
import {
fetchLatestInvoices,
fetchCounts,
fetchTotalAmountByStatus,
} from '@/app/lib/data';
import { Suspense } from 'react';
import { lusitana } from '@/app/ui/fonts';
import { fetchLatestInvoices, fetchCardData } from '@/app/lib/data';
import { Suspense } from 'react';
import { RevenueChartSkeleton } from '@/app/ui/dashboard/skeletons';
export const dynamic = 'force-dynamic';
export default async function Page() {
const latestInvoices = await fetchLatestInvoices();
const { numberOfInvoices, numberOfCustomers } = await fetchCounts();
const { totalPaidInvoices, totalPendingInvoices } =
await fetchTotalAmountByStatus();
const {
numberOfInvoices,
numberOfCustomers,
totalPaidInvoices,
totalPendingInvoices,
} = await fetchCardData();
return (
<main>

View File

@@ -27,37 +27,6 @@ export async function fetchRevenue() {
}
}
export async function fetchCounts() {
try {
const invoiceCount = await sql`SELECT COUNT(*) FROM invoices`;
const numberOfInvoices = parseInt(invoiceCount.rows[0].count, 10);
const customerCount = await sql`SELECT COUNT(*) FROM customers`;
const numberOfCustomers = parseInt(customerCount.rows[0].count, 10);
return { numberOfCustomers, numberOfInvoices };
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch counts.');
}
}
export async function fetchTotalAmountByStatus() {
try {
const data = await sql`SELECT
SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS "paid",
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS "pending"
FROM invoices`;
const totalPaidInvoices = formatCurrency(data.rows[0].paid);
const totalPendingInvoices = formatCurrency(data.rows[0].pending);
return { totalPaidInvoices, totalPendingInvoices };
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch total amounts by status.');
}
}
export async function fetchLatestInvoices() {
try {
const data = await sql<LatestInvoiceRaw>`
@@ -78,6 +47,38 @@ export async function fetchLatestInvoices() {
}
}
export async function fetchCardData() {
try {
const invoiceCountPromise = sql`SELECT COUNT(*) FROM invoices`;
const customerCountPromise = sql`SELECT COUNT(*) FROM customers`;
const invoiceStatusPromise = sql`SELECT
SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS "paid",
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS "pending"
FROM invoices`;
const data = await Promise.all([
invoiceCountPromise,
customerCountPromise,
invoiceStatusPromise,
]);
const numberOfInvoices = Number(data[0].rows[0].count ?? '0');
const numberOfCustomers = Number(data[1].rows[0].count ?? '0');
const totalPaidInvoices = formatCurrency(data[2].rows[0].paid ?? '0');
const totalPendingInvoices = formatCurrency(data[2].rows[0].pending ?? '0');
return {
numberOfCustomers,
numberOfInvoices,
totalPaidInvoices,
totalPendingInvoices,
};
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to card data.');
}
}
export async function fetchFilteredInvoices(
query: string,
currentPage: number,