+ {shown === null ? 'Velg antall stjerner' : `${formatStars(shown)} av 5`}
+
diff --git a/frontend/src/features/wine/components/StudentReviews.module.css b/frontend/src/features/wine/components/StudentReviews.module.css
index 42a141f..760a067 100644
--- a/frontend/src/features/wine/components/StudentReviews.module.css
+++ b/frontend/src/features/wine/components/StudentReviews.module.css
@@ -48,3 +48,7 @@
.empty {
color: var(--color-text-muted);
}
+
+.subtitle {
+ font-size: var(--text-lg);
+}
diff --git a/frontend/src/features/wine/components/StudentReviews.tsx b/frontend/src/features/wine/components/StudentReviews.tsx
index 00363ad..f6a59b9 100644
--- a/frontend/src/features/wine/components/StudentReviews.tsx
+++ b/frontend/src/features/wine/components/StudentReviews.tsx
@@ -4,6 +4,7 @@ import { ErrorNotice } from '../../../components/ui/ErrorNotice';
import { Skeleton } from '../../../components/ui/Skeleton';
import { formatRatingSummary, formatRelativeTime } from '../../../lib/format';
import { useWineReviews, type ReviewDraft } from '../api/useWineReviews';
+import { RatingDistribution } from './RatingDistribution';
import { ReviewForm } from './ReviewForm';
import styles from './StudentReviews.module.css';
@@ -13,10 +14,20 @@ interface StudentReviewsProps {
now?: Date;
}
-/** The students' average, the user's own review and the newest reviews of one wine. */
+/** All ratings of one wine, the students' average, the user's own review and the newest reviews. */
export function StudentReviews({ wineId, now }: StudentReviewsProps) {
- const { students, reviews, myReview, loading, error, refetch, save, remove, saving } =
- useWineReviews(wineId);
+ const {
+ students,
+ reviews,
+ distribution,
+ myReview,
+ loading,
+ error,
+ refetch,
+ save,
+ remove,
+ saving,
+ } = useWineReviews(wineId);
const [message, setMessage] = useState('');
async function handleSave(draft: ReviewDraft) {
@@ -33,7 +44,7 @@ export function StudentReviews({ wineId, now }: StudentReviewsProps) {
return (
- Studentenes dommer
+ Vurderinger
{loading && }
@@ -41,6 +52,9 @@ export function StudentReviews({ wineId, now }: StudentReviewsProps) {
{!loading && !error && (
<>
+
+ Studentenes dommer
+
{formatRatingSummary(students ?? { average: null, count: 0 }, {
singular: 'dom',
diff --git a/frontend/src/lib/stars.ts b/frontend/src/lib/stars.ts
new file mode 100644
index 0000000..40da915
--- /dev/null
+++ b/frontend/src/lib/stars.ts
@@ -0,0 +1,32 @@
+// Helpers for ratings in half stars from 0.5 to 5.
+
+export type StarFill = 'full' | 'half' | 'empty';
+
+/** How much of star number `star` (1–5) is filled for a rating. */
+export function starFill(star: number, rating: number | null): StarFill {
+ if (rating === null || rating < star - 0.5) return 'empty';
+ return rating >= star ? 'full' : 'half';
+}
+
+/** «3,5» or «4»: one decimal only when needed. */
+export function formatStars(rating: number): string {
+ return new Intl.NumberFormat('nb-NO', { maximumFractionDigits: 1 }).format(rating);
+}
+
+export interface StarRow {
+ stars: number;
+ count: number;
+}
+
+/**
+ * Groups scores into five rows, one per whole star, highest first.
+ * A half star counts with the star above it: 3.5 counts as 4, and 0.5 as 1.
+ */
+export function groupByStars(distribution: { rating: number; count: number }[]): StarRow[] {
+ const rows = [5, 4, 3, 2, 1].map((stars) => ({ stars, count: 0 }));
+ for (const { rating, count } of distribution) {
+ const stars = Math.min(5, Math.max(1, Math.ceil(rating)));
+ rows[5 - stars].count += count;
+ }
+ return rows;
+}