|
| 1 | +import { getListReviews } from '@/lib/api/review' |
| 2 | +import { Verify } from '@/lib/firebase/firebase' |
| 3 | +import { Calendar, Star } from 'lucide-react' |
| 4 | +import { Card, CardContent } from '@/components/ui/card' |
| 5 | +import { Avatar, AvatarFallback } from '@/components/ui/avatar' |
| 6 | +import { formatDate } from '@/lib/utils' |
| 7 | +import { DateTime } from '@/components/common/DateTime' |
| 8 | +import { |
| 9 | + Pagination, |
| 10 | + PaginationContent, |
| 11 | + PaginationItem, |
| 12 | + PaginationNext, |
| 13 | + PaginationPrevious, |
| 14 | +} from '@/components/ui/pagination' |
| 15 | +import { Route } from 'next' |
| 16 | +import { cookies } from 'next/headers' |
| 17 | +import Image from 'next/image' |
| 18 | +import Link from 'next/link' |
| 19 | +import clsx from 'clsx' |
| 20 | + |
| 21 | +export default async function ReviewsPage({ |
| 22 | + searchParams, |
| 23 | +}: { |
| 24 | + searchParams: Promise<{ |
| 25 | + skip?: number |
| 26 | + limit?: number |
| 27 | + rating?: number |
| 28 | + comment?: string |
| 29 | + }> |
| 30 | +}) { |
| 31 | + const { rating, comment, ...sp } = await searchParams |
| 32 | + const skip = Number(sp?.skip ?? 0) |
| 33 | + const limit = Number(sp?.limit ?? 20) |
| 34 | + |
| 35 | + const headers = await Verify({ |
| 36 | + from: `/admin/reviews`, |
| 37 | + }) |
| 38 | + |
| 39 | + const cookieStore = await cookies() |
| 40 | + const sessionName = process.env.LIBRARY_COOKIE_NAME as string |
| 41 | + const activeLibraryID = cookieStore.get(sessionName)?.value |
| 42 | + |
| 43 | + const res = await getListReviews( |
| 44 | + { |
| 45 | + library_id: activeLibraryID, |
| 46 | + skip, |
| 47 | + rating, |
| 48 | + comment, |
| 49 | + limit, |
| 50 | + }, |
| 51 | + { headers } |
| 52 | + ) |
| 53 | + |
| 54 | + if ('error' in res) { |
| 55 | + return <div>{JSON.stringify(res.message)}</div> |
| 56 | + } |
| 57 | + |
| 58 | + const prevSkip = skip - limit > 0 ? skip - limit : 0 |
| 59 | + |
| 60 | + const nextURL = `/admin/reviews?skip=${skip + limit}&limit=${limit}` as const |
| 61 | + const prevURL = `/admin/reviews?skip=${prevSkip}&limit=${limit}` as const |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="space-y-4"> |
| 65 | + {res.data.map((review) => ( |
| 66 | + <Card key={review.id} className="hover:shadow-md transition-shadow"> |
| 67 | + <CardContent> |
| 68 | + <Link href={`/admin/books/${review.book.id}`} className="shrink-0"> |
| 69 | + <div className="flex gap-4"> |
| 70 | + {/* 3D Book Effect */} |
| 71 | + <div className="shrink-0"> |
| 72 | + <div className="flex"> |
| 73 | + <div className="bg-accent transform-[perspective(400px)_rotateY(314deg)] -mr-1 w-4"> |
| 74 | + <span className="inline-block text-nowrap text-[0.5rem] font-bold text-accent-foreground/50 transform-[rotate(90deg)_translateY(-16px)] origin-top-left"></span> |
| 75 | + </div> |
| 76 | + <Image |
| 77 | + src={review.book.cover ?? '/book-placeholder.svg'} |
| 78 | + alt={review.book.title + "'s cover"} |
| 79 | + width={96} |
| 80 | + height={144} |
| 81 | + className={clsx( |
| 82 | + 'shadow-xl rounded-r-md md:w-24 md:h-36 object-cover', |
| 83 | + 'transform-[perspective(800px)_rotateY(14deg)]' |
| 84 | + )} |
| 85 | + priority |
| 86 | + /> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + <div className="space-y-4"> |
| 90 | + <div className="flex flex-col md:flex-row md:justify-between"> |
| 91 | + <div> |
| 92 | + <h4 className="font-medium">{review.book.title}</h4> |
| 93 | + <div>{review.book.code}</div> |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className="flex items-center gap-1"> |
| 97 | + {[1, 2, 3, 4, 5].map((star) => ( |
| 98 | + <Star |
| 99 | + key={star} |
| 100 | + className={`h-5 w-5 ${ |
| 101 | + star <= review.rating |
| 102 | + ? 'fill-(--color-vibrant,var(--color-yellow-400)) text-(--color-vibrant,var(--color-yellow-400))' |
| 103 | + : 'text-gray-300' |
| 104 | + }`} |
| 105 | + /> |
| 106 | + ))} |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + |
| 110 | + {/* Review text */} |
| 111 | + <div> |
| 112 | + <p className="text-sm leading-relaxed mb-4 text-foreground"> |
| 113 | + {review.comment} |
| 114 | + </p> |
| 115 | + |
| 116 | + <div className="flex items-center gap-2"> |
| 117 | + <Avatar className="h-8 w-8"> |
| 118 | + <AvatarFallback className="text-xs bg-primary/10"> |
| 119 | + {review.user.name.slice(0, 2)} |
| 120 | + </AvatarFallback> |
| 121 | + </Avatar> |
| 122 | + <div>{review.user.name}</div> |
| 123 | + <div className="ml-auto flex gap-1"> |
| 124 | + <Calendar className="h-3 w-3" /> |
| 125 | + <DateTime |
| 126 | + dateTime={review.created_at} |
| 127 | + className="text-xs text-muted-foreground" |
| 128 | + > |
| 129 | + {formatDate(review.created_at)} |
| 130 | + </DateTime> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + </Link> |
| 137 | + </CardContent> |
| 138 | + </Card> |
| 139 | + ))} |
| 140 | + |
| 141 | + <Pagination> |
| 142 | + <PaginationContent> |
| 143 | + {res.meta.skip > 0 && ( |
| 144 | + <PaginationItem> |
| 145 | + <PaginationPrevious href={prevURL as Route} /> |
| 146 | + </PaginationItem> |
| 147 | + )} |
| 148 | + {res.meta.limit <= res.data.length && ( |
| 149 | + <PaginationItem> |
| 150 | + <PaginationNext href={nextURL as Route} /> |
| 151 | + </PaginationItem> |
| 152 | + )} |
| 153 | + </PaginationContent> |
| 154 | + </Pagination> |
| 155 | + </div> |
| 156 | + ) |
| 157 | +} |
0 commit comments