Chapter 11 - Implement invoice search
This commit is contained in:
@@ -1,3 +1,35 @@
|
||||
export default function InvoicesPage() {
|
||||
return <p>Invoices page</p>
|
||||
import Pagination from '@/app/ui/invoices/pagination';
|
||||
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';
|
||||
|
||||
export default async function Page({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams?: {
|
||||
query?: string;
|
||||
page?: string;
|
||||
};
|
||||
}) {
|
||||
const query = searchParams?.query || '';
|
||||
const currentPage = Number(searchParams?.page) || 1; 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>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,22 @@
|
||||
'use client';
|
||||
|
||||
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
|
||||
import {usePathname, useRouter, useSearchParams} from "next/navigation";
|
||||
|
||||
export default function Search({ placeholder }: { placeholder: string }) {
|
||||
const searchParams = useSearchParams();
|
||||
const pathname = usePathname();
|
||||
const { replace } = useRouter();
|
||||
function handleSearch(term: string) {
|
||||
const params = new URLSearchParams(searchParams);
|
||||
if (term) {
|
||||
params.set('query', term);
|
||||
} else {
|
||||
params.delete('query');
|
||||
}
|
||||
|
||||
replace(`${pathname}?${params.toString()}`);
|
||||
}
|
||||
return (
|
||||
<div className="relative flex flex-1 flex-shrink-0">
|
||||
<label htmlFor="search" className="sr-only">
|
||||
@@ -11,6 +25,10 @@ export default function Search({ placeholder }: { placeholder: string }) {
|
||||
<input
|
||||
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}
|
||||
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" />
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user