This commit is contained in:
2026-05-11 17:51:47 +03:00
parent 922cc1bcfc
commit a50e525de1
13 changed files with 4709 additions and 96 deletions
+37 -2
View File
@@ -1,3 +1,38 @@
export default function Page() { import Pagination from '@/app/ui/invoices/pagination';
return <p>Invoices Page</p>; import Search from '@/app/ui/search';
import Table from '@/app/ui/invoices/table';
import { CreateInvoice } from '@/app/ui/invoices/buttons';
import { lusitana } from '@/app/ui/fonts';
import { Suspense } from 'react';
import { InvoicesTableSkeleton } from '@/app/ui/skeletons';
import { fetchInvoicesPages } from '@/app/lib/data';
export default async function Page(props: {
searchParams?: Promise<{
query?: string;
page?: string;
}>;
}) {
const searchParams = await props.searchParams;
const query = searchParams?.query || '';
const currentPage = Number(searchParams?.page) || 1;
const totalPages = await fetchInvoicesPages(query);
return (
<div className="w-full">
<div className="flex w-full items-center justify-between">
<h1 className={`${lusitana.className} text-2xl`}>Invoices</h1>
</div>
<div className="mt-4 flex items-center justify-between gap-2 md:mt-8">
<Search placeholder="Search invoices..." />
<CreateInvoice />
</div>
<Suspense key={query + currentPage} fallback={<InvoicesTableSkeleton />}>
<Table query={query} currentPage={currentPage} />
</Suspense>
<div className="mt-5 flex w-full justify-center">
<Pagination totalPages={totalPages} />
</div>
</div>
);
} }
-3
View File
@@ -1,3 +0,0 @@
export default function Page() {
return <p>Dashboard Page</p>;
}
+4 -3
View File
@@ -18,12 +18,12 @@ export async function fetchRevenue() {
// Artificially delay a reponse for demo purposes. // Artificially delay a reponse for demo purposes.
// Don't do this in real life :) // Don't do this in real life :)
// console.log('Fetching revenue data...'); console.log('Fetching revenue data...');
// await new Promise((resolve) => setTimeout(resolve, 3000)); await new Promise((resolve) => setTimeout(resolve, 3000));
const data = sql<Revenue[]>`SELECT * FROM revenue`; const data = sql<Revenue[]>`SELECT * FROM revenue`;
// console.log('Data fetch complete after 3 seconds.'); console.log('Data fetch complete after 3 seconds.');
return data; return data;
} catch (error) { } catch (error) {
@@ -162,6 +162,7 @@ export async function fetchInvoiceById(id: string) {
amount: invoice.amount / 100, amount: invoice.amount / 100,
})); }));
console.log(invoice); // Invoice is an empty array []
return invoice[0]; return invoice[0];
} catch (error) { } catch (error) {
console.error('Database Error:', error); console.error('Database Error:', error);
+11 -5
View File
@@ -5,6 +5,7 @@ import {
InboxIcon, InboxIcon,
} from '@heroicons/react/24/outline'; } from '@heroicons/react/24/outline';
import { lusitana } from '@/app/ui/fonts'; import { lusitana } from '@/app/ui/fonts';
import { fetchCardData } from '@/app/lib/data';
const iconMap = { const iconMap = {
collected: BanknotesIcon, collected: BanknotesIcon,
@@ -13,19 +14,24 @@ const iconMap = {
invoices: InboxIcon, invoices: InboxIcon,
}; };
export default async function Cards() { export default async function CardWrapper() {
const {
numberOfInvoices,
numberOfCustomers,
totalPaidInvoices,
totalPendingInvoices,
} = await fetchCardData();
return ( return (
<> <>
{/* NOTE: comment in this code when you get to this point in the course */} <Card title="Collected" value={totalPaidInvoices} type="collected" />
{/* <Card title="Collected" value={totalPaidInvoices} type="collected" />
<Card title="Pending" value={totalPendingInvoices} type="pending" /> <Card title="Pending" value={totalPendingInvoices} type="pending" />
<Card title="Total Invoices" value={numberOfInvoices} type="invoices" /> <Card title="Total Invoices" value={numberOfInvoices} type="invoices" />
<Card <Card
title="Total Customers" title="Total Customers"
value={numberOfCustomers} value={numberOfCustomers}
type="customers" type="customers"
/> */} />
</> </>
); );
} }
+7 -7
View File
@@ -3,11 +3,11 @@ import clsx from 'clsx';
import Image from 'next/image'; import Image from 'next/image';
import { lusitana } from '@/app/ui/fonts'; import { lusitana } from '@/app/ui/fonts';
import { LatestInvoice } from '@/app/lib/definitions'; import { LatestInvoice } from '@/app/lib/definitions';
export default async function LatestInvoices({ import { fetchLatestInvoices } from '@/app/lib/data';
latestInvoices,
}: { export default async function LatestInvoices() { // Remove props
latestInvoices: LatestInvoice[]; const latestInvoices = await fetchLatestInvoices();
}) {
return ( return (
<div className="flex w-full flex-col md:col-span-4 lg:col-span-4"> <div className="flex w-full flex-col md:col-span-4 lg:col-span-4">
<h2 className={`${lusitana.className} mb-4 text-xl md:text-2xl`}> <h2 className={`${lusitana.className} mb-4 text-xl md:text-2xl`}>
@@ -16,7 +16,7 @@ export default async function LatestInvoices({
<div className="flex grow flex-col justify-between rounded-xl bg-gray-50 p-4"> <div className="flex grow flex-col justify-between rounded-xl bg-gray-50 p-4">
{/* NOTE: comment in this code when you get to this point in the course */} {/* NOTE: comment in this code when you get to this point in the course */}
{/* <div className="bg-white px-6"> { <div className="bg-white px-6">
{latestInvoices.map((invoice, i) => { {latestInvoices.map((invoice, i) => {
return ( return (
<div <div
@@ -53,7 +53,7 @@ export default async function LatestInvoices({
</div> </div>
); );
})} })}
</div> */} </div> }
<div className="flex items-center pb-2 pt-6"> <div className="flex items-center pb-2 pt-6">
<ArrowPathIcon className="h-5 w-5 text-gray-500" /> <ArrowPathIcon className="h-5 w-5 text-gray-500" />
<h3 className="ml-2 text-sm text-gray-500 ">Updated just now</h3> <h3 className="ml-2 text-sm text-gray-500 ">Updated just now</h3>
+10 -11
View File
@@ -2,6 +2,7 @@ import { generateYAxis } from '@/app/lib/utils';
import { CalendarIcon } from '@heroicons/react/24/outline'; import { CalendarIcon } from '@heroicons/react/24/outline';
import { lusitana } from '@/app/ui/fonts'; import { lusitana } from '@/app/ui/fonts';
import { Revenue } from '@/app/lib/definitions'; import { Revenue } from '@/app/lib/definitions';
import { fetchRevenue } from '@/app/lib/data';
// This component is representational only. // This component is representational only.
// For data visualization UI, check out: // For data visualization UI, check out:
@@ -9,19 +10,17 @@ import { Revenue } from '@/app/lib/definitions';
// https://www.chartjs.org/ // https://www.chartjs.org/
// https://airbnb.io/visx/ // https://airbnb.io/visx/
export default async function RevenueChart({ export default async function RevenueChart() { // Make component async, remove the props
revenue, const revenue = await fetchRevenue(); // Fetch data inside the component
}: {
revenue: Revenue[];
}) {
const chartHeight = 350; const chartHeight = 350;
//NOTE: comment in this code when you get to this point in the course //NOTE: comment in this code when you get to this point in the course
// const { yAxisLabels, topLabel } = generateYAxis(revenue); const { yAxisLabels, topLabel } = generateYAxis(revenue);
// if (!revenue || revenue.length === 0) { if (!revenue || revenue.length === 0) {
// return <p className="mt-4 text-gray-400">No data available.</p>; return <p className="mt-4 text-gray-400">No data available.</p>;
// } }
return ( return (
<div className="w-full md:col-span-4"> <div className="w-full md:col-span-4">
@@ -30,7 +29,7 @@ export default async function RevenueChart({
</h2> </h2>
{/* NOTE: comment in this code when you get to this point in the course */} {/* NOTE: comment in this code when you get to this point in the course */}
{/* <div className="rounded-xl bg-gray-50 p-4"> { <div className="rounded-xl bg-gray-50 p-4">
<div className="sm:grid-cols-13 mt-0 grid grid-cols-12 items-end gap-2 rounded-md bg-white p-4 md:gap-4"> <div className="sm:grid-cols-13 mt-0 grid grid-cols-12 items-end gap-2 rounded-md bg-white p-4 md:gap-4">
<div <div
className="mb-6 hidden flex-col justify-between text-sm text-gray-400 sm:flex" className="mb-6 hidden flex-col justify-between text-sm text-gray-400 sm:flex"
@@ -59,7 +58,7 @@ export default async function RevenueChart({
<CalendarIcon className="h-5 w-5 text-gray-500" /> <CalendarIcon className="h-5 w-5 text-gray-500" />
<h3 className="ml-2 text-sm text-gray-500 ">Last 12 months</h3> <h3 className="ml-2 text-sm text-gray-500 ">Last 12 months</h3>
</div> </div>
</div> */} </div> }
</div> </div>
); );
} }
+9 -5
View File
@@ -1,5 +1,7 @@
import { PencilIcon, PlusIcon, TrashIcon } from '@heroicons/react/24/outline'; import { PencilIcon, PlusIcon, TrashIcon } from '@heroicons/react/24/outline';
import Link from 'next/link'; import Link from 'next/link';
import { deleteInvoice } from '@/app/lib/actions';
export function CreateInvoice() { export function CreateInvoice() {
return ( return (
@@ -16,7 +18,7 @@ export function CreateInvoice() {
export function UpdateInvoice({ id }: { id: string }) { export function UpdateInvoice({ id }: { id: string }) {
return ( return (
<Link <Link
href="/dashboard/invoices" href={`/dashboard/invoices/${id}/edit`}
className="rounded-md border p-2 hover:bg-gray-100" className="rounded-md border p-2 hover:bg-gray-100"
> >
<PencilIcon className="w-5" /> <PencilIcon className="w-5" />
@@ -25,12 +27,14 @@ export function UpdateInvoice({ id }: { id: string }) {
} }
export function DeleteInvoice({ id }: { id: string }) { export function DeleteInvoice({ id }: { id: string }) {
const deleteInvoiceWithId = deleteInvoice.bind(null, id);
return ( return (
<> <form action={deleteInvoiceWithId}>
<button className="rounded-md border p-2 hover:bg-gray-100"> <button type="submit" className="rounded-md border p-2 hover:bg-gray-100">
<span className="sr-only">Delete</span> <span className="sr-only">Delete</span>
<TrashIcon className="w-5" /> <TrashIcon className="w-4" />
</button> </button>
</> </form>
); );
} }
+16 -1
View File
@@ -9,10 +9,16 @@ import {
UserCircleIcon, UserCircleIcon,
} from '@heroicons/react/24/outline'; } from '@heroicons/react/24/outline';
import { Button } from '../button'; import { Button } from '../button';
import { createInvoice, State } from '@/app/lib/actions';
import { useActionState } from 'react';
export default function Form({ customers }: { customers: CustomerField[] }) { export default function Form({ customers }: { customers: CustomerField[] }) {
const initialState: State = { message: null, errors: {} };
const [state, formAction] = useActionState(createInvoice, initialState);
return ( return (
<form> <form action={createInvoice}>
<div className="rounded-md bg-gray-50 p-4 md:p-6"> <div className="rounded-md bg-gray-50 p-4 md:p-6">
{/* Customer Name */} {/* Customer Name */}
<div className="mb-4"> <div className="mb-4">
@@ -25,6 +31,7 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
name="customerId" name="customerId"
className="peer block w-full rounded-md border border-gray-200 py-2 pl-10 text-sm outline-2 placeholder:text-gray-500" className="peer block w-full rounded-md border border-gray-200 py-2 pl-10 text-sm outline-2 placeholder:text-gray-500"
defaultValue="" defaultValue=""
aria-describedby="customer-error"
> >
<option value="" disabled> <option value="" disabled>
Select a customer Select a customer
@@ -37,6 +44,14 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
</select> </select>
<UserCircleIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500" /> <UserCircleIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500" />
</div> </div>
<div id="customer-error" aria-live="polite" aria-atomic="true">
{state.errors?.customerId &&
state.errors.customerId.map((error: string) => (
<p className="mt-2 text-sm text-red-500" key={error}>
{error}
</p>
))}
</div>
</div> </div>
{/* Invoice Amount */} {/* Invoice Amount */}
+13 -5
View File
@@ -4,17 +4,25 @@ import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
import clsx from 'clsx'; import clsx from 'clsx';
import Link from 'next/link'; import Link from 'next/link';
import { generatePagination } from '@/app/lib/utils'; import { generatePagination } from '@/app/lib/utils';
import { usePathname, useSearchParams } from 'next/navigation';
export default function Pagination({ totalPages }: { totalPages: number }) { export default function Pagination({ totalPages }: { totalPages: number }) {
// NOTE: comment in this code when you get to this point in the course const pathname = usePathname();
const searchParams = useSearchParams();
const currentPage = Number(searchParams.get('page')) || 1;
// const allPages = generatePagination(currentPage, totalPages); const createPageURL = (pageNumber: number | string) => {
const params = new URLSearchParams(searchParams);
params.set('page', pageNumber.toString());
return `${pathname}?${params.toString()}`;
};
const allPages = generatePagination(currentPage, totalPages);
return ( return (
<> <>
{/* NOTE: comment in this code when you get to this point in the course */}
{/* <div className="inline-flex"> <div className="inline-flex">
<PaginationArrow <PaginationArrow
direction="left" direction="left"
href={createPageURL(currentPage - 1)} href={createPageURL(currentPage - 1)}
@@ -47,7 +55,7 @@ export default function Pagination({ totalPages }: { totalPages: number }) {
href={createPageURL(currentPage + 1)} href={createPageURL(currentPage + 1)}
isDisabled={currentPage >= totalPages} isDisabled={currentPage >= totalPages}
/> />
</div> */} </div>
</> </>
); );
} }
+25 -1
View File
@@ -1,8 +1,28 @@
'use client'; 'use client';
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline'; import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
import { useSearchParams, usePathname, useRouter } from 'next/navigation';
import { useDebouncedCallback } from 'use-debounce';
export default function Search({ placeholder }: { placeholder: string }) { var placeholder = '';
export default function Search() {
const searchParams = useSearchParams();
const pathname = usePathname();
const { replace } = useRouter();
const handleSearch = useDebouncedCallback((term) => {
console.log(`Searching... ${term}`);
const params = new URLSearchParams(searchParams);
params.set('page', '1');
if (term) {
params.set('query', term);
} else {
params.delete('query');
}
replace(`${pathname}?${params.toString()}`);
}, 300);
return ( return (
<div className="relative flex flex-1 flex-shrink-0"> <div className="relative flex flex-1 flex-shrink-0">
<label htmlFor="search" className="sr-only"> <label htmlFor="search" className="sr-only">
@@ -11,6 +31,10 @@ export default function Search({ placeholder }: { placeholder: string }) {
<input <input
className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500" className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500"
placeholder={placeholder} placeholder={placeholder}
onChange={(e) => {
handleSearch(e.target.value);
}}
defaultValue={searchParams.get('query')?.toString()}
/> />
<MagnifyingGlassIcon className="absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" /> <MagnifyingGlassIcon className="absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
</div> </div>
+7 -3
View File
@@ -3,16 +3,18 @@
"scripts": { "scripts": {
"build": "next build", "build": "next build",
"dev": "next dev --turbopack", "dev": "next dev --turbopack",
"start": "next start" "start": "next start",
"lint": "eslint ."
}, },
"dependencies": { "dependencies": {
"@heroicons/react": "^2.2.0", "@heroicons/react": "^2.2.0",
"@tailwindcss/forms": "^0.5.10", "@tailwindcss/forms": "^0.5.10",
"autoprefixer": "10.4.20", "autoprefixer": "10.4.20",
"babel": "^5.8.38",
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
"clsx": "^2.1.1", "clsx": "^2.1.1",
"next": "latest", "next": "latest",
"next-auth": "5.0.0-beta.25", "next-auth": "5.0.0-beta.31",
"postcss": "8.5.1", "postcss": "8.5.1",
"postgres": "^3.4.6", "postgres": "^3.4.6",
"react": "latest", "react": "latest",
@@ -26,7 +28,9 @@
"@types/bcrypt": "^5.0.2", "@types/bcrypt": "^5.0.2",
"@types/node": "22.10.7", "@types/node": "22.10.7",
"@types/react": "19.0.7", "@types/react": "19.0.7",
"@types/react-dom": "19.0.3" "@types/react-dom": "19.0.3",
"eslint": "^10.3.0",
"eslint-config-next": "^16.2.6"
}, },
"pnpm": { "pnpm": {
"onlyBuiltDependencies": [ "onlyBuiltDependencies": [
+4556 -44
View File
File diff suppressed because it is too large Load Diff
+12 -4
View File
@@ -1,7 +1,11 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2017", "target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"], "lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true, "allowJs": true,
"skipLibCheck": true, "skipLibCheck": true,
"strict": true, "strict": true,
@@ -11,7 +15,7 @@
"moduleResolution": "bundler", "moduleResolution": "bundler",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "react-jsx", "jsx": "preserve",
"incremental": true, "incremental": true,
"plugins": [ "plugins": [
{ {
@@ -19,7 +23,9 @@
} }
], ],
"paths": { "paths": {
"@/*": ["./*"] "@/*": [
"./*"
]
} }
}, },
"include": [ "include": [
@@ -31,5 +37,7 @@
"scripts/seed.js", "scripts/seed.js",
".next/dev/types/**/*.ts" ".next/dev/types/**/*.ts"
], ],
"exclude": ["node_modules"] "exclude": [
"node_modules"
]
} }