|
| 1 | +import { |
| 2 | + Breadcrumb, |
| 3 | + BreadcrumbItem, |
| 4 | + BreadcrumbLink, |
| 5 | + BreadcrumbList, |
| 6 | + BreadcrumbPage, |
| 7 | + BreadcrumbSeparator, |
| 8 | +} from '@/components/ui/breadcrumb' |
| 9 | +import { Button } from '@/components/ui/button' |
| 10 | +import { |
| 11 | + Pagination, |
| 12 | + PaginationContent, |
| 13 | + PaginationItem, |
| 14 | + PaginationNext, |
| 15 | + PaginationPrevious, |
| 16 | +} from '@/components/ui/pagination' |
| 17 | +import { |
| 18 | + Table, |
| 19 | + TableBody, |
| 20 | + TableCell, |
| 21 | + TableHead, |
| 22 | + TableHeader, |
| 23 | + TableRow, |
| 24 | +} from '@/components/ui/table' |
| 25 | +import { getListUsers } from '@/lib/api/user' |
| 26 | +import Link from 'next/link' |
| 27 | + |
| 28 | +export default async function Users({ |
| 29 | + searchParams, |
| 30 | +}: { |
| 31 | + searchParams: Promise<{ |
| 32 | + skip?: number |
| 33 | + limit?: number |
| 34 | + }> |
| 35 | +}) { |
| 36 | + const sp = await searchParams |
| 37 | + const skip = Number(sp?.skip ?? 0) |
| 38 | + const limit = Number(sp?.limit ?? 20) |
| 39 | + const res = await getListUsers({ |
| 40 | + sort_by: 'created_at', |
| 41 | + sort_in: 'desc', |
| 42 | + limit: limit, |
| 43 | + skip: skip, |
| 44 | + }) |
| 45 | + |
| 46 | + if ('error' in res) { |
| 47 | + console.log(res) |
| 48 | + return <div>{JSON.stringify(res.message)}</div> |
| 49 | + } |
| 50 | + |
| 51 | + const prevSkip = skip - limit > 0 ? skip - limit : 0 |
| 52 | + |
| 53 | + const nextURL = `/users?skip=${skip + limit}&limit=${limit}` |
| 54 | + const prevURL = `/users?skip=${prevSkip}&limit=${limit}` |
| 55 | + |
| 56 | + return ( |
| 57 | + <div> |
| 58 | + <h1 className="text-2xl font-semibold">Users</h1> |
| 59 | + <div className="flex justify-between items-center"> |
| 60 | + <Breadcrumb> |
| 61 | + <BreadcrumbList> |
| 62 | + <BreadcrumbItem> |
| 63 | + <Link href="/" passHref legacyBehavior> |
| 64 | + <BreadcrumbLink>Home</BreadcrumbLink> |
| 65 | + </Link> |
| 66 | + </BreadcrumbItem> |
| 67 | + <BreadcrumbSeparator /> |
| 68 | + |
| 69 | + <BreadcrumbItem> |
| 70 | + <BreadcrumbPage>Users</BreadcrumbPage> |
| 71 | + </BreadcrumbItem> |
| 72 | + </BreadcrumbList> |
| 73 | + </Breadcrumb> |
| 74 | + <Button asChild> |
| 75 | + <Link href="/users/new">Add User</Link> |
| 76 | + </Button> |
| 77 | + </div> |
| 78 | + |
| 79 | + <Table> |
| 80 | + {/* <TableCaption>List of books available in the library.</TableCaption> */} |
| 81 | + <TableHeader> |
| 82 | + <TableRow> |
| 83 | + <TableHead>ID</TableHead> |
| 84 | + <TableHead>Name</TableHead> |
| 85 | + <TableHead>Email</TableHead> |
| 86 | + <TableHead>Created At</TableHead> |
| 87 | + </TableRow> |
| 88 | + </TableHeader> |
| 89 | + <TableBody> |
| 90 | + {res.data.map((u) => ( |
| 91 | + <TableRow key={u.id}> |
| 92 | + <TableCell>{u.id}</TableCell> |
| 93 | + <TableCell>{u.name}</TableCell> |
| 94 | + <TableCell>{u.email}</TableCell> |
| 95 | + <TableCell>{u.created_at}</TableCell> |
| 96 | + </TableRow> |
| 97 | + ))} |
| 98 | + </TableBody> |
| 99 | + </Table> |
| 100 | + |
| 101 | + <Pagination> |
| 102 | + <PaginationContent> |
| 103 | + <PaginationItem> |
| 104 | + <PaginationPrevious href={prevURL} /> |
| 105 | + </PaginationItem> |
| 106 | + <PaginationItem> |
| 107 | + <PaginationNext href={nextURL} /> |
| 108 | + </PaginationItem> |
| 109 | + </PaginationContent> |
| 110 | + </Pagination> |
| 111 | + </div> |
| 112 | + ) |
| 113 | +} |
0 commit comments