From 3111af51fc2d6beb4fc553902a4eb96697a5bfe5 Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Thu, 6 Feb 2025 23:36:12 +1100 Subject: [PATCH] add search for contact book (#100) --- .../contacts/contact-books-list.tsx | 18 ++++++++++++- apps/web/src/server/api/routers/contacts.ts | 27 +++++++++++-------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/apps/web/src/app/(dashboard)/contacts/contact-books-list.tsx b/apps/web/src/app/(dashboard)/contacts/contact-books-list.tsx index f8c21f1..1315d0c 100644 --- a/apps/web/src/app/(dashboard)/contacts/contact-books-list.tsx +++ b/apps/web/src/app/(dashboard)/contacts/contact-books-list.tsx @@ -7,14 +7,30 @@ import Link from "next/link"; import EditContactBook from "./edit-contact-book"; import { useRouter } from "next/navigation"; import { motion } from "framer-motion"; +import { useUrlState } from "~/hooks/useUrlState"; +import { Input } from "@unsend/ui/src/input"; +import { useDebouncedCallback } from "use-debounce"; export default function ContactBooksList() { - const contactBooksQuery = api.contacts.getContactBooks.useQuery(); + const [search, setSearch] = useUrlState("search"); + const contactBooksQuery = api.contacts.getContactBooks.useQuery({ + search: search ?? undefined, + }); const router = useRouter(); + const debouncedSearch = useDebouncedCallback((value: string) => { + setSearch(value); + }, 1000); + return (
+ debouncedSearch(e.target.value)} + />
{contactBooksQuery.data?.map((contactBook) => ( { - return db.contactBook.findMany({ - where: { - teamId: team.id, - }, - include: { - _count: { - select: { contacts: true }, + getContactBooks: teamProcedure + .input(z.object({ search: z.string().optional() })) + .query(async ({ ctx: { db, team }, input }) => { + return db.contactBook.findMany({ + where: { + teamId: team.id, + ...(input.search + ? { name: { contains: input.search, mode: "insensitive" } } + : {}), }, - }, - }); - }), + include: { + _count: { + select: { contacts: true }, + }, + }, + }); + }), createContactBook: teamProcedure .input(