Compare commits
12 Commits
62b58918d0
...
3e4959dd9d
| Author | SHA1 | Date | |
|---|---|---|---|
| 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>
|
||||||
);
|
);
|
||||||
26
app/dashboard/invoices/[id]/edit/page.tsx
Normal file
26
app/dashboard/invoices/[id]/edit/page.tsx
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import Form from '@/app/ui/invoices/edit-form';
|
||||||
|
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
|
||||||
|
import {fetchCustomers, fetchInvoiceById} from '@/app/lib/data';
|
||||||
|
|
||||||
|
export default async function Page({ params }: { params: { id: string } }) {
|
||||||
|
const id = params.id;
|
||||||
|
const [invoice, customers] = await Promise.all([
|
||||||
|
fetchInvoiceById(id),
|
||||||
|
fetchCustomers(),
|
||||||
|
]);
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,3 +1,39 @@
|
|||||||
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";
|
||||||
|
|
||||||
|
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>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
63
app/lib/actions.ts
Normal file
63
app/lib/actions.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { sql } from '@vercel/postgres';
|
||||||
|
import { revalidatePath } from 'next/cache';
|
||||||
|
import {redirect} from "next/navigation";
|
||||||
|
|
||||||
|
const FormSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
customerId: z.string(),
|
||||||
|
amount: z.coerce.number(),
|
||||||
|
status: z.enum(['pending', 'paid']),
|
||||||
|
date: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const UpdateInvoice = FormSchema.omit({ id: true, date: true });
|
||||||
|
|
||||||
|
const CreateInvoice = FormSchema.omit({ id: true, date: true });
|
||||||
|
|
||||||
|
export async function createInvoice(formData: FormData) {
|
||||||
|
|
||||||
|
const { customerId, amount, status } = CreateInvoice.parse({
|
||||||
|
customerId: formData.get('customerId'),
|
||||||
|
amount: formData.get('amount'),
|
||||||
|
status: formData.get('status'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const amountInCents = amount * 100;
|
||||||
|
const date = new Date().toISOString().split('T')[0];
|
||||||
|
|
||||||
|
await sql`
|
||||||
|
INSERT INTO invoices (customer_id, amount, status, date)
|
||||||
|
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
|
||||||
|
`;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
await sql`
|
||||||
|
UPDATE invoices
|
||||||
|
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
|
||||||
|
WHERE id = ${id}
|
||||||
|
`;
|
||||||
|
|
||||||
|
revalidatePath('/dashboard/invoices');
|
||||||
|
redirect('/dashboard/invoices');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteInvoice(id: string) {
|
||||||
|
await sql`DELETE FROM invoices WHERE id = ${id}`;
|
||||||
|
revalidatePath('/dashboard/invoices');
|
||||||
|
}
|
||||||
@@ -27,8 +27,12 @@ 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
|
||||||
|
|||||||
@@ -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,11 +3,10 @@ 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`}>
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ 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";
|
||||||
|
|
||||||
export default function Form({ customers }: { customers: CustomerField[] }) {
|
export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||||
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">
|
||||||
|
|||||||
@@ -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,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>
|
||||||
|
|||||||
12
package-lock.json
generated
12
package-lock.json
generated
@@ -18,6 +18,7 @@
|
|||||||
"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": {
|
||||||
@@ -7100,6 +7101,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",
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
"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