|
@@ -108,9 +108,12 @@ export default function InvoicesTable() {
{renderInvoiceStatus(invoice.status)}
|
-
+
|
{/*
From 62271d7986281bb4026a65f9b739ea869cda041d Mon Sep 17 00:00:00 2001
From: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
Date: Wed, 6 Sep 2023 16:19:15 +0100
Subject: [PATCH 3/4] Add total paid/pending columns to customer table (#145)
* Delete individual invoice pages
* Add pending / paid totals to customer table
* Misc
---
.../app/dashboard/invoices/[id]/page.tsx | 3 --
dashboard/15-final/app/lib/calculations.tsx | 25 +++++++++-
dashboard/15-final/app/lib/dummy-data.tsx | 7 +++
dashboard/15-final/app/ui/customers/table.tsx | 47 ++++++++++++-------
.../15-final/app/ui/dashboard/overview.tsx | 6 +--
dashboard/15-final/app/ui/invoices/table.tsx | 11 -----
6 files changed, 64 insertions(+), 35 deletions(-)
delete mode 100644 dashboard/15-final/app/dashboard/invoices/[id]/page.tsx
diff --git a/dashboard/15-final/app/dashboard/invoices/[id]/page.tsx b/dashboard/15-final/app/dashboard/invoices/[id]/page.tsx
deleted file mode 100644
index 30de135..0000000
--- a/dashboard/15-final/app/dashboard/invoices/[id]/page.tsx
+++ /dev/null
@@ -1,3 +0,0 @@
-export default function Page() {
- return Individual Invoice Page ;
-}
diff --git a/dashboard/15-final/app/lib/calculations.tsx b/dashboard/15-final/app/lib/calculations.tsx
index 34ab6dc..a31aef9 100644
--- a/dashboard/15-final/app/lib/calculations.tsx
+++ b/dashboard/15-final/app/lib/calculations.tsx
@@ -1,6 +1,6 @@
import { Invoice, Revenue } from "./definitions";
-export const calculateInvoices = (
+export const calculateAllInvoices = (
invoices: Invoice[],
status: "pending" | "paid",
) => {
@@ -13,11 +13,34 @@ export const calculateInvoices = (
});
};
+export const calculateCustomerInvoices = (
+ invoices: Invoice[],
+ status: "pending" | "paid",
+ customerId: number,
+) => {
+ return invoices
+ .filter((invoice) => invoice.customerId === customerId)
+ .filter((invoice) => !status || invoice.status === status)
+ .reduce((total, invoice) => total + invoice.amount / 100, 0)
+ .toLocaleString("en-US", {
+ style: "currency",
+ currency: "USD",
+ });
+};
+
// Once a database is connected, we can use SQL to query the database directly
// This will be more efficient than querying all invoices and then filtering them
// E.g. "SELECT * FROM invoices
// ORDER BY date DESC
// LIMIT 5;"
+
+export const countCustomerInvoices = (
+ invoices: Invoice[],
+ customerId: number,
+) => {
+ return invoices.filter((invoice) => invoice.customerId === customerId).length;
+};
+
export const findLatestInvoices = (invoices: Invoice[]) => {
return [...invoices]
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime())
diff --git a/dashboard/15-final/app/lib/dummy-data.tsx b/dashboard/15-final/app/lib/dummy-data.tsx
index 40b6887..933a940 100644
--- a/dashboard/15-final/app/lib/dummy-data.tsx
+++ b/dashboard/15-final/app/lib/dummy-data.tsx
@@ -84,6 +84,13 @@ export const invoices: Invoice[] = [
id: 7,
customerId: 3,
amount: 8945,
+ status: "pending",
+ date: "2023-06-01",
+ },
+ {
+ id: 8,
+ customerId: 4,
+ amount: 32545,
status: "paid",
date: "2023-06-01",
},
diff --git a/dashboard/15-final/app/ui/customers/table.tsx b/dashboard/15-final/app/ui/customers/table.tsx
index eff06ba..ceb36ff 100644
--- a/dashboard/15-final/app/ui/customers/table.tsx
+++ b/dashboard/15-final/app/ui/customers/table.tsx
@@ -1,13 +1,10 @@
import { customers, invoices } from "@/app/lib/dummy-data";
-import Image from "next/image";
+import {
+ countCustomerInvoices,
+ calculateCustomerInvoices,
+} from "@/app/lib/calculations";
export default function CustomersTable() {
- function totalInvoices(customerId: number) {
- const customerInvoices = invoices.filter((invoice) => {
- return invoice.customerId === customerId
- });
- return customerInvoices.length;
- };
return (
@@ -15,7 +12,7 @@ export default function CustomersTable() {
-
+
@@ -31,19 +28,25 @@ export default function CustomersTable() {
|
Total Invoices
|
+
+ Total Pending
+ |
+
+ Total Paid
+ |
{customers.map((customer) => (
-
-
- 
-
+ |
+
+ 
+
|
{customer.name}
@@ -52,7 +55,17 @@ export default function CustomersTable() {
{customer.email}
|
- {totalInvoices(customer.id)}
+ {countCustomerInvoices(invoices, customer.id)}
+ |
+
+ {calculateCustomerInvoices(
+ invoices,
+ "pending",
+ customer.id,
+ )}
+ |
+
+ {calculateCustomerInvoices(invoices, "paid", customer.id)}
|
))}
diff --git a/dashboard/15-final/app/ui/dashboard/overview.tsx b/dashboard/15-final/app/ui/dashboard/overview.tsx
index 8ff64e0..173005b 100644
--- a/dashboard/15-final/app/ui/dashboard/overview.tsx
+++ b/dashboard/15-final/app/ui/dashboard/overview.tsx
@@ -1,12 +1,12 @@
import Card from "@/app/ui/dashboard/card";
import { invoices, customers, revenue } from "@/app/lib/dummy-data";
-import { calculateInvoices } from "@/app/lib/calculations";
+import { calculateAllInvoices } from "@/app/lib/calculations";
import RevenueChart from "@/app/ui/dashboard/revenue-chart";
import LatestInvoices from "@/app/ui/dashboard/latest-invoices";
export default function DashboardOverview() {
- const totalPaidInvoices = calculateInvoices(invoices, "paid");
- const totalPendingInvoices = calculateInvoices(invoices, "pending");
+ const totalPaidInvoices = calculateAllInvoices(invoices, "paid");
+ const totalPendingInvoices = calculateAllInvoices(invoices, "pending");
const numberOfInvoices = invoices.length;
const numberOfCustomers = customers.length;
diff --git a/dashboard/15-final/app/ui/invoices/table.tsx b/dashboard/15-final/app/ui/invoices/table.tsx
index 466b48e..b7fcac5 100644
--- a/dashboard/15-final/app/ui/invoices/table.tsx
+++ b/dashboard/15-final/app/ui/invoices/table.tsx
@@ -72,9 +72,6 @@ export default function InvoicesTable() {
Edit
|
- {/*
- View
- | */}
@@ -116,14 +113,6 @@ export default function InvoicesTable() {
- {/*
-
- View, {invoice.id}
-
- | */}
))}
From a657dd215b8bb4669f5682619a3eee85b0e8de9d Mon Sep 17 00:00:00 2001
From: Spoof14
Date: Wed, 6 Sep 2023 20:45:10 +0500
Subject: [PATCH 4/4] Adds consistency between `Create a Next.js App` and
`Navigate Between Pages` tutorials (#116)
---
basics/navigate-between-pages-starter/pages/index.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/basics/navigate-between-pages-starter/pages/index.js b/basics/navigate-between-pages-starter/pages/index.js
index 32a91df..0647b88 100644
--- a/basics/navigate-between-pages-starter/pages/index.js
+++ b/basics/navigate-between-pages-starter/pages/index.js
@@ -11,7 +11,7 @@ export default function Home() {
@@ -112,4 +112,4 @@ export default function Home() {
`}
)
-}
\ No newline at end of file
+}
|