|
| 1 | +import { |
| 2 | + Breadcrumb, |
| 3 | + BreadcrumbItem, |
| 4 | + BreadcrumbLink, |
| 5 | + BreadcrumbList, |
| 6 | + BreadcrumbPage, |
| 7 | + BreadcrumbSeparator, |
| 8 | +} from '@/components/ui/breadcrumb' |
| 9 | +import { |
| 10 | + Pagination, |
| 11 | + PaginationContent, |
| 12 | + PaginationItem, |
| 13 | + PaginationNext, |
| 14 | + PaginationPrevious, |
| 15 | +} from '@/components/ui/pagination' |
| 16 | +import { getListBooks } from '@/lib/api/book' |
| 17 | +import Link from 'next/link' |
| 18 | +import type { Metadata } from 'next' |
| 19 | +import { SITE_NAME } from '@/lib/consts' |
| 20 | +import { Search } from 'lucide-react' |
| 21 | +import { DebouncedInput } from '@/components/common/DebouncedInput' |
| 22 | +import { Badge } from '@/components/ui/badge' |
| 23 | +import { ListBook } from '@/components/books/ListBook' |
| 24 | +import { Verify } from '@/lib/firebase/firebase' |
| 25 | +import { getListWatchlist } from '@/lib/api/watchlist' |
| 26 | + |
| 27 | +export const metadata: Metadata = { |
| 28 | + title: `Watchlist Books · ${SITE_NAME}`, |
| 29 | +} |
| 30 | + |
| 31 | +export default async function UserBooks({ |
| 32 | + searchParams, |
| 33 | +}: { |
| 34 | + searchParams: Promise<{ |
| 35 | + skip?: number |
| 36 | + limit?: number |
| 37 | + library_id?: string |
| 38 | + title?: string |
| 39 | + }> |
| 40 | +}) { |
| 41 | + const sp = await searchParams |
| 42 | + const skip = Number(sp?.skip ?? 0) |
| 43 | + const limit = Number(sp?.limit ?? 20) |
| 44 | + const library_id = sp?.library_id |
| 45 | + |
| 46 | + const query = { |
| 47 | + sort_by: 'created_at', |
| 48 | + sort_in: 'desc', |
| 49 | + limit: limit, |
| 50 | + skip: skip, |
| 51 | + title: sp?.title, |
| 52 | + include_stats: 'true', |
| 53 | + ...(library_id ? { library_id } : {}), |
| 54 | + } as const |
| 55 | + |
| 56 | + const headers = await Verify({ from: '/books' }) |
| 57 | + |
| 58 | + const res = await getListWatchlist(query, { headers }) |
| 59 | + |
| 60 | + if ('error' in res) { |
| 61 | + console.log(res) |
| 62 | + return <div>{JSON.stringify(res.message)}</div> |
| 63 | + } |
| 64 | + |
| 65 | + const prevSkip = skip - limit > 0 ? skip - limit : 0 |
| 66 | + |
| 67 | + const nextURL = `/books?skip=${skip + limit}&limit=${limit}` as const |
| 68 | + const prevURL = `/books?skip=${prevSkip}&limit=${limit}` as const |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="space-y-4"> |
| 72 | + <nav className="backdrop-blur-sm sticky top-0 z-10"> |
| 73 | + <h1 className="text-2xl font-semibold">Books</h1> |
| 74 | + <div className="flex justify-between items-center"> |
| 75 | + <Breadcrumb> |
| 76 | + <BreadcrumbList> |
| 77 | + <BreadcrumbItem> |
| 78 | + <BreadcrumbLink href="/">Home</BreadcrumbLink> |
| 79 | + </BreadcrumbItem> |
| 80 | + <BreadcrumbSeparator /> |
| 81 | + |
| 82 | + <BreadcrumbItem> |
| 83 | + <BreadcrumbPage> |
| 84 | + Books{' '} |
| 85 | + <Badge className="ml-4" variant="outline"> |
| 86 | + {res.meta.total} |
| 87 | + </Badge> |
| 88 | + </BreadcrumbPage> |
| 89 | + </BreadcrumbItem> |
| 90 | + </BreadcrumbList> |
| 91 | + </Breadcrumb> |
| 92 | + </div> |
| 93 | + </nav> |
| 94 | + |
| 95 | + <div className="relative flex-1"> |
| 96 | + <Search className="absolute left-3 top-3 size-4 text-muted-foreground" /> |
| 97 | + |
| 98 | + <DebouncedInput |
| 99 | + name="title" |
| 100 | + placeholder="Search by title" |
| 101 | + className="pl-8 max-w-md" |
| 102 | + /> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"> |
| 106 | + {res.data.map((book) => ( |
| 107 | + <Link key={book.id} href={`/books/${book.id}`} passHref> |
| 108 | + <ListBook book={book} /> |
| 109 | + </Link> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + <Pagination> |
| 113 | + <PaginationContent> |
| 114 | + {res.meta.skip > 0 && ( |
| 115 | + <PaginationItem> |
| 116 | + <PaginationPrevious href={prevURL} /> |
| 117 | + </PaginationItem> |
| 118 | + )} |
| 119 | + {res.meta.limit <= res.data.length && ( |
| 120 | + <PaginationItem> |
| 121 | + <PaginationNext href={nextURL} /> |
| 122 | + </PaginationItem> |
| 123 | + )} |
| 124 | + </PaginationContent> |
| 125 | + </Pagination> |
| 126 | + </div> |
| 127 | + ) |
| 128 | +} |
0 commit comments