diff --git a/apps/web/app/(app)/recipes/[id]/page.tsx b/apps/web/app/(app)/recipes/[id]/page.tsx index 5c53a72..3f0a4e7 100644 --- a/apps/web/app/(app)/recipes/[id]/page.tsx +++ b/apps/web/app/(app)/recipes/[id]/page.tsx @@ -55,7 +55,7 @@ export default async function RecipePage({ params }: Params) { const VISIBILITY_LABEL = m.recipe.visibility; const DIETARY_LABELS = m.recipe.dietary; - const [recipe, ratingData, favoriteData] = await Promise.all([ + const [recipe, ratingData, favoriteData, myRating] = await Promise.all([ db.query.recipes.findFirst({ where: and( eq(recipes.id, id), @@ -70,6 +70,7 @@ export default async function RecipePage({ params }: Params) { }), db.select({ avgScore: avg(ratings.score), total: count() }).from(ratings).where(eq(ratings.recipeId, id)), db.query.favorites.findFirst({ where: and(eq(favorites.userId, session.user.id), eq(favorites.recipeId, id)) }), + db.query.ratings.findFirst({ where: and(eq(ratings.recipeId, id), eq(ratings.userId, session.user.id)) }), ]); if (!recipe) notFound(); @@ -79,6 +80,7 @@ export default async function RecipePage({ params }: Params) { const avgScore = ratingData[0]?.avgScore ? parseFloat(ratingData[0].avgScore) : null; const ratingCount = ratingData[0]?.total ?? 0; const isFavorited = !!favoriteData; + const myScore = myRating?.score ?? 0; const cover = recipe.photos.find((p) => p.isCover) ?? recipe.photos[0]; const totalMins = (recipe.prepMins ?? 0) + (recipe.cookMins ?? 0); @@ -384,7 +386,7 @@ export default async function RecipePage({ params }: Params) { <>
- +
diff --git a/apps/web/components/social/comment-reactions.tsx b/apps/web/components/social/comment-reactions.tsx index 7949929..79fe94f 100644 --- a/apps/web/components/social/comment-reactions.tsx +++ b/apps/web/components/social/comment-reactions.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import { toast } from "sonner"; const REACTIONS: Record = { @@ -24,6 +24,20 @@ export function CommentReactions({ recipeId, commentId, initialCounts = {}, init const [userReactions, setUserReactions] = useState(initialUserReactions); const [pending, setPending] = useState(null); + useEffect(() => { + let cancelled = false; + fetch(`/api/v1/recipes/${recipeId}/comments/${commentId}/reactions`) + .then((res) => (res.ok ? res.json() : null)) + .then((data: { counts: Record; userReactions: string[] } | null) => { + if (!data || cancelled) return; + setCounts(data.counts); + setUserReactions(data.userReactions); + }) + .catch(() => {}); + return () => { cancelled = true; }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [recipeId, commentId]); + async function toggle(type: string) { if (pending) return; setPending(type);