Skip to content

Commit 6cb0035

Browse files
committed
feat: display rating in product description page
1 parent a7f5668 commit 6cb0035

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

public/unknown.webp

3.02 KB
Binary file not shown.

src/pages/products/[slug].tsx

+58
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function Example(
2727
if (!data) {
2828
return
2929
}
30+
console.log(data)
3031
function handleCartItemAdd() {
3132
if (!data) return
3233
addCartItem({
@@ -166,6 +167,63 @@ export default function Example(
166167
/>
167168
</div>
168169
</div>
170+
<article className="px-4 py-5">
171+
<h3 className="text-lg font-medium tracking-wider md:text-xl">
172+
Rating
173+
</h3>
174+
{data.review.length < 1 ? (
175+
<p className="my-4 font-medium tracking-wider">No review</p>
176+
) : null}
177+
{data.review.map((review, reviewIdx) => (
178+
<div
179+
key={review.id}
180+
className="flex space-x-4 text-sm text-gray-500"
181+
>
182+
<div className="flex-none py-5">
183+
<div className="relative h-10 w-10 rounded-full bg-gray-100">
184+
<Image
185+
fill
186+
src="/unknown.webp"
187+
alt="user profile picture"
188+
className="rounded-full"
189+
/>
190+
</div>
191+
</div>
192+
<div
193+
className={classNames(
194+
reviewIdx === 0
195+
? ''
196+
: 'border-t border-gray-300 dark:border-gray-700',
197+
'flex-1 py-5'
198+
)}
199+
>
200+
<h3 className="font-medium">{review.user.email}</h3>
201+
<p>
202+
<time dateTime={review.createdAt.toISOString()}>
203+
{review.createdAt.toISOString().substring(0, 10)}
204+
</time>
205+
</p>
206+
207+
<div className="mt-4 flex items-center">
208+
{[0, 1, 2, 3, 4].map((rating) => (
209+
<MdStar
210+
key={rating}
211+
className={classNames(
212+
review.rating > rating
213+
? 'text-yellow-500'
214+
: 'text-gray-300',
215+
'h-5 w-5 flex-shrink-0'
216+
)}
217+
aria-hidden="true"
218+
/>
219+
))}
220+
</div>
221+
<p className="sr-only">{review.rating} out of 5 stars</p>
222+
<p>{review.message}</p>
223+
</div>
224+
</div>
225+
))}
226+
</article>
169227
</div>
170228
</div>
171229
) : null}

src/server/router/product.ts

+36
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ const adminProduct = Prisma.validator<Prisma.ProductSelect>()({
2626
subSection: true,
2727
slug: true,
2828
})
29+
const returnProduct = Prisma.validator<Prisma.ProductSelect>()({
30+
brand: true,
31+
category: true,
32+
countInStock: true,
33+
description: true,
34+
id: true,
35+
images: true,
36+
metaDescription: true,
37+
newPrice: true,
38+
oldPrice: true,
39+
rating: true,
40+
section: true,
41+
slug: true,
42+
sold: true,
43+
subSection: true,
44+
title: true,
45+
})
2946

3047
export const productRouter = router({
3148
add: protectedSuperAdminProcedure
@@ -133,6 +150,25 @@ export const productRouter = router({
133150
.query(async ({ input, ctx }) => {
134151
const product = await ctx.prisma.product.findFirst({
135152
where: { slug: input.slug },
153+
select: {
154+
...returnProduct,
155+
review: {
156+
orderBy: {
157+
createdAt: 'desc',
158+
},
159+
select: {
160+
id: true,
161+
message: true,
162+
createdAt: true,
163+
rating: true,
164+
user: {
165+
select: {
166+
email: true,
167+
},
168+
},
169+
},
170+
},
171+
},
136172
})
137173
if (!product) {
138174
throw new TRPCError({

0 commit comments

Comments
 (0)