-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathpage.tsx
More file actions
132 lines (113 loc) · 4.87 KB
/
page.tsx
File metadata and controls
132 lines (113 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use client";
import { notFound } from "next/navigation";
import { useEffect, useState } from "react";
import { supabase } from "@/lib/supabase";
import { useCourses } from "@/hooks/useCourses";
import CoursePageHeader from "@/components/courses/course_page/CoursePageHeader";
import CoursePageStats from "@/components/courses/course_page/CoursePageStats";
import CoursePageReviews from "@/components/courses/course_page/CoursePageReviews";
import RateThisCourse from "@/components/courses/course_page/RateThisCourse";
import AddToComparison from "@/components/courses/course_page/AddToComparison";
import CourseSummary from "@/components/courses/course_page/CourseSummary";
import Example from "@/components/courses/course_page/CoursePageLoader";
export default function CoursePage({ params }: { params: { courseId: string } }) {
const { courses, isLoading } = useCourses();
const [averageRating, setAverageRating] = useState(0);
const [reviewCount, setReviewCount] = useState(0);
const [courseUUID, setCourseUUID] = useState<string | null>(null);
const course = courses.find((course) => course.id === params.courseId);
/* ---------- Fetch Course UUID from Supabase ---------- */
useEffect(() => {
if (!course?.code) return;
const fetchCourseUUID = async () => {
const { data, error } = await supabase
.from('courses')
.select('id')
.eq('code', course.code.toUpperCase())
.single();
if (data && !error) {
setCourseUUID(data.id);
} else {
console.error('Error fetching course UUID:', error);
}
};
fetchCourseUUID();
}, [course?.code]);
/* ---------- Fetch Ratings to Compute Avg + Count ---------- */
useEffect(() => {
if (!courseUUID) return;
const fetchRatings = async () => {
const { data, error } = await supabase
.from("reviews")
.select("rating_value")
.eq("target_id", courseUUID)
.eq("target_type", "course");
if (error) {
console.error("Error fetching ratings:", error.message);
return;
}
if (data && data.length > 0) {
const total = data.reduce((sum, r) => sum + (r.rating_value || 0), 0);
const avg = total / data.length;
setAverageRating(parseFloat(avg.toFixed(1)));
setReviewCount(data.length);
} else {
setAverageRating(0);
setReviewCount(0);
}
};
fetchRatings();
}, [courseUUID]);
if (isLoading) {
return <Example />;
}
if (!course) {
notFound();
}
return (
<div className="container px-4 sm:px-6 lg:px-8 py-6 sm:py-8 lg:py-10 min-h-[calc(100vh-6rem)] mx-auto">
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6 sm:gap-8 lg:gap-10">
{/* Left Section */}
<div className="lg:col-span-8 space-y-6 sm:space-y-8">
{/* Course Header with modern styling */}
<div className="bg-gradient-to-br dark:from-blue-600/20 dark:to-purple-700/20 border border-blue-500/30 rounded-2xl p-8 backdrop-blur-md shadow-2xl shadow-blue-500/10 hover:shadow-blue-500/20 transition-all duration-300 ">
<CoursePageHeader
course={course}
averageRating={averageRating}
reviewCount={reviewCount}
/>
</div>
{/* Stats Grid - 2x2 Layout */}
<div className=" rounded-2xl p-6 backdrop-blur-md shadow-xl transition-all duration-300 border border-border bg-gradient-to-b from-background to-muted/40 hover:shadow-primary/20">
<CoursePageStats reviewCount={reviewCount} />
</div>
{/* AI-Generated Course Summary */}
{courseUUID && (
<div className="rounded-2xl backdrop-blur-md shadow-xl transition-all duration-300">
<CourseSummary
courseId={courseUUID}
courseCode={course.code}
courseTitle={course.title}
/>
</div>
)}
{/* Reviews Section with modern container */}
<div className="rounded-2xl p-6 backdrop-blur-md shadow-xl transition-all duration-300 border border-border bg-gradient-to-b from-background to-muted/40 hover:shadow-primary/20">
<CoursePageReviews id={courseUUID || course.id} reviewCount={reviewCount} />
</div>
</div>
{/* Right Section - Sticky Sidebar */}
<div className="lg:col-span-4">
<div className="lg:sticky lg:top-8 space-y-6">
{/* Add to Comparison Card */}
<AddToComparison course={course} />
{/* Rate This Course Card */}
<div className="rounded-2xl p-6 backdrop-blur-md shadow-xl transition-all duration-300 border border-border bg-gradient-to-b from-background to-muted/40 hover:shadow-primary/20">
{courseUUID && <RateThisCourse courseId={courseUUID} />}
</div>
</div>
</div>
</div>
</div>
);
}