Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
congle-engineer committed May 1, 2024
1 parent 0e3445a commit c5a5f9f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/dashboard/invoices/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import Form from '@/app/ui/invoices/edit-form';
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
import { fetchInvoiceById, fetchCustomers } from '@/app/lib/data';
import { fetchInvoiceById, fetchCustomers, fetchAllInvoices } from '@/app/lib/data';
import { notFound } from 'next/navigation';

export async function generateStaticParams() {
const invoices = await fetchAllInvoices();
console.log(`invoices: ${invoices}`);
return invoices.map((item) => ({ id: item.id }));
}

export default async function Page({ params }: { params: { id: string } }) {
// const invoices = await fetchAllInvoices();
// console.log(`invoices: ${JSON.stringify(invoices, null, 4)}`);

const id = params.id;
const [invoice, customers] = await Promise.all([
fetchInvoiceById(id),
Expand Down
19 changes: 19 additions & 0 deletions app/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,22 @@ export async function getUser(email: string) {
throw new Error('Failed to fetch user.');
}
}

export async function fetchAllInvoices() {
noStore();

try {
const data = await sql<Invoice>`
SELECT invoices.id
FROM invoices
ORDER BY invoices.date DESC`;

const allInvoices = data.rows.map((invoice) => ({
...invoice,
}));
return allInvoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch the latest invoices.');
}
}

0 comments on commit c5a5f9f

Please sign in to comment.