Spaces:
Running
Running
Download src/components/CourseReviews.tsx from ahmdelbaz28/AHMDRTAP: direct link, hf CLI and curl.
- Browser
- Download file 17.4 kB
-
https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/components/CourseReviews.tsx
- Command line
-
hf download hf://spaces/ahmdelbaz28/AHMDRTAP/src/components/CourseReviews.tsx
-
curl -L -o CourseReviews.tsx https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/components/CourseReviews.tsx
17.4 kB
| /** | |
| * COURSE REVIEWS — student ratings & comments section (CourseDetail). | |
| * | |
| * ✅ FEATURE GAP FIX (pre-launch audit): the backend shipped a complete | |
| * review system (review.listByCourse / myReview / create upsert / delete, | |
| * entitlement-gated) but NO page rendered it — reviewCount on course cards | |
| * could never grow and students had no social proof, the #1 conversion | |
| * lever on competitor platforms (Udemy, Coursera, Thinkific). | |
| * | |
| * Behaviour: | |
| * - Public list (paginated) of published reviews for the course. | |
| * - Authenticated users get a star-picker + comment form; submitting | |
| * creates OR updates their single review (server-side upsert). | |
| * - Unauthenticated users get a sign-in CTA with ?redirect= back here. | |
| * - Server enforces entitlement (premium: active enrollment) — the client | |
| * only surfaces a friendly message on FORBIDDEN. | |
| */ | |
| import { BadgeCheck, Lock, MessageSquareText, ShieldCheck, Star } from "lucide-react"; | |
| import { useState } from "react"; | |
| import { Link } from "react-router"; | |
| import { toast } from "sonner"; | |
| import { useAuth } from "@/hooks/useAuth"; | |
| import { useTranslation } from "@/hooks/useTranslation"; | |
| import { trpc } from "@/providers/trpc"; | |
| interface ReviewItem { | |
| id: number; | |
| userId: number; | |
| courseId: number; | |
| rating: number; | |
| comment: string | null; | |
| createdAt: string | Date; | |
| updatedAt: string | Date; | |
| userName: string | null; | |
| userUsername: string | null; | |
| /** Reviewer's profile photo (data URL from users.avatar) — null when unset. */ | |
| userAvatar: string | null; | |
| } | |
| interface MyReview { | |
| id: number; | |
| rating: number; | |
| comment: string | null; | |
| } | |
| const PAGE_SIZE = 5; | |
| function Stars({ value, size = "h-4 w-4" }: { readonly value: number; readonly size?: string }) { | |
| return ( | |
| <div className="flex items-center gap-0.5" role="img" aria-label={`${value}/5`}> | |
| {[1, 2, 3, 4, 5].map((s) => ( | |
| <Star | |
| key={s} | |
| aria-hidden="true" | |
| className={`${size} ${s <= value ? "fill-[#f59e0b] text-[#f59e0b]" : "text-[#475569]"}`} | |
| /> | |
| ))} | |
| </div> | |
| ); | |
| } | |
| /** Success toast for a review submit — bilingual, action-aware. */ | |
| function submitSuccessMessage(lang: "ar" | "en", updated: boolean): string { | |
| if (lang === "ar") return updated ? "تم تحديث تقييمك، شكراً لك!" : "تم إضافة تقييمك، شكراً لك!"; | |
| return updated ? "Your review was updated — thank you!" : "Your review was submitted — thank you!"; | |
| } | |
| /** "X.Y out of 5 (N reviews)" line under the stars, or the empty-state hint. */ | |
| function reviewCountLabel(lang: "ar" | "en", averageRating: number, total: number): string { | |
| if (total > 0) { | |
| const reviews = total === 1 ? "review" : "reviews"; | |
| return lang === "ar" | |
| ? `${averageRating.toFixed(1)} من 5 (${total} تقييم)` | |
| : `${averageRating.toFixed(1)} out of 5 (${total} ${reviews})`; | |
| } | |
| return lang === "ar" ? "لا تقييمات بعد" : "No reviews yet"; | |
| } | |
| /** Toast copy for a rejected review submit (S3776 helper — keeps the | |
| * component's onError branch out of its complexity budget). */ | |
| function reviewSubmitErrorToast(lang: "ar" | "en", err: unknown): void { | |
| const code = (err as { data?: { code?: string } } | undefined)?.data?.code; | |
| if (code === "FORBIDDEN") { | |
| toast.error(lang === "ar" ? "يجب التسجيل في الكورس أولاً قبل إضافة التقييم" : "Enroll in the course before leaving a review"); | |
| } else { | |
| toast.error(lang === "ar" ? "تعذر إرسال التقييم، حاول مرة أخرى" : "Could not submit your review, please try again"); | |
| } | |
| } | |
| /** Reviewer display name with the anonymous fallback (S3776 helper). */ | |
| function reviewerNameOrUnknown(r: ReviewItem): string { | |
| return r.userName || r.userUsername || "?"; | |
| } | |
| /** Reviewer display name — falls back to a generic "Student" label (S3776). */ | |
| function reviewerDisplayName(r: ReviewItem, lang: "ar" | "en"): string { | |
| return r.userName || r.userUsername || (lang === "ar" ? "طالب" : "Student"); | |
| } | |
| /** Write-review button label — "edit" variant once the user has a review. */ | |
| function writeReviewLabel(lang: "ar" | "en", editing: boolean): string { | |
| if (lang === "ar") return editing ? "تعديل تقييمك" : "اكتب تقييماً"; | |
| return editing ? "Edit your review" : "Write a review"; | |
| } | |
| /** Submit button label: pending state wins, then edit-vs-create wording. */ | |
| function submitButtonLabel(lang: "ar" | "en", pending: boolean, editing: boolean): string { | |
| if (pending) return lang === "ar" ? "جارٍ الإرسال…" : "Submitting…"; | |
| if (lang === "ar") return editing ? "حفظ التعديل" : "إرسال التقييم"; | |
| return editing ? "Save changes" : "Submit review"; | |
| } | |
| /** S3776 helper: the reviews header — title, aggregate rating, verify link. */ | |
| function ReviewsHeader({ lang, averageRating, total }: { readonly lang: "ar" | "en"; readonly averageRating: number; readonly total: number }) { | |
| return ( | |
| <div> | |
| <h2 className="flex items-center gap-2 text-lg font-semibold text-[#f0f4f8]"> | |
| <MessageSquareText className="h-5 w-5 text-[#06b6d4]" /> | |
| {lang === "ar" ? "تقييمات الطلاب" : "Student Reviews"} | |
| </h2> | |
| <div className="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1"> | |
| <Stars value={Math.round(averageRating)} /> | |
| <span className="text-sm text-[#94a3b8]"> | |
| {reviewCountLabel(lang, averageRating, total)} | |
| </span> | |
| {/* Brand Kit §7 — every proof is clickable: verify any student's | |
| certificate directly from the reviews section. */} | |
| <Link | |
| to="/verify" | |
| className="flex items-center gap-1 text-xs font-medium text-[#10b981] transition-colors hover:text-[#34d399]" | |
| > | |
| <ShieldCheck className="h-3.5 w-3.5" /> | |
| {lang === "ar" ? "تحقق من شهادة" : "Verify a certificate"} | |
| </Link> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| export default function CourseReviews({ courseId, averageRating }: { readonly courseId: number; readonly averageRating: number }) { | |
| const { lang } = useTranslation(); | |
| const { isAuthenticated } = useAuth(); | |
| const [listPage, setListPage] = useState(1); | |
| const [hoverRating, setHoverRating] = useState(0); | |
| const [draftRating, setDraftRating] = useState(0); | |
| const [draftComment, setDraftComment] = useState(""); | |
| const [showForm, setShowForm] = useState(false); | |
| // Types flow from the typed AppRouter — no explicit generics needed. | |
| const listQuery = trpc.review.listByCourse.useQuery({ courseId, page: listPage, limit: PAGE_SIZE }); | |
| const myReviewQuery = trpc.review.myReview.useQuery({ courseId }, { enabled: isAuthenticated }); | |
| const utils = trpc.useUtils(); | |
| const submitMutation = trpc.review.create.useMutation({ | |
| onSuccess: (result) => { | |
| toast.success(submitSuccessMessage(lang, result.action === "updated")); | |
| utils.review.listByCourse.invalidate({ courseId }).catch(() => {}); | |
| utils.review.myReview.invalidate({ courseId }).catch(() => {}); | |
| setShowForm(false); | |
| }, | |
| onError: (err) => reviewSubmitErrorToast(lang, err), | |
| }); | |
| // Prefill the form from the user's existing review. | |
| const myReview: MyReview | null = myReviewQuery.data | |
| ? { id: myReviewQuery.data.id, rating: myReviewQuery.data.rating, comment: myReviewQuery.data.comment } | |
| : null; | |
| const openForm = () => { | |
| setDraftRating(myReviewQuery.data?.rating ?? 0); | |
| setDraftComment(myReviewQuery.data?.comment ?? ""); | |
| setShowForm(true); | |
| }; | |
| const items = (listQuery.data?.items ?? []) as ReviewItem[]; | |
| const total = listQuery.data?.total ?? 0; | |
| const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE)); | |
| return ( | |
| <section aria-label={lang === "ar" ? "تقييمات الطلاب" : "Student reviews"} className="mx-auto mt-10 max-w-7xl px-4 lg:px-6"> | |
| <div className="rounded-xl border border-[#1f2d44] bg-[rgba(15,23,42,0.55)] p-6"> | |
| {/* ── Header ── */} | |
| <ReviewsHeader lang={lang} averageRating={averageRating} total={total} /> | |
| {isAuthenticated && !showForm && ( | |
| <button | |
| onClick={openForm} | |
| data-testid="write-review-btn" | |
| className="rounded-lg bg-[#06b6d4] px-4 py-2 text-sm font-semibold text-[#0a0e17] transition-colors hover:bg-[#0891b2]" | |
| > | |
| {writeReviewLabel(lang, myReview !== null)} | |
| </button> | |
| )} | |
| {!isAuthenticated && ( | |
| <Link | |
| to={`/login?redirect=/courses/${courseId}`} | |
| className="rounded-lg border border-[#1f2d44] px-4 py-2 text-sm text-[#94a3b8] transition-colors hover:border-[#06b6d4] hover:text-[#06b6d4]" | |
| > | |
| {lang === "ar" ? "سجّل الدخول لتقييم الكورس" : "Sign in to review"} | |
| </Link> | |
| )} | |
| {/* ── Write / edit form ── */} | |
| {showForm && ( | |
| <form | |
| onSubmit={(e) => { | |
| e.preventDefault(); | |
| if (draftRating < 1) return; | |
| submitMutation.mutate({ courseId, rating: draftRating, comment: draftComment.trim() || undefined }); | |
| }} | |
| className="mt-5 rounded-lg border border-[#1f2d44] bg-[rgba(10,14,23,0.6)] p-4" | |
| > | |
| <span className="text-sm font-medium text-[#f0f4f8]"> | |
| {lang === "ar" ? "تقييمك" : "Your rating"} | |
| </span> | |
| <div className="mt-2 flex items-center gap-1" role="radiogroup" aria-label={lang === "ar" ? "اختر عدد النجوم" : "Choose a star rating"}> | |
| {[1, 2, 3, 4, 5].map((s) => ( | |
| <label | |
| key={s} | |
| onMouseEnter={() => setHoverRating(s)} | |
| onMouseLeave={() => setHoverRating(0)} | |
| className="cursor-pointer rounded p-0.5 focus-within:outline-none focus-within:ring-2 focus-within:ring-[#06b6d4]" | |
| > | |
| <input | |
| type="radio" | |
| name="course-review-rating" | |
| value={s} | |
| checked={draftRating === s} | |
| onChange={() => setDraftRating(s)} | |
| aria-label={`${s} ${lang === "ar" ? "نجوم" : "stars"}`} | |
| className="sr-only" | |
| /> | |
| <Star | |
| aria-hidden="true" | |
| className={`h-6 w-6 transition-colors ${ | |
| s <= (hoverRating || draftRating) | |
| ? "fill-[#f59e0b] text-[#f59e0b]" | |
| : "text-[#475569] hover:text-[#f59e0b]/60" | |
| }`} | |
| /> | |
| </label> | |
| ))} | |
| </div> | |
| <textarea | |
| value={draftComment} | |
| onChange={(e) => setDraftComment(e.target.value)} | |
| maxLength={2000} | |
| rows={3} | |
| placeholder={lang === "ar" ? "شاركنا تجربتك مع الكورس (اختياري)" : "Share your experience with this course (optional)"} | |
| className="mt-3 w-full rounded-lg border border-[#1f2d44] bg-[#0a0e17] p-3 text-sm text-[#f0f4f8] placeholder:text-[#475569] focus:border-[#06b6d4] focus:outline-none" | |
| /> | |
| <div className="mt-3 flex items-center gap-2"> | |
| <button | |
| type="submit" | |
| disabled={draftRating < 1 || submitMutation.isPending} | |
| className="rounded-lg bg-[#06b6d4] px-4 py-2 text-sm font-semibold text-[#0a0e17] transition-colors hover:bg-[#0891b2] disabled:cursor-not-allowed disabled:opacity-50" | |
| > | |
| {submitButtonLabel(lang, submitMutation.isPending, myReview !== null)} | |
| </button> | |
| <button | |
| type="button" | |
| onClick={() => setShowForm(false)} | |
| className="rounded-lg border border-[#1f2d44] px-4 py-2 text-sm text-[#94a3b8] transition-colors hover:text-[#f0f4f8]" | |
| > | |
| {lang === "ar" ? "إلغاء" : "Cancel"} | |
| </button> | |
| </div> | |
| </form> | |
| )} | |
| {/* ── Reviews list ── */} | |
| {items.length > 0 ? ( | |
| <ul className="mt-6 space-y-4"> | |
| {items.map((r, ri) => ( | |
| <li key={r.id} className={`rounded-lg border border-[#1f2d44] bg-[rgba(10,14,23,0.5)] p-4${ri >= 6 ? " cv-auto-md" : ""}`}> | |
| <div className="flex items-center justify-between gap-3"> | |
| <div className="flex min-w-0 items-center gap-3"> | |
| {/* ✅ Owner request (2026-11-26): the reviewer's real photo | |
| replaces the initials circle; the initials circle stays | |
| as the fallback for reviewers without a photo. */} | |
| {r.userAvatar ? ( | |
| <img | |
| src={r.userAvatar} | |
| alt={reviewerNameOrUnknown(r)} | |
| loading="lazy" | |
| decoding="async" | |
| draggable={false} | |
| className="h-9 w-9 shrink-0 rounded-full border border-[#06b6d4]/30 object-cover" | |
| /> | |
| ) : ( | |
| <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-[#06b6d4]/15 text-sm font-bold text-[#06b6d4]"> | |
| {reviewerNameOrUnknown(r).charAt(0).toUpperCase()} | |
| </span> | |
| )} | |
| <div className="min-w-0"> | |
| <div className="flex items-center gap-1.5"> | |
| <p className="truncate text-sm font-medium text-[#f0f4f8]">{reviewerDisplayName(r, lang)}</p> | |
| {/* Brand Kit §7 proof system — ONE green check badge. | |
| Truthful by construction: the server enforces that | |
| reviews come only from enrolled students, so every | |
| published review IS a verified student. */} | |
| <span | |
| className="flex shrink-0 items-center gap-1 rounded-full bg-[rgba(16,185,129,0.12)] px-1.5 py-0.5 text-[10px] font-semibold text-[#10b981]" | |
| title={lang === "ar" ? "رأي طالب مسجل فعلياً في الكورس (تحقق خادمي)" : "Verified enrolled student (server-enforced)"} | |
| > | |
| <BadgeCheck className="h-3 w-3" /> | |
| {lang === "ar" ? "طالب موثق" : "Verified"} | |
| </span> | |
| </div> | |
| <Stars value={r.rating} size="h-3.5 w-3.5" /> | |
| </div> | |
| </div> | |
| <time className="shrink-0 text-xs text-[#64748b]"> | |
| {new Date(r.createdAt).toLocaleDateString(lang === "ar" ? "ar-EG" : "en-GB", { year: "numeric", month: "short", day: "numeric" })} | |
| </time> | |
| </div> | |
| {r.comment && ( | |
| <p className="mt-3 text-sm leading-relaxed text-[#94a3b8]">{r.comment}</p> | |
| )} | |
| </li> | |
| ))} | |
| </ul> | |
| ) : ( | |
| <div className="mt-6 rounded-lg border border-dashed border-[#1f2d44] p-8 text-center"> | |
| <p className="text-sm text-[#94a3b8]"> | |
| {lang === "ar" ? "لا توجد تقييمات منشورة بعد — كن أول من يشارك تجربته!" : "No published reviews yet — be the first to share your experience!"} | |
| </p> | |
| </div> | |
| )} | |
| {/* ── Pagination ── */} | |
| {totalPages > 1 && ( | |
| <div className="mt-5 flex items-center justify-center gap-3"> | |
| <button | |
| onClick={() => setListPage((p) => Math.max(1, p - 1))} | |
| disabled={listPage <= 1} | |
| className="rounded-lg border border-[#1f2d44] px-3 py-1.5 text-sm text-[#94a3b8] transition-colors hover:border-[#06b6d4] hover:text-[#06b6d4] disabled:cursor-not-allowed disabled:opacity-40" | |
| > | |
| {lang === "ar" ? "السابق" : "Prev"} | |
| </button> | |
| <span className="text-xs text-[#64748b]"> | |
| {lang === "ar" ? `صفحة ${listPage} من ${totalPages}` : `Page ${listPage} of ${totalPages}`} | |
| </span> | |
| <button | |
| onClick={() => setListPage((p) => Math.min(totalPages, p + 1))} | |
| disabled={listPage >= totalPages} | |
| className="rounded-lg border border-[#1f2d44] px-3 py-1.5 text-sm text-[#94a3b8] transition-colors hover:border-[#06b6d4] hover:text-[#06b6d4] disabled:cursor-not-allowed disabled:opacity-40" | |
| > | |
| {lang === "ar" ? "التالي" : "Next"} | |
| </button> | |
| </div> | |
| )} | |
| {/* Entitlement hint for premium non-enrolled users */} | |
| {!isAuthenticated && ( | |
| <p className="mt-4 flex items-center gap-1.5 text-xs text-[#64748b]"> | |
| <Lock className="h-3 w-3" /> | |
| {lang === "ar" ? "التقييم متاح للطلاب المسجلين في الكورس" : "Reviews are for enrolled students"} | |
| </p> | |
| )} | |
| </div> | |
| </section> | |
| ); | |
| } | |