Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dashboard/final-example/app/lib/actions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use server';

import { z } from 'zod';
import { sql } from '@vercel/postgres';
import postgres from 'postgres';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { signIn } from '@/auth';
import { AuthError } from 'next-auth';

const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });

const FormSchema = z.object({
id: z.string(),
customerId: z.string({
Expand Down
39 changes: 20 additions & 19 deletions dashboard/final-example/app/lib/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sql } from '@vercel/postgres';
import postgres from 'postgres';
import {
CustomerField,
CustomersTableType,
Expand All @@ -9,6 +9,8 @@ import {
} from './definitions';
import { formatCurrency } from './utils';

const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });

export async function fetchRevenue() {
try {
// Artificially delay a response for demo purposes.
Expand All @@ -17,11 +19,11 @@ export async function fetchRevenue() {
// console.log('Fetching revenue data...');
// await new Promise((resolve) => setTimeout(resolve, 3000));

const data = await sql<Revenue>`SELECT * FROM revenue`;
const data = await sql<Revenue[]>`SELECT * FROM revenue`;

// console.log('Data fetch completed after 3 seconds.');

return data.rows;
return data;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch revenue data.');
Expand All @@ -30,14 +32,14 @@ export async function fetchRevenue() {

export async function fetchLatestInvoices() {
try {
const data = await sql<LatestInvoiceRaw>`
const data = await sql<LatestInvoiceRaw[]>`
SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
ORDER BY invoices.date DESC
LIMIT 5`;

const latestInvoices = data.rows.map((invoice) => ({
const latestInvoices = data.map((invoice) => ({
...invoice,
amount: formatCurrency(invoice.amount),
}));
Expand Down Expand Up @@ -66,10 +68,10 @@ export async function fetchCardData() {
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');
const numberOfInvoices = Number(data[0].count ?? '0');
const numberOfCustomers = Number(data[1].count ?? '0');
const totalPaidInvoices = formatCurrency(data[2][0].paid ?? '0');
const totalPendingInvoices = formatCurrency(data[2][0].pending ?? '0');

return {
numberOfCustomers,
Expand All @@ -91,7 +93,7 @@ export async function fetchFilteredInvoices(
const offset = (currentPage - 1) * ITEMS_PER_PAGE;

try {
const invoices = await sql<InvoicesTable>`
const invoices = await sql<InvoicesTable[]>`
SELECT
invoices.id,
invoices.amount,
Expand All @@ -112,7 +114,7 @@ export async function fetchFilteredInvoices(
LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset}
`;

return invoices.rows;
return invoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch invoices.');
Expand All @@ -121,7 +123,7 @@ export async function fetchFilteredInvoices(

export async function fetchInvoicesPages(query: string) {
try {
const count = await sql`SELECT COUNT(*)
const data = await sql`SELECT COUNT(*)
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
WHERE
Expand All @@ -132,7 +134,7 @@ export async function fetchInvoicesPages(query: string) {
invoices.status ILIKE ${`%${query}%`}
`;

const totalPages = Math.ceil(Number(count.rows[0].count) / ITEMS_PER_PAGE);
const totalPages = Math.ceil(Number(data[0].count) / ITEMS_PER_PAGE);
return totalPages;
} catch (error) {
console.error('Database Error:', error);
Expand All @@ -142,7 +144,7 @@ export async function fetchInvoicesPages(query: string) {

export async function fetchInvoiceById(id: string) {
try {
const data = await sql<InvoiceForm>`
const data = await sql<InvoiceForm[]>`
SELECT
invoices.id,
invoices.customer_id,
Expand All @@ -152,7 +154,7 @@ export async function fetchInvoiceById(id: string) {
WHERE invoices.id = ${id};
`;

const invoice = data.rows.map((invoice) => ({
const invoice = data.map((invoice) => ({
...invoice,
// Convert amount from cents to dollars
amount: invoice.amount / 100,
Expand All @@ -167,15 +169,14 @@ export async function fetchInvoiceById(id: string) {

export async function fetchCustomers() {
try {
const data = await sql<CustomerField>`
const customers = await sql<CustomerField[]>`
SELECT
id,
name
FROM customers
ORDER BY name ASC
`;

const customers = data.rows;
return customers;
} catch (err) {
console.error('Database Error:', err);
Expand All @@ -185,7 +186,7 @@ export async function fetchCustomers() {

export async function fetchFilteredCustomers(query: string) {
try {
const data = await sql<CustomersTableType>`
const data = await sql<CustomersTableType[]>`
SELECT
customers.id,
customers.name,
Expand All @@ -203,7 +204,7 @@ export async function fetchFilteredCustomers(query: string) {
ORDER BY customers.name ASC
`;

const customers = data.rows.map((customer) => ({
const customers = data.map((customer) => ({
...customer,
total_pending: formatCurrency(customer.total_pending),
total_paid: formatCurrency(customer.total_paid),
Expand Down
Loading
Loading