216 lines
6.6 KiB
TypeScript
216 lines
6.6 KiB
TypeScript
"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<Rating | null>(null);
|
|||
|
|
const [open, setOpen] = useState(false);
|
|||
|
|
const [tags, setTags] = useState<string[]>([]);
|
|||
|
|
const [comment, setComment] = useState("");
|
|||
|
|
const [mayRead, setMayRead] = useState(false);
|
|||
|
|
const [busy, setBusy] = useState(false);
|
|||
|
|
const [error, setError] = useState<string | null>(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<Rating>("/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 (
|
|||
|
|
<div className={thumbs ? "rate rate-thumbs" : "rate"}>
|
|||
|
|
<div className="rate-row">
|
|||
|
|
<span className="rate-question" id={`${id}-q`}>
|
|||
|
|
{thumbs
|
|||
|
|
? "Hasznos volt a válasz?"
|
|||
|
|
: "Mennyire érezted magadra szabottnak?"}
|
|||
|
|
</span>
|
|||
|
|
<div className="rate-buttons" role="group" aria-labelledby={`${id}-q`}>
|
|||
|
|
{thumbs
|
|||
|
|
? [
|
|||
|
|
{ value: 5, label: "Igen", icon: "👍" },
|
|||
|
|
{ value: 1, label: "Nem", icon: "👎" },
|
|||
|
|
].map((b) => (
|
|||
|
|
<button
|
|||
|
|
key={b.value}
|
|||
|
|
type="button"
|
|||
|
|
className={
|
|||
|
|
stars === b.value ? "rate-thumb is-on" : "rate-thumb"
|
|||
|
|
}
|
|||
|
|
aria-pressed={stars === b.value}
|
|||
|
|
aria-label={b.label}
|
|||
|
|
disabled={busy}
|
|||
|
|
onClick={() => save(b.value, false)}
|
|||
|
|
>
|
|||
|
|
<span aria-hidden="true">{b.icon}</span>
|
|||
|
|
</button>
|
|||
|
|
))
|
|||
|
|
: [1, 2, 3, 4, 5].map((n) => (
|
|||
|
|
<button
|
|||
|
|
key={n}
|
|||
|
|
type="button"
|
|||
|
|
className={n <= stars ? "rate-star is-on" : "rate-star"}
|
|||
|
|
aria-pressed={n === stars}
|
|||
|
|
aria-label={`${n} csillag`}
|
|||
|
|
disabled={busy}
|
|||
|
|
onClick={() => save(n, false)}
|
|||
|
|
>
|
|||
|
|
<span aria-hidden="true">★</span>
|
|||
|
|
</button>
|
|||
|
|
))}
|
|||
|
|
</div>
|
|||
|
|
{rating && !open && (
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="btn btn-ghost btn-sm"
|
|||
|
|
onClick={() => setOpen(true)}
|
|||
|
|
>
|
|||
|
|
{rating.comment || rating.tags.length
|
|||
|
|
? "Módosítom"
|
|||
|
|
: "Hozzáfűzök valamit"}
|
|||
|
|
</button>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{open && rating && (
|
|||
|
|
<div className="rate-details">
|
|||
|
|
<p className="hint">
|
|||
|
|
Köszönjük! Ha szeretnéd, mondd el bővebben is (nem kötelező):
|
|||
|
|
</p>
|
|||
|
|
<div className="rate-tags">
|
|||
|
|
{offered.map((tag) => (
|
|||
|
|
<button
|
|||
|
|
key={tag}
|
|||
|
|
type="button"
|
|||
|
|
className={tags.includes(tag) ? "rate-tag is-on" : "rate-tag"}
|
|||
|
|
aria-pressed={tags.includes(tag)}
|
|||
|
|
onClick={() => toggle(tag)}
|
|||
|
|
>
|
|||
|
|
{TAG_LABELS[tag]}
|
|||
|
|
</button>
|
|||
|
|
))}
|
|||
|
|
</div>
|
|||
|
|
<label htmlFor={`${id}-comment`} className="sr-only">
|
|||
|
|
Megjegyzés
|
|||
|
|
</label>
|
|||
|
|
<textarea
|
|||
|
|
id={`${id}-comment`}
|
|||
|
|
className="inp"
|
|||
|
|
rows={2}
|
|||
|
|
maxLength={MAX_COMMENT}
|
|||
|
|
value={comment}
|
|||
|
|
onChange={(e) => setComment(e.target.value)}
|
|||
|
|
placeholder="Mi tetszett, mi nem? Mi hiányzott?"
|
|||
|
|
/>
|
|||
|
|
<label className="check">
|
|||
|
|
<input
|
|||
|
|
type="checkbox"
|
|||
|
|
checked={mayRead}
|
|||
|
|
onChange={(e) => setMayRead(e.target.checked)}
|
|||
|
|
/>
|
|||
|
|
Megnézhetitek{" "}
|
|||
|
|
{thumbs ? "a kérdést és a választ" : "az elemzés szövegét"}, hogy
|
|||
|
|
javíthassatok rajta.
|
|||
|
|
</label>
|
|||
|
|
<div className="form-actions">
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="btn btn-primary btn-sm"
|
|||
|
|
disabled={busy}
|
|||
|
|
onClick={() => save(stars, true)}
|
|||
|
|
>
|
|||
|
|
{busy ? "Mentés…" : "Elküldöm"}
|
|||
|
|
</button>
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="btn btn-ghost btn-sm"
|
|||
|
|
onClick={() => setOpen(false)}
|
|||
|
|
>
|
|||
|
|
Most nem
|
|||
|
|
</button>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
<div aria-live="polite">
|
|||
|
|
{error && <p className="msg msg-error">{error}</p>}
|
|||
|
|
{saved && <p className="hint">Köszönjük a visszajelzést!</p>}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
);
|
|||
|
|
}
|