"use client"; import { useEffect, useId, useState } from "react"; import { api, errorText } from "@/lib/api"; import { HIGH_TAGS, LOW_TAGS, TAG_LABELS } from "@/lib/feedback"; import type { Rating } from "@/lib/types"; const MAX_COMMENT = 1000; /** * "Mennyire érezted magadra szabottnak?": the user rates what they got (an * analysis: 1–5 stars; an answer: thumbs). A click saves it at once; tags, a * comment and the permission to read the text can follow. Only the * operator sees it, nothing is public. */ export function RateContent({ analysisId, questionId, thumbs = false, }: { analysisId?: string; questionId?: string; /** Thumbs up/down instead of stars (answers to questions). */ thumbs?: boolean; }) { const id = useId(); const [rating, setRating] = useState(null); const [open, setOpen] = useState(false); const [tags, setTags] = useState([]); const [comment, setComment] = useState(""); const [mayRead, setMayRead] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [saved, setSaved] = useState(false); const target = analysisId ? { analysis_id: analysisId } : { question_id: questionId }; useEffect(() => { const query = analysisId ? `analysis_ids=${analysisId}` : `question_ids=${questionId}`; api<{ ratings: Rating[] }>(`/ratings?${query}`) .then((r) => { const mine = r.ratings[0]; if (!mine) return; setRating(mine); setTags(mine.tags); setComment(mine.comment ?? ""); setMayRead(mine.may_read); }) .catch(() => {}); }, [analysisId, questionId]); async function save(stars: number, details: boolean) { setBusy(true); setError(null); setSaved(false); try { const shown = stars >= 4 ? HIGH_TAGS : LOW_TAGS; const next = await api("/ratings", { method: "PUT", body: { ...target, stars, // Tags of the other side do not fit a changed rating. tags: tags.filter((t) => shown.includes(t)), comment: comment.trim() || null, may_read: mayRead, }, }); setRating(next); setTags(next.tags); if (details) { setSaved(true); setOpen(false); } else { setOpen(true); } } catch (e) { setError(errorText(e)); } finally { setBusy(false); } } const stars = rating?.stars ?? 0; const offered = stars >= 4 ? HIGH_TAGS : LOW_TAGS; const toggle = (tag: string) => setTags((all) => all.includes(tag) ? all.filter((t) => t !== tag) : [...all, tag], ); return (
{thumbs ? "Hasznos volt a válasz?" : "Mennyire érezted magadra szabottnak?"}
{thumbs ? [ { value: 5, label: "Igen", icon: "👍" }, { value: 1, label: "Nem", icon: "👎" }, ].map((b) => ( )) : [1, 2, 3, 4, 5].map((n) => ( ))}
{rating && !open && ( )}
{open && rating && (

Köszönjük! Ha szeretnéd, mondd el bővebben is (nem kötelező):

{offered.map((tag) => ( ))}