fix: rating not shown after refresh, comment reactions not shown until clicked
- The interactive RatingStars widget was always initialized with
initialScore={0}, ignoring the current user's existing rating.
Fetch it via a third parallel query and pass it through — the
rating was persisted correctly all along, just never displayed
back.
- CommentReactions never fetched its counts on mount, relying solely
on initialCounts/initialUserReactions props that comments-section
always passed as empty — so every comment showed zero reactions
until you clicked one yourself (which returns fresh counts from the
POST response). Fetch on mount via the existing GET endpoint.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
const REACTIONS: Record<string, string> = {
|
||||
@@ -24,6 +24,20 @@ export function CommentReactions({ recipeId, commentId, initialCounts = {}, init
|
||||
const [userReactions, setUserReactions] = useState<string[]>(initialUserReactions);
|
||||
const [pending, setPending] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
fetch(`/api/v1/recipes/${recipeId}/comments/${commentId}/reactions`)
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.then((data: { counts: Record<string, number>; 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);
|
||||
|
||||
Reference in New Issue
Block a user