From d12596f9b6a802db81daaa0e0b02937ec0534035 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Sun, 29 Oct 2023 23:54:52 -0700 Subject: [PATCH] Adding search on invoices with debounced searching. --- app/dashboard/invoices/page.tsx | 40 ++++++++++++++++++++++++++++++--- app/ui/search.tsx | 19 ++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 12 ++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/app/dashboard/invoices/page.tsx b/app/dashboard/invoices/page.tsx index e706d66..4797842 100644 --- a/app/dashboard/invoices/page.tsx +++ b/app/dashboard/invoices/page.tsx @@ -1,3 +1,37 @@ -export default function Page() { - return

Invoices Page

; -} \ No newline at end of file +import { Suspense } from "react"; +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"; + +export default async function Page({ + searchParams, +}: { + searchParams?: { + query?: string; + page?: string; + }; +}) { + const query = searchParams?.query || ""; + const currentPage = Number(searchParams?.page) || 1; + + return ( +
+
+

Invoices

+
+
+ + +
+ }> + + +
+ {/* */} +
+ + ); +} diff --git a/app/ui/search.tsx b/app/ui/search.tsx index e6e9391..bd6415b 100644 --- a/app/ui/search.tsx +++ b/app/ui/search.tsx @@ -1,8 +1,25 @@ 'use client'; +import { useSearchParams, usePathname, useRouter } from "next/navigation"; import { MagnifyingGlassIcon } from '@heroicons/react/24/outline'; +import { useDebouncedCallback } from "use-debounce"; export default function Search({ placeholder }: { placeholder: string }) { + const searchParams = useSearchParams(); + const pathname = usePathname(); + const { replace } = useRouter(); + + const handleSearch = useDebouncedCallback((term: string) => { + 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 (
diff --git a/package.json b/package.json index 56d898f..a3b6660 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "react-dom": "18.2.0", "tailwindcss": "3.3.3", "typescript": "5.2.2", + "use-debounce": "^9.0.4", "zod": "^3.22.2" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 59b6efe..dede481 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -44,6 +44,9 @@ dependencies: typescript: specifier: 5.2.2 version: 5.2.2 + use-debounce: + specifier: ^9.0.4 + version: 9.0.4(react@18.2.0) zod: specifier: ^3.22.2 version: 3.22.4 @@ -1338,6 +1341,15 @@ packages: picocolors: 1.0.0 dev: false + /use-debounce@9.0.4(react@18.2.0): + resolution: {integrity: sha512-6X8H/mikbrt0XE8e+JXRtZ8yYVvKkdYRfmIhWZYsP8rcNs9hk3APV8Ua2mFkKRLcJKVdnX2/Vwrmg2GWKUQEaQ==} + engines: {node: '>= 10.0.0'} + peerDependencies: + react: '>=16.8.0' + dependencies: + react: 18.2.0 + dev: false + /utf-8-validate@6.0.3: resolution: {integrity: sha512-uIuGf9TWQ/y+0Lp+KGZCMuJWc3N9BHA+l/UmHd/oUHwJJDeysyTRxNQVkbzsIWfGFbRe3OcgML/i0mvVRPOyDA==} engines: {node: '>=6.14.2'}