Compare commits
19 Commits
62b58918d0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b6aab0170 | |||
| 5bdf7f05e6 | |||
| 045ba08b17 | |||
| 8e51988395 | |||
| 69f0e86915 | |||
| 1c23830272 | |||
| f3626ec7c0 | |||
| 3e4959dd9d | |||
| 32871e3ff4 | |||
| 3e0842aef2 | |||
| 86036bdf1f | |||
| 4f2ab25f16 | |||
| 500928c719 | |||
| 4df12d45ea | |||
| db28d8d3e7 | |||
| 67e324acae | |||
| 1824c019ab | |||
| c52d9d55bc | |||
| 45036d14ad |
5
app/dashboard/(overview)/loading.tsx
Normal file
5
app/dashboard/(overview)/loading.tsx
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import DashboardSkeleton from '@/app/ui/skeletons';
|
||||||
|
|
||||||
|
export default function Loading() {
|
||||||
|
return <DashboardSkeleton />;
|
||||||
|
}
|
||||||
@@ -2,12 +2,12 @@ import { Card } from '@/app/ui/dashboard/cards';
|
|||||||
import RevenueChart from '@/app/ui/dashboard/revenue-chart';
|
import RevenueChart from '@/app/ui/dashboard/revenue-chart';
|
||||||
import LatestInvoices from '@/app/ui/dashboard/latest-invoices';
|
import LatestInvoices from '@/app/ui/dashboard/latest-invoices';
|
||||||
import { lusitana } from '@/app/ui/fonts';
|
import { lusitana } from '@/app/ui/fonts';
|
||||||
import { fetchRevenue, fetchLatestInvoices, fetchCardData} from "@/app/lib/data";
|
import { fetchCardData} from "@/app/lib/data";
|
||||||
|
import { Suspense } from 'react';
|
||||||
|
import {CardsSkeleton, LatestInvoicesSkeleton, RevenueChartSkeleton} from '@/app/ui/skeletons';
|
||||||
|
import CardWrapper from '@/app/ui/dashboard/cards';
|
||||||
export default async function Page() {
|
export default async function Page() {
|
||||||
|
|
||||||
const revenue = await fetchRevenue();
|
|
||||||
const latestInvoices = await fetchLatestInvoices();
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
numberOfInvoices,
|
numberOfInvoices,
|
||||||
@@ -21,18 +21,17 @@ export default async function Page() {
|
|||||||
Dashboard
|
Dashboard
|
||||||
</h1>
|
</h1>
|
||||||
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-4">
|
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-4">
|
||||||
<Card title="Collected" value={totalPaidInvoices} type="collected" />
|
<Suspense fallback={<CardsSkeleton />}>
|
||||||
<Card title="Pending" value={totalPendingInvoices} type="pending" />
|
<CardWrapper />
|
||||||
<Card title="Total Invoices" value={numberOfInvoices} type="invoices" />
|
</Suspense>
|
||||||
<Card
|
|
||||||
title="Total Customers"
|
|
||||||
value={numberOfCustomers}
|
|
||||||
type="customers"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-6 grid grid-cols-1 gap-6 md:grid-cols-4 lg:grid-cols-8">
|
<div className="mt-6 grid grid-cols-1 gap-6 md:grid-cols-4 lg:grid-cols-8">
|
||||||
<RevenueChart revenue={revenue} />
|
<Suspense fallback={<RevenueChartSkeleton />}>
|
||||||
<LatestInvoices latestInvoices={latestInvoices} />
|
<RevenueChart />
|
||||||
|
</Suspense>
|
||||||
|
<Suspense fallback={<LatestInvoicesSkeleton />}>
|
||||||
|
<LatestInvoices />
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
18
app/dashboard/invoices/[id]/edit/not-found.tsx
Normal file
18
app/dashboard/invoices/[id]/edit/not-found.tsx
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import Link from 'next/link';
|
||||||
|
import { FaceFrownIcon } from '@heroicons/react/24/outline';
|
||||||
|
|
||||||
|
export default function NotFound() {
|
||||||
|
return (
|
||||||
|
<main className="flex h-full flex-col items-center justify-center gap-2">
|
||||||
|
<FaceFrownIcon className="w-10 text-gray-400" />
|
||||||
|
<h2 className="text-xl font-semibold">404 Not Found</h2>
|
||||||
|
<p>Could not find the requested invoice.</p>
|
||||||
|
<Link
|
||||||
|
href="/dashboard/invoices"
|
||||||
|
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
|
||||||
|
>
|
||||||
|
Go Back
|
||||||
|
</Link>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
31
app/dashboard/invoices/[id]/edit/page.tsx
Normal file
31
app/dashboard/invoices/[id]/edit/page.tsx
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import Form from '@/app/ui/invoices/edit-form';
|
||||||
|
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
|
||||||
|
import {fetchCustomers, fetchInvoiceById} from '@/app/lib/data';
|
||||||
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
|
export default async function Page({ params }: { params: { id: string } }) {
|
||||||
|
const id = params.id;
|
||||||
|
const [invoice, customers] = await Promise.all([
|
||||||
|
fetchInvoiceById(id),
|
||||||
|
fetchCustomers(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!invoice) {
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<Breadcrumbs
|
||||||
|
breadcrumbs={[
|
||||||
|
{ label: 'Invoices', href: '/dashboard/invoices' },
|
||||||
|
{
|
||||||
|
label: 'Edit Invoice',
|
||||||
|
href: `/dashboard/invoices/${id}/edit`,
|
||||||
|
active: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
<Form invoice={invoice} customers={customers} />
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
23
app/dashboard/invoices/create/page.tsx
Normal file
23
app/dashboard/invoices/create/page.tsx
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import Form from '@/app/ui/invoices/create-form';
|
||||||
|
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
|
||||||
|
import { fetchCustomers } from '@/app/lib/data';
|
||||||
|
|
||||||
|
export default async function Page() {
|
||||||
|
const customers = await fetchCustomers();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<Breadcrumbs
|
||||||
|
breadcrumbs={[
|
||||||
|
{ label: 'Invoices', href: '/dashboard/invoices' },
|
||||||
|
{
|
||||||
|
label: 'Create Invoice',
|
||||||
|
href: '/dashboard/invoices/create',
|
||||||
|
active: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
<Form customers={customers} />
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
31
app/dashboard/invoices/error.tsx
Normal file
31
app/dashboard/invoices/error.tsx
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
export default function Error({
|
||||||
|
error,
|
||||||
|
reset,
|
||||||
|
}: {
|
||||||
|
error: Error & { digest?: string };
|
||||||
|
reset: () => void;
|
||||||
|
}) {
|
||||||
|
useEffect(() => {
|
||||||
|
// Optionally log the error to an error reporting service
|
||||||
|
console.error(error);
|
||||||
|
}, [error]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="flex h-full flex-col items-center justify-center">
|
||||||
|
<h2 className="text-center">Something went wrong!</h2>
|
||||||
|
<button
|
||||||
|
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
|
||||||
|
onClick={
|
||||||
|
// Attempt to recover by trying to re-render the invoices route
|
||||||
|
() => reset()
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Try again
|
||||||
|
</button>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,3 +1,45 @@
|
|||||||
export default function InvoicesPage() {
|
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 { InvoicesTableSkeleton } from '@/app/ui/skeletons';
|
||||||
|
import { Suspense } from 'react';
|
||||||
|
import {fetchInvoicesPages} from "@/app/lib/data";
|
||||||
|
|
||||||
|
import { Metadata } from 'next';
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: 'Invoices',
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function Page({
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
searchParams?: {
|
||||||
|
query?: string;
|
||||||
|
page?: string;
|
||||||
|
};
|
||||||
|
}) {
|
||||||
|
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>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
BIN
app/favicon.ico
Normal file
BIN
app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
@@ -1,6 +1,17 @@
|
|||||||
import '@/app/ui/global.css';
|
import '@/app/ui/global.css';
|
||||||
import { inter } from '@/app/ui/fonts';
|
import { inter } from '@/app/ui/fonts';
|
||||||
|
|
||||||
|
import { Metadata } from 'next';
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: {
|
||||||
|
template: '%s | Acme Dashboard',
|
||||||
|
default: 'Acme Dashboard',
|
||||||
|
},
|
||||||
|
description: 'The official Next.js Learn Dashboard built with App Router.',
|
||||||
|
metadataBase: new URL('https://next-learn-dashboard.vercel.sh'),
|
||||||
|
};
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
|
|||||||
122
app/lib/actions.ts
Normal file
122
app/lib/actions.ts
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { sql } from '@vercel/postgres';
|
||||||
|
import { revalidatePath } from 'next/cache';
|
||||||
|
import {redirect} from "next/navigation";
|
||||||
|
import { signIn } from '@/auth';
|
||||||
|
import { AuthError } from 'next-auth';
|
||||||
|
|
||||||
|
|
||||||
|
const FormSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
customerId: z.string({
|
||||||
|
invalid_type_error: 'Please select a customer.',
|
||||||
|
}),
|
||||||
|
amount: z.coerce.number().gt(0, { message: 'Please enter an amount greater than $0.' }),
|
||||||
|
status: z.enum(['pending', 'paid'], { invalid_type_error: 'Please select an invoice status'}),
|
||||||
|
date: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const UpdateInvoice = FormSchema.omit({ id: true, date: true });
|
||||||
|
|
||||||
|
const CreateInvoice = FormSchema.omit({ id: true, date: true });
|
||||||
|
|
||||||
|
export type State = {
|
||||||
|
errors?: {
|
||||||
|
customerId?: string[];
|
||||||
|
amount?: string[];
|
||||||
|
status?: string[];
|
||||||
|
};
|
||||||
|
message?: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function createInvoice(prevState: State, formData: FormData) {
|
||||||
|
|
||||||
|
const validatedFields = CreateInvoice.safeParse({
|
||||||
|
customerId: formData.get('customerId'),
|
||||||
|
amount: formData.get('amount'),
|
||||||
|
status: formData.get('status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
// If form validation fails, return errors early. Otherwise, continue.
|
||||||
|
if (!validatedFields.success) {
|
||||||
|
return {
|
||||||
|
errors: validatedFields.error.flatten().fieldErrors,
|
||||||
|
message: 'Missing Fields. Failed to Create Invoice.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const { customerId, amount, status } = validatedFields.data;
|
||||||
|
|
||||||
|
const amountInCents = amount * 100;
|
||||||
|
const date = new Date().toISOString().split('T')[0];
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sql`
|
||||||
|
INSERT INTO invoices (customer_id, amount, status, date)
|
||||||
|
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
|
||||||
|
`;
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
message: 'Database Error: Failed to Create Invoice.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath('/dashboard/invoices');
|
||||||
|
redirect('/dashboard/invoices');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function updateInvoice(id: string, formData: FormData) {
|
||||||
|
const { customerId, amount, status } = UpdateInvoice.parse({
|
||||||
|
customerId: formData.get('customerId'),
|
||||||
|
amount: formData.get('amount'),
|
||||||
|
status: formData.get('status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const amountInCents = amount * 100;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sql`
|
||||||
|
UPDATE invoices
|
||||||
|
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
|
||||||
|
WHERE id = ${id}
|
||||||
|
`;
|
||||||
|
} catch (error) {
|
||||||
|
return { message: 'Database Error: Failed to Update Invoice.' };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath('/dashboard/invoices');
|
||||||
|
redirect('/dashboard/invoices');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteInvoice(id: string) {
|
||||||
|
throw new Error('Failed to Delete Invoice');
|
||||||
|
try {
|
||||||
|
await sql`DELETE FROM invoices WHERE id = ${id}`;
|
||||||
|
revalidatePath('/dashboard/invoices');
|
||||||
|
return { message: 'Deleted Invoice.' };
|
||||||
|
} catch (error) {
|
||||||
|
return { message: 'Database Error: Failed to Delete Invoice.' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function authenticate(
|
||||||
|
prevState: string | undefined,
|
||||||
|
formData: FormData,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await signIn('credentials', formData);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof AuthError) {
|
||||||
|
switch (error.type) {
|
||||||
|
case 'CredentialsSignin':
|
||||||
|
return 'Invalid credentials.';
|
||||||
|
default:
|
||||||
|
return 'Something went wrong.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -169,6 +169,7 @@ export async function fetchInvoiceById(id: string) {
|
|||||||
amount: invoice.amount / 100,
|
amount: invoice.amount / 100,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
console.log(invoice);
|
||||||
return invoice[0];
|
return invoice[0];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Database Error:', error);
|
console.error('Database Error:', error);
|
||||||
|
|||||||
17
app/login/page.tsx
Normal file
17
app/login/page.tsx
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import AcmeLogo from '@/app/ui/acme-logo';
|
||||||
|
import LoginForm from '@/app/ui/login-form';
|
||||||
|
|
||||||
|
export default function LoginPage() {
|
||||||
|
return (
|
||||||
|
<main className="flex items-center justify-center md:h-screen">
|
||||||
|
<div className="relative mx-auto flex w-full max-w-[400px] flex-col space-y-2.5 p-4 md:-mt-32">
|
||||||
|
<div className="flex h-20 w-full items-end rounded-lg bg-blue-500 p-3 md:h-36">
|
||||||
|
<div className="w-32 text-white md:w-36">
|
||||||
|
<AcmeLogo />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<LoginForm />
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
BIN
app/opengraph-image.png
Normal file
BIN
app/opengraph-image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 238 KiB |
@@ -27,9 +27,13 @@ export default function Page() {
|
|||||||
className="flex items-center gap-5 self-start rounded-lg bg-blue-500 px-6 py-3 text-sm font-medium text-white transition-colors hover:bg-blue-400 md:text-base"
|
className="flex items-center gap-5 self-start rounded-lg bg-blue-500 px-6 py-3 text-sm font-medium text-white transition-colors hover:bg-blue-400 md:text-base"
|
||||||
>
|
>
|
||||||
<span>Log in</span> <ArrowRightIcon className="w-5 md:w-6"/>
|
<span>Log in</span> <ArrowRightIcon className="w-5 md:w-6"/>
|
||||||
</Link>
|
</Link><Link href="/dashboard"
|
||||||
|
className="flex items-center gap-5 self-start rounded-lg bg-blue-500 px-6 py-3 text-sm font-medium text-white transition-colors hover:bg-blue-400 md:text-base"
|
||||||
|
><span> Dasbhoard</span> <ArrowRightIcon className="w-5 md:w-6"/></Link>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center justify-center p-6 md:w-3/5 md:px-28 md:py-12">
|
|
||||||
|
<div className="flex items-center justify-center p-6 md:w-3/5 md:px-28 md:py-12">
|
||||||
{/* Add Hero Images Here */}
|
{/* Add Hero Images Here */}
|
||||||
<Image
|
<Image
|
||||||
src="/hero-desktop.png"
|
src="/hero-desktop.png"
|
||||||
|
|||||||
@@ -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,
|
||||||
@@ -14,18 +15,22 @@ const iconMap = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default async function CardWrapper() {
|
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"
|
||||||
/> */}
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +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() {
|
||||||
}: {
|
const latestInvoices = await fetchLatestInvoices();
|
||||||
latestInvoices: LatestInvoice[];
|
|
||||||
}) {
|
return (
|
||||||
return (
|
|
||||||
<div className="flex w-full flex-col md:col-span-4">
|
<div className="flex w-full flex-col md: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`}>
|
||||||
Latest Invoices
|
Latest Invoices
|
||||||
|
|||||||
@@ -3,17 +3,17 @@ 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:
|
||||||
// https://www.tremor.so/
|
// https://www.tremor.so/
|
||||||
// https://www.chartjs.org/
|
// https://www.chartjs.org/
|
||||||
// https://airbnb.io/visx/
|
// https://airbnb.io/visx/
|
||||||
|
|
||||||
export default async function RevenueChart({
|
|
||||||
revenue,
|
export default async function RevenueChart() { // Make component async, remove the props
|
||||||
}: {
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import Link from 'next/link';
|
|||||||
import NavLinks from '@/app/ui/dashboard/nav-links';
|
import NavLinks from '@/app/ui/dashboard/nav-links';
|
||||||
import AcmeLogo from '@/app/ui/acme-logo';
|
import AcmeLogo from '@/app/ui/acme-logo';
|
||||||
import { PowerIcon } from '@heroicons/react/24/outline';
|
import { PowerIcon } from '@heroicons/react/24/outline';
|
||||||
|
import { signOut } from '@/auth';
|
||||||
|
|
||||||
export default function SideNav() {
|
export default function SideNav() {
|
||||||
return (
|
return (
|
||||||
@@ -17,7 +18,10 @@ export default function SideNav() {
|
|||||||
<div className="flex grow flex-row justify-between space-x-2 md:flex-col md:space-x-0 md:space-y-2">
|
<div className="flex grow flex-row justify-between space-x-2 md:flex-col md:space-x-0 md:space-y-2">
|
||||||
<NavLinks />
|
<NavLinks />
|
||||||
<div className="hidden h-auto w-full grow rounded-md bg-gray-50 md:block"></div>
|
<div className="hidden h-auto w-full grow rounded-md bg-gray-50 md:block"></div>
|
||||||
<form>
|
<form action={async () => {
|
||||||
|
'use server';
|
||||||
|
await signOut();
|
||||||
|
}}>
|
||||||
<button className="flex h-[48px] w-full grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3">
|
<button className="flex h-[48px] w-full grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3">
|
||||||
<PowerIcon className="w-6" />
|
<PowerIcon className="w-6" />
|
||||||
<div className="hidden md:block">Sign Out</div>
|
<div className="hidden md:block">Sign Out</div>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
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 +17,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 +26,13 @@ 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 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-5" />
|
||||||
</button>
|
</button>
|
||||||
</>
|
</form>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
'use client';
|
||||||
import { CustomerField } from '@/app/lib/definitions';
|
import { CustomerField } from '@/app/lib/definitions';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import {
|
import {
|
||||||
@@ -7,10 +8,15 @@ import {
|
|||||||
UserCircleIcon,
|
UserCircleIcon,
|
||||||
} from '@heroicons/react/24/outline';
|
} from '@heroicons/react/24/outline';
|
||||||
import { Button } from '@/app/ui/button';
|
import { Button } from '@/app/ui/button';
|
||||||
|
import {createInvoice} from "@/app/lib/actions";
|
||||||
|
import { useFormState } from 'react-dom';
|
||||||
|
|
||||||
export default function Form({ customers }: { customers: CustomerField[] }) {
|
export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||||
|
const initialState = { message: null, errors: {} };
|
||||||
|
const [state, dispatch] = useFormState(createInvoice, initialState);
|
||||||
|
console.log(state);
|
||||||
return (
|
return (
|
||||||
<form>
|
<form action={dispatch} aria-describedby="form-error">
|
||||||
<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">
|
||||||
@@ -19,21 +25,31 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
|||||||
</label>
|
</label>
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<select
|
<select
|
||||||
id="customer"
|
id="customer"
|
||||||
name="customerId"
|
name="customerId"
|
||||||
className="peer block w-full cursor-pointer rounded-md border border-gray-200 py-2 pl-10 text-sm outline-2 placeholder:text-gray-500"
|
className="peer block w-full cursor-pointer 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
|
||||||
</option>
|
</option>
|
||||||
{customers.map((customer) => (
|
{customers.map((customer) => (
|
||||||
<option key={customer.id} value={customer.id}>
|
<option key={customer.id} value={customer.id}>
|
||||||
{customer.name}
|
{customer.name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</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 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>
|
</div>
|
||||||
|
|
||||||
@@ -54,6 +70,13 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
|||||||
/>
|
/>
|
||||||
<CurrencyDollarIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
|
<CurrencyDollarIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
|
||||||
</div>
|
</div>
|
||||||
|
<div id="amount-error" aria-live="polite" aria-atomic="true">
|
||||||
|
{state.errors?.amount && state.errors.amount.map(s => (
|
||||||
|
<p className="mt-2 text-sm text-red-500" key={s}>{s}</p>
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -66,42 +89,54 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
|||||||
<div className="flex gap-4">
|
<div className="flex gap-4">
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
<input
|
<input
|
||||||
id="pending"
|
id="pending"
|
||||||
name="status"
|
name="status"
|
||||||
type="radio"
|
type="radio"
|
||||||
value="pending"
|
value="pending"
|
||||||
className="h-4 w-4 cursor-pointer border-gray-300 bg-gray-100 text-gray-600 focus:ring-2"
|
className="h-4 w-4 cursor-pointer border-gray-300 bg-gray-100 text-gray-600 focus:ring-2"
|
||||||
|
aria-describedby="status-error"
|
||||||
/>
|
/>
|
||||||
<label
|
<label
|
||||||
htmlFor="pending"
|
htmlFor="pending"
|
||||||
className="ml-2 flex cursor-pointer items-center gap-1.5 rounded-full bg-gray-100 px-3 py-1.5 text-xs font-medium text-gray-600"
|
className="ml-2 flex cursor-pointer items-center gap-1.5 rounded-full bg-gray-100 px-3 py-1.5 text-xs font-medium text-gray-600"
|
||||||
>
|
>
|
||||||
Pending <ClockIcon className="h-4 w-4" />
|
Pending <ClockIcon className="h-4 w-4"/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
<input
|
<input
|
||||||
id="paid"
|
id="paid"
|
||||||
name="status"
|
name="status"
|
||||||
type="radio"
|
type="radio"
|
||||||
value="paid"
|
value="paid"
|
||||||
className="h-4 w-4 cursor-pointer border-gray-300 bg-gray-100 text-gray-600 focus:ring-2"
|
className="h-4 w-4 cursor-pointer border-gray-300 bg-gray-100 text-gray-600 focus:ring-2"
|
||||||
/>
|
/>
|
||||||
<label
|
<label
|
||||||
htmlFor="paid"
|
htmlFor="paid"
|
||||||
className="ml-2 flex cursor-pointer items-center gap-1.5 rounded-full bg-green-500 px-3 py-1.5 text-xs font-medium text-white"
|
className="ml-2 flex cursor-pointer items-center gap-1.5 rounded-full bg-green-500 px-3 py-1.5 text-xs font-medium text-white"
|
||||||
>
|
>
|
||||||
Paid <CheckIcon className="h-4 w-4" />
|
Paid <CheckIcon className="h-4 w-4"/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="status-error" aria-live="polite" aria-atomic="true">
|
||||||
|
{state.errors?.status && state.errors?.status.map(e => (
|
||||||
|
<p key={e} className="mt-2 text-sm text-red-500">{e}</p>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="form-error">
|
||||||
|
{
|
||||||
|
state.message && (<p className="mt-2 text-sm text-red-500 font-bold">{state.message}</p>)
|
||||||
|
}
|
||||||
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-6 flex justify-end gap-4">
|
<div className="mt-6 flex justify-end gap-4">
|
||||||
<Link
|
<Link
|
||||||
href="/dashboard/invoices"
|
href="/dashboard/invoices"
|
||||||
className="flex h-10 items-center rounded-lg bg-gray-100 px-4 text-sm font-medium text-gray-600 transition-colors hover:bg-gray-200"
|
className="flex h-10 items-center rounded-lg bg-gray-100 px-4 text-sm font-medium text-gray-600 transition-colors hover:bg-gray-200"
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</Link>
|
</Link>
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
} from '@heroicons/react/24/outline';
|
} from '@heroicons/react/24/outline';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Button } from '@/app/ui/button';
|
import { Button } from '@/app/ui/button';
|
||||||
|
import { updateInvoice } from '@/app/lib/actions';
|
||||||
|
|
||||||
export default function EditInvoiceForm({
|
export default function EditInvoiceForm({
|
||||||
invoice,
|
invoice,
|
||||||
@@ -17,8 +18,10 @@ export default function EditInvoiceForm({
|
|||||||
invoice: InvoiceForm;
|
invoice: InvoiceForm;
|
||||||
customers: CustomerField[];
|
customers: CustomerField[];
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
|
const updateInvoiceWithId = updateInvoice.bind(null, invoice.id);
|
||||||
return (
|
return (
|
||||||
<form>
|
<form action={updateInvoiceWithId}>
|
||||||
<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">
|
||||||
|
|||||||
@@ -4,17 +4,27 @@ 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
|
// 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 */}
|
{/* 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 +57,7 @@ export default function Pagination({ totalPages }: { totalPages: number }) {
|
|||||||
href={createPageURL(currentPage + 1)}
|
href={createPageURL(currentPage + 1)}
|
||||||
isDisabled={currentPage >= totalPages}
|
isDisabled={currentPage >= totalPages}
|
||||||
/>
|
/>
|
||||||
</div> */}
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
'use client';
|
||||||
import { lusitana } from '@/app/ui/fonts';
|
import { lusitana } from '@/app/ui/fonts';
|
||||||
import {
|
import {
|
||||||
AtSymbolIcon,
|
AtSymbolIcon,
|
||||||
@@ -6,68 +7,84 @@ import {
|
|||||||
} from '@heroicons/react/24/outline';
|
} from '@heroicons/react/24/outline';
|
||||||
import { ArrowRightIcon } from '@heroicons/react/20/solid';
|
import { ArrowRightIcon } from '@heroicons/react/20/solid';
|
||||||
import { Button } from './button';
|
import { Button } from './button';
|
||||||
|
import { useFormState, useFormStatus } from 'react-dom';
|
||||||
|
import { authenticate } from '@/app/lib/actions';
|
||||||
|
|
||||||
export default function LoginForm() {
|
export default function LoginForm() {
|
||||||
|
const [errorMessage, dispatch] = useFormState(authenticate, undefined);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form className="space-y-3">
|
<form action={dispatch} className="space-y-3">
|
||||||
<div className="flex-1 rounded-lg bg-gray-50 px-6 pb-4 pt-8">
|
<div className="flex-1 rounded-lg bg-gray-50 px-6 pb-4 pt-8">
|
||||||
<h1 className={`${lusitana.className} mb-3 text-2xl`}>
|
<h1 className={`${lusitana.className} mb-3 text-2xl`}>
|
||||||
Please log in to continue.
|
Please log in to continue.
|
||||||
</h1>
|
</h1>
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<div>
|
<div>
|
||||||
<label
|
<label
|
||||||
className="mb-3 mt-5 block text-xs font-medium text-gray-900"
|
className="mb-3 mt-5 block text-xs font-medium text-gray-900"
|
||||||
htmlFor="email"
|
htmlFor="email"
|
||||||
>
|
>
|
||||||
Email
|
Email
|
||||||
</label>
|
</label>
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<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"
|
||||||
id="email"
|
id="email"
|
||||||
type="email"
|
type="email"
|
||||||
name="email"
|
name="email"
|
||||||
placeholder="Enter your email address"
|
placeholder="Enter your email address"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<AtSymbolIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
|
<AtSymbolIcon
|
||||||
|
className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="mt-4">
|
||||||
|
<label
|
||||||
|
className="mb-3 mt-5 block text-xs font-medium text-gray-900"
|
||||||
|
htmlFor="password"
|
||||||
|
>
|
||||||
|
Password
|
||||||
|
</label>
|
||||||
|
<div className="relative">
|
||||||
|
<input
|
||||||
|
className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500"
|
||||||
|
id="password"
|
||||||
|
type="password"
|
||||||
|
name="password"
|
||||||
|
placeholder="Enter password"
|
||||||
|
required
|
||||||
|
minLength={6}
|
||||||
|
/>
|
||||||
|
<KeyIcon
|
||||||
|
className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900"/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-4">
|
<LoginButton/>
|
||||||
<label
|
<div
|
||||||
className="mb-3 mt-5 block text-xs font-medium text-gray-900"
|
className="flex h-8 items-end space-x-1"
|
||||||
htmlFor="password"
|
aria-live="polite"
|
||||||
>
|
aria-atomic="true"
|
||||||
Password
|
>
|
||||||
</label>
|
{errorMessage && (
|
||||||
<div className="relative">
|
<>
|
||||||
<input
|
<ExclamationCircleIcon className="h-5 w-5 text-red-500"/>
|
||||||
className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500"
|
<p className="text-sm text-red-500">{errorMessage}</p>
|
||||||
id="password"
|
</>
|
||||||
type="password"
|
)}
|
||||||
name="password"
|
|
||||||
placeholder="Enter password"
|
|
||||||
required
|
|
||||||
minLength={6}
|
|
||||||
/>
|
|
||||||
<KeyIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<LoginButton />
|
</form>
|
||||||
<div className="flex h-8 items-end space-x-1">
|
|
||||||
{/* Add form errors here */}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function LoginButton() {
|
function LoginButton() {
|
||||||
|
const { pending } = useFormStatus();
|
||||||
return (
|
return (
|
||||||
<Button className="mt-4 w-full">
|
<Button className="mt-4 w-full" aria-disabled={pending}>
|
||||||
Log in <ArrowRightIcon className="ml-auto h-5 w-5 text-gray-50" />
|
Log in <ArrowRightIcon className="ml-auto h-5 w-5 text-gray-50" />
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,25 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
|
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
|
||||||
|
import {usePathname, useRouter, useSearchParams} from "next/navigation";
|
||||||
|
import { useDebouncedCallback } from 'use-debounce';
|
||||||
|
|
||||||
export default function Search({ placeholder }: { placeholder: string }) {
|
export default function Search({ placeholder }: { placeholder: string }) {
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const pathname = usePathname();
|
||||||
|
const { replace } = useRouter();
|
||||||
|
|
||||||
|
const handleSearch = useDebouncedCallback((term) => {
|
||||||
|
console.log(`Searching... ${term}`);
|
||||||
|
const params = new URLSearchParams(searchParams);
|
||||||
|
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 +28,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>
|
||||||
|
|||||||
21
auth.config.ts
Normal file
21
auth.config.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import type { NextAuthConfig } from 'next-auth';
|
||||||
|
|
||||||
|
export const authConfig = {
|
||||||
|
pages: {
|
||||||
|
signIn: '/login',
|
||||||
|
},
|
||||||
|
callbacks: {
|
||||||
|
authorized({ auth, request: { nextUrl } }) {
|
||||||
|
const isLoggedIn = !!auth?.user;
|
||||||
|
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard');
|
||||||
|
if (isOnDashboard) {
|
||||||
|
if (isLoggedIn) return true;
|
||||||
|
return false; // Redirect unauthenticated users to login page
|
||||||
|
} else if (isLoggedIn) {
|
||||||
|
return Response.redirect(new URL('/dashboard', nextUrl));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
providers: [], // Add providers with an empty array for now
|
||||||
|
} satisfies NextAuthConfig;
|
||||||
40
auth.ts
Normal file
40
auth.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import NextAuth from 'next-auth';
|
||||||
|
import Credentials from 'next-auth/providers/credentials';
|
||||||
|
import { authConfig } from './auth.config';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { sql } from '@vercel/postgres';
|
||||||
|
import type { User } from '@/app/lib/definitions';
|
||||||
|
import bcrypt from 'bcrypt';
|
||||||
|
|
||||||
|
async function getUser(email: string): Promise<User | undefined> {
|
||||||
|
try {
|
||||||
|
const user = await sql<User>`SELECT * FROM users WHERE email=${email}`;
|
||||||
|
return user.rows[0];
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch user:', error);
|
||||||
|
throw new Error('Failed to fetch user.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const { auth, signIn, signOut } = NextAuth({
|
||||||
|
...authConfig,
|
||||||
|
providers: [
|
||||||
|
Credentials({
|
||||||
|
async authorize(credentials) {
|
||||||
|
const parsedCredentials = z
|
||||||
|
.object({ email: z.string().email(), password: z.string().min(6) })
|
||||||
|
.safeParse(credentials);
|
||||||
|
|
||||||
|
if (parsedCredentials.success) {
|
||||||
|
const { email, password } = parsedCredentials.data;
|
||||||
|
const user = await getUser(email);
|
||||||
|
if (!user) return null;
|
||||||
|
const passwordsMatch = await bcrypt.compare(password, user.password);
|
||||||
|
if (passwordsMatch) return user;
|
||||||
|
}
|
||||||
|
console.log('Invalid credentials');
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
9
middleware.ts
Normal file
9
middleware.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import NextAuth from 'next-auth';
|
||||||
|
import { authConfig } from './auth.config';
|
||||||
|
|
||||||
|
export default NextAuth(authConfig).auth;
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
|
||||||
|
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
|
||||||
|
};
|
||||||
109
package-lock.json
generated
109
package-lock.json
generated
@@ -13,11 +13,13 @@
|
|||||||
"bcrypt": "^5.1.1",
|
"bcrypt": "^5.1.1",
|
||||||
"clsx": "^2.0.0",
|
"clsx": "^2.0.0",
|
||||||
"next": "^14.0.2",
|
"next": "^14.0.2",
|
||||||
|
"next-auth": "^5.0.0-beta.4",
|
||||||
"postcss": "8.4.31",
|
"postcss": "8.4.31",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"react-dom": "18.2.0",
|
"react-dom": "18.2.0",
|
||||||
"tailwindcss": "3.3.3",
|
"tailwindcss": "3.3.3",
|
||||||
"typescript": "5.2.2",
|
"typescript": "5.2.2",
|
||||||
|
"use-debounce": "^10.0.0",
|
||||||
"zod": "^3.22.2"
|
"zod": "^3.22.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -69,6 +71,27 @@
|
|||||||
"node": ">=6.0.0"
|
"node": ">=6.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@auth/core": {
|
||||||
|
"version": "0.18.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/@auth/core/-/core-0.18.4.tgz",
|
||||||
|
"integrity": "sha512-GsNhsP1xE/3FoNS3dVkPjqRljLNJ4iyL2OLv3klQGNvw3bMpROFcK4lqhx7+pPHiamnVaYt2vg1xbB+lsNaevg==",
|
||||||
|
"dependencies": {
|
||||||
|
"@panva/hkdf": "^1.1.1",
|
||||||
|
"cookie": "0.6.0",
|
||||||
|
"jose": "^5.1.0",
|
||||||
|
"oauth4webapi": "^2.3.0",
|
||||||
|
"preact": "10.11.3",
|
||||||
|
"preact-render-to-string": "5.2.3"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"nodemailer": "^6.8.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"nodemailer": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@babel/code-frame": {
|
"node_modules/@babel/code-frame": {
|
||||||
"version": "7.22.13",
|
"version": "7.22.13",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.13.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.13.tgz",
|
||||||
@@ -1035,6 +1058,14 @@
|
|||||||
"node": ">= 8"
|
"node": ">= 8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@panva/hkdf": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@panva/hkdf/-/hkdf-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-dhPeilub1NuIG0X5Kvhh9lH4iW3ZsHlnzwgwbOlgwQ2wG1IqFzsgHqmKPk3WzsdWAeaxKJxgM0+W433RmN45GA==",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/panva"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@pkgr/utils": {
|
"node_modules/@pkgr/utils": {
|
||||||
"version": "2.4.2",
|
"version": "2.4.2",
|
||||||
"resolved": "https://registry.npmjs.org/@pkgr/utils/-/utils-2.4.2.tgz",
|
"resolved": "https://registry.npmjs.org/@pkgr/utils/-/utils-2.4.2.tgz",
|
||||||
@@ -2296,6 +2327,14 @@
|
|||||||
"integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==",
|
"integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/cookie": {
|
||||||
|
"version": "0.6.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz",
|
||||||
|
"integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cross-spawn": {
|
"node_modules/cross-spawn": {
|
||||||
"version": "7.0.3",
|
"version": "7.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
|
||||||
@@ -4655,6 +4694,14 @@
|
|||||||
"integrity": "sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA==",
|
"integrity": "sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/jose": {
|
||||||
|
"version": "5.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/jose/-/jose-5.2.0.tgz",
|
||||||
|
"integrity": "sha512-oW3PCnvyrcm1HMvGTzqjxxfnEs9EoFOFWi2HsEGhlFVOXxTE3K9GKWVMFoFw06yPUqwpvEWic1BmtUZBI/tIjw==",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/panva"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/js-tokens": {
|
"node_modules/js-tokens": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
|
||||||
@@ -5056,6 +5103,24 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/next-auth": {
|
||||||
|
"version": "5.0.0-beta.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/next-auth/-/next-auth-5.0.0-beta.4.tgz",
|
||||||
|
"integrity": "sha512-vgocjvwPA8gxd/zrIP/vr9lJ/HeNe+C56lPP1D3sdyenHt8KncQV6ro7q0xCsDp1fcOKx7WAWVZH5o8aMxDzgw==",
|
||||||
|
"dependencies": {
|
||||||
|
"@auth/core": "0.18.4"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"next": "^14",
|
||||||
|
"nodemailer": "^6.6.5",
|
||||||
|
"react": "^18.2.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"nodemailer": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/node-addon-api": {
|
"node_modules/node-addon-api": {
|
||||||
"version": "5.1.0",
|
"version": "5.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-5.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-5.1.0.tgz",
|
||||||
@@ -5184,6 +5249,14 @@
|
|||||||
"set-blocking": "^2.0.0"
|
"set-blocking": "^2.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/oauth4webapi": {
|
||||||
|
"version": "2.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/oauth4webapi/-/oauth4webapi-2.4.1.tgz",
|
||||||
|
"integrity": "sha512-qor45aeDGaGqDOizwut+Q/rZ+J6BIJvOp7U0LtHfbFhg3O7JV5lvQFDXPNqSmcUjuq/Zeq8CIII4RD0sWLOdSQ==",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/panva"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/object-assign": {
|
"node_modules/object-assign": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||||
@@ -5704,6 +5777,26 @@
|
|||||||
"node": ">=0.10.0"
|
"node": ">=0.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/preact": {
|
||||||
|
"version": "10.11.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/preact/-/preact-10.11.3.tgz",
|
||||||
|
"integrity": "sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg==",
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/preact"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/preact-render-to-string": {
|
||||||
|
"version": "5.2.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.3.tgz",
|
||||||
|
"integrity": "sha512-aPDxUn5o3GhWdtJtW0svRC2SS/l8D9MAgo2+AWml+BhDImb27ALf04Q2d+AHqUUOc6RdSXFIBVa2gxzgMKgtZA==",
|
||||||
|
"dependencies": {
|
||||||
|
"pretty-format": "^3.8.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"preact": ">=10"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/prelude-ls": {
|
"node_modules/prelude-ls": {
|
||||||
"version": "1.2.1",
|
"version": "1.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
|
||||||
@@ -5818,6 +5911,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/pretty-format": {
|
||||||
|
"version": "3.8.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-3.8.0.tgz",
|
||||||
|
"integrity": "sha512-WuxUnVtlWL1OfZFQFuqvnvs6MiAGk9UNsBostyBOB0Is9wb5uRESevA6rnl/rkksXaGX3GzZhPup5d6Vp1nFew=="
|
||||||
|
},
|
||||||
"node_modules/prop-types": {
|
"node_modules/prop-types": {
|
||||||
"version": "15.8.1",
|
"version": "15.8.1",
|
||||||
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
|
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
|
||||||
@@ -7100,6 +7198,17 @@
|
|||||||
"punycode": "^2.1.0"
|
"punycode": "^2.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/use-debounce": {
|
||||||
|
"version": "10.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/use-debounce/-/use-debounce-10.0.0.tgz",
|
||||||
|
"integrity": "sha512-XRjvlvCB46bah9IBXVnq/ACP2lxqXyZj0D9hj4K5OzNroMDpTEBg8Anuh1/UfRTRs7pLhQ+RiNxxwZu9+MVl1A==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 16.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">=16.8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/utf-8-validate": {
|
"node_modules/utf-8-validate": {
|
||||||
"version": "6.0.3",
|
"version": "6.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-6.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-6.0.3.tgz",
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
"prettier": "prettier --write --ignore-unknown .",
|
"prettier": "prettier --write --ignore-unknown .",
|
||||||
"prettier:check": "prettier --check --ignore-unknown .",
|
"prettier:check": "prettier --check --ignore-unknown .",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"seed": "node -r dotenv/config ./scripts/seed.js"
|
"seed": "node -r dotenv/config ./scripts/seed.js",
|
||||||
|
"lint": "next lint"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@heroicons/react": "^2.0.18",
|
"@heroicons/react": "^2.0.18",
|
||||||
@@ -17,11 +18,13 @@
|
|||||||
"bcrypt": "^5.1.1",
|
"bcrypt": "^5.1.1",
|
||||||
"clsx": "^2.0.0",
|
"clsx": "^2.0.0",
|
||||||
"next": "^14.0.2",
|
"next": "^14.0.2",
|
||||||
|
"next-auth": "^5.0.0-beta.4",
|
||||||
"postcss": "8.4.31",
|
"postcss": "8.4.31",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"react-dom": "18.2.0",
|
"react-dom": "18.2.0",
|
||||||
"tailwindcss": "3.3.3",
|
"tailwindcss": "3.3.3",
|
||||||
"typescript": "5.2.2",
|
"typescript": "5.2.2",
|
||||||
|
"use-debounce": "^10.0.0",
|
||||||
"zod": "^3.22.2"
|
"zod": "^3.22.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user