Pending course, rest ready for launch
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { requireUser } from "@/lib/auth/requireUser";
|
||||
import { db } from "@/lib/prisma";
|
||||
|
||||
type ToggleLessonCompleteInput = {
|
||||
courseSlug: string;
|
||||
lessonId: string;
|
||||
};
|
||||
|
||||
export async function toggleLessonComplete({ courseSlug, lessonId }: ToggleLessonCompleteInput) {
|
||||
const user = await requireUser();
|
||||
|
||||
if (!user?.id) {
|
||||
return { success: false, error: "Unauthorized" };
|
||||
}
|
||||
|
||||
const lesson = await db.lesson.findUnique({
|
||||
where: { id: lessonId },
|
||||
select: {
|
||||
id: true,
|
||||
module: {
|
||||
select: {
|
||||
courseId: true,
|
||||
course: {
|
||||
select: {
|
||||
slug: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!lesson || lesson.module.course.slug !== courseSlug) {
|
||||
return { success: false, error: "Lesson not found" };
|
||||
}
|
||||
|
||||
const enrollment = await db.enrollment.findUnique({
|
||||
where: {
|
||||
userId_courseId: {
|
||||
userId: user.id,
|
||||
courseId: lesson.module.courseId,
|
||||
},
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!enrollment) {
|
||||
return { success: false, error: "Not enrolled in this course" };
|
||||
}
|
||||
|
||||
const existingProgress = await db.userProgress.findUnique({
|
||||
where: {
|
||||
userId_lessonId: {
|
||||
userId: user.id,
|
||||
lessonId,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
isCompleted: true,
|
||||
},
|
||||
});
|
||||
|
||||
const nextCompleted = !existingProgress?.isCompleted;
|
||||
|
||||
if (existingProgress) {
|
||||
await db.userProgress.update({
|
||||
where: { id: existingProgress.id },
|
||||
data: {
|
||||
isCompleted: nextCompleted,
|
||||
finishedAt: nextCompleted ? new Date() : null,
|
||||
lastPlayedAt: new Date(),
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await db.userProgress.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
lessonId,
|
||||
isCompleted: true,
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
revalidatePath(`/courses/${courseSlug}/learn`);
|
||||
|
||||
return { success: true, isCompleted: nextCompleted };
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { db } from "@/lib/prisma";
|
||||
import { requireUser } from "@/lib/auth/requireUser";
|
||||
import StudentClassroomClient from "@/components/courses/StudentClassroomClient";
|
||||
|
||||
function getText(value: unknown): string {
|
||||
if (!value) return "";
|
||||
if (typeof value === "string") return value;
|
||||
if (typeof value === "object") {
|
||||
const record = value as Record<string, unknown>;
|
||||
if (typeof record.es === "string") return record.es;
|
||||
if (typeof record.en === "string") return record.en;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
type PageProps = {
|
||||
params: Promise<{ slug: string }>;
|
||||
searchParams: Promise<{ lesson?: string }>;
|
||||
};
|
||||
|
||||
export default async function CourseLearnPage({ params, searchParams }: PageProps) {
|
||||
const { slug } = await params;
|
||||
const { lesson: requestedLessonId } = await searchParams;
|
||||
|
||||
const user = await requireUser();
|
||||
if (!user?.id) {
|
||||
redirect(`/courses/${slug}`);
|
||||
}
|
||||
|
||||
const course = await db.course.findUnique({
|
||||
where: { slug },
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
title: true,
|
||||
price: true,
|
||||
modules: {
|
||||
orderBy: { orderIndex: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
lessons: {
|
||||
orderBy: { orderIndex: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
description: true,
|
||||
videoUrl: true,
|
||||
estimatedDuration: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!course) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
let enrollment = await db.enrollment.findUnique({
|
||||
where: {
|
||||
userId_courseId: {
|
||||
userId: user.id,
|
||||
courseId: course.id,
|
||||
},
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!enrollment) {
|
||||
const isFree = Number(course.price) === 0;
|
||||
if (isFree) {
|
||||
enrollment = await db.enrollment.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
courseId: course.id,
|
||||
amountPaid: 0,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
} else {
|
||||
redirect(`/courses/${slug}`);
|
||||
}
|
||||
}
|
||||
|
||||
const completedProgress = await db.userProgress.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
isCompleted: true,
|
||||
lesson: {
|
||||
module: {
|
||||
courseId: course.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
lessonId: true,
|
||||
},
|
||||
});
|
||||
|
||||
const modules = course.modules.map((module) => ({
|
||||
id: module.id,
|
||||
title: getText(module.title) || "Untitled module",
|
||||
lessons: module.lessons.map((lesson) => ({
|
||||
id: lesson.id,
|
||||
title: getText(lesson.title) || "Untitled lesson",
|
||||
description: getText(lesson.description),
|
||||
videoUrl: lesson.videoUrl,
|
||||
estimatedDuration: lesson.estimatedDuration,
|
||||
})),
|
||||
}));
|
||||
|
||||
const flattenedLessonIds = modules.flatMap((module) => module.lessons.map((lesson) => lesson.id));
|
||||
const initialSelectedLessonId =
|
||||
requestedLessonId && flattenedLessonIds.includes(requestedLessonId)
|
||||
? requestedLessonId
|
||||
: flattenedLessonIds[0] ?? "";
|
||||
|
||||
return (
|
||||
<StudentClassroomClient
|
||||
courseSlug={course.slug}
|
||||
courseTitle={getText(course.title) || "Untitled course"}
|
||||
modules={modules}
|
||||
initialSelectedLessonId={initialSelectedLessonId}
|
||||
initialCompletedLessonIds={completedProgress.map((progress) => progress.lessonId)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
229
app/(protected)/my-courses/page.tsx
Normal file
229
app/(protected)/my-courses/page.tsx
Normal file
@@ -0,0 +1,229 @@
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { UserRole } from "@prisma/client";
|
||||
import { requireUser } from "@/lib/auth/requireUser";
|
||||
import { db } from "@/lib/prisma";
|
||||
|
||||
function getText(value: unknown): string {
|
||||
if (!value) return "";
|
||||
if (typeof value === "string") return value;
|
||||
if (typeof value === "object") {
|
||||
const record = value as Record<string, unknown>;
|
||||
if (typeof record.en === "string") return record.en;
|
||||
if (typeof record.es === "string") return record.es;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export default async function MyCoursesPage() {
|
||||
const user = await requireUser();
|
||||
if (!user?.id) {
|
||||
redirect("/auth/login?redirectTo=/my-courses");
|
||||
}
|
||||
|
||||
const isTeacher = user.role === UserRole.TEACHER || user.role === UserRole.SUPER_ADMIN;
|
||||
|
||||
if (isTeacher) {
|
||||
const courses = await db.course.findMany({
|
||||
where: { authorId: user.id },
|
||||
include: {
|
||||
modules: {
|
||||
include: {
|
||||
lessons: {
|
||||
select: {
|
||||
id: true,
|
||||
videoUrl: true,
|
||||
youtubeUrl: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
_count: {
|
||||
select: {
|
||||
enrollments: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="acve-page">
|
||||
<section className="acve-panel acve-section-base">
|
||||
<p className="acve-pill mb-3 w-fit">My Courses</p>
|
||||
<h1 className="text-3xl font-semibold leading-tight text-[#222a38] md:text-4xl">Your created courses</h1>
|
||||
<p className="mt-2 max-w-3xl text-base leading-relaxed text-slate-600">
|
||||
Review, edit, and publish the courses you are building for your students.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
{courses.length === 0 ? (
|
||||
<section className="acve-panel p-6">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">No courses created yet</h2>
|
||||
<p className="mt-2 text-slate-600">Create your first teacher course to get started.</p>
|
||||
<Link className="acve-button-primary mt-4 inline-flex px-4 py-2 text-sm font-semibold" href="/teacher/courses/new">
|
||||
Create course
|
||||
</Link>
|
||||
</section>
|
||||
) : (
|
||||
<section className="grid gap-4 md:grid-cols-2">
|
||||
{courses.map((course) => {
|
||||
const totalLessons = course.modules.reduce((acc, module) => acc + module.lessons.length, 0);
|
||||
const lessonsWithVideo = course.modules.reduce(
|
||||
(acc, module) => acc + module.lessons.filter((lesson) => lesson.videoUrl || lesson.youtubeUrl).length,
|
||||
0,
|
||||
);
|
||||
const title = getText(course.title) || "Untitled course";
|
||||
|
||||
return (
|
||||
<article key={course.id} className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-[#222a38]">{title}</h2>
|
||||
<p className="mt-2 text-sm text-slate-600">
|
||||
{course._count.enrollments} students | {course.modules.length} modules | {totalLessons} lessons
|
||||
</p>
|
||||
<p className="mt-1 text-sm text-slate-600">
|
||||
Upload coverage: {lessonsWithVideo}/{totalLessons || 0} lessons with video
|
||||
</p>
|
||||
<div className="mt-4 flex flex-wrap gap-2">
|
||||
<Link
|
||||
className="acve-button-primary inline-flex px-4 py-2 text-sm font-semibold"
|
||||
href={`/teacher/courses/${course.slug}/edit`}
|
||||
>
|
||||
Edit course
|
||||
</Link>
|
||||
<Link
|
||||
className="rounded-md border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50"
|
||||
href={`/courses/${course.slug}`}
|
||||
target="_blank"
|
||||
>
|
||||
Preview
|
||||
</Link>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
})}
|
||||
</section>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const enrollments = await db.enrollment.findMany({
|
||||
where: { userId: user.id },
|
||||
include: {
|
||||
course: {
|
||||
include: {
|
||||
modules: {
|
||||
include: {
|
||||
lessons: {
|
||||
select: { id: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { purchasedAt: "desc" },
|
||||
});
|
||||
|
||||
const courseIds = enrollments.map((enrollment) => enrollment.courseId);
|
||||
const completed = await db.userProgress.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
isCompleted: true,
|
||||
lesson: {
|
||||
module: {
|
||||
courseId: {
|
||||
in: courseIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
lesson: {
|
||||
select: {
|
||||
module: {
|
||||
select: { courseId: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const certificates = await db.certificate.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
courseId: {
|
||||
in: courseIds,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
courseId: true,
|
||||
},
|
||||
});
|
||||
|
||||
const completedByCourse = new Map<string, number>();
|
||||
for (const item of completed) {
|
||||
const courseId = item.lesson.module.courseId;
|
||||
completedByCourse.set(courseId, (completedByCourse.get(courseId) ?? 0) + 1);
|
||||
}
|
||||
const certificateByCourse = new Map(certificates.map((certificate) => [certificate.courseId, certificate.id]));
|
||||
|
||||
return (
|
||||
<div className="acve-page">
|
||||
<section className="acve-panel acve-section-base">
|
||||
<p className="acve-pill mb-3 w-fit">My Courses</p>
|
||||
<h1 className="text-3xl font-semibold leading-tight text-[#222a38] md:text-4xl">Your enrolled courses</h1>
|
||||
<p className="mt-2 max-w-3xl text-base leading-relaxed text-slate-600">
|
||||
Continue where you left off, check completion, and download certificates for finished courses.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
{enrollments.length === 0 ? (
|
||||
<section className="acve-panel p-6">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">No courses enrolled yet</h2>
|
||||
<p className="mt-2 text-slate-600">Browse the catalog and start your first learning path.</p>
|
||||
<Link className="acve-button-primary mt-4 inline-flex px-4 py-2 text-sm font-semibold" href="/courses">
|
||||
Browse courses
|
||||
</Link>
|
||||
</section>
|
||||
) : (
|
||||
<section className="grid gap-4 md:grid-cols-2">
|
||||
{enrollments.map((enrollment) => {
|
||||
const totalLessons = enrollment.course.modules.reduce((acc, module) => acc + module.lessons.length, 0);
|
||||
const completedLessons = completedByCourse.get(enrollment.course.id) ?? 0;
|
||||
const progressPercent = totalLessons > 0 ? Math.round((completedLessons / totalLessons) * 100) : 0;
|
||||
const courseTitle = getText(enrollment.course.title) || "Untitled course";
|
||||
const certificateId = certificateByCourse.get(enrollment.course.id);
|
||||
|
||||
return (
|
||||
<article key={enrollment.id} className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-[#222a38]">{courseTitle}</h2>
|
||||
<p className="mt-2 text-sm text-slate-600">
|
||||
Progress: {completedLessons}/{totalLessons} lessons ({progressPercent}%)
|
||||
</p>
|
||||
<div className="mt-4 flex flex-wrap gap-2">
|
||||
<Link
|
||||
className="acve-button-primary inline-flex px-4 py-2 text-sm font-semibold"
|
||||
href={`/courses/${enrollment.course.slug}/learn`}
|
||||
>
|
||||
{progressPercent >= 100 ? "Review" : "Continue"}
|
||||
</Link>
|
||||
{certificateId ? (
|
||||
<Link
|
||||
className="rounded-md border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50"
|
||||
href={`/api/certificates/${certificateId}/pdf`}
|
||||
>
|
||||
Download Certificate
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
})}
|
||||
</section>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
113
app/(protected)/practice/[slug]/actions.ts
Normal file
113
app/(protected)/practice/[slug]/actions.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { requireUser } from "@/lib/auth/requireUser";
|
||||
import { mockPracticeModules } from "@/lib/data/mockPractice";
|
||||
import { db } from "@/lib/prisma";
|
||||
import { refreshStudyRecommendations } from "@/lib/recommendations";
|
||||
|
||||
type SubmitAttemptInput = {
|
||||
slug: string;
|
||||
selectedAnswers: number[];
|
||||
};
|
||||
|
||||
type PracticePrismaClient = {
|
||||
miniGame: {
|
||||
upsert: (args: object) => Promise<{ id: string }>;
|
||||
};
|
||||
miniGameAttempt: {
|
||||
create: (args: object) => Promise<unknown>;
|
||||
findMany: (args: object) => Promise<
|
||||
{ id: string; scorePercent: number; correctCount: number; totalQuestions: number; completedAt: Date }[]
|
||||
>;
|
||||
};
|
||||
};
|
||||
|
||||
function toDifficulty(level?: string): "BEGINNER" | "INTERMEDIATE" | "ADVANCED" {
|
||||
if (level === "Beginner") return "BEGINNER";
|
||||
if (level === "Advanced") return "ADVANCED";
|
||||
return "INTERMEDIATE";
|
||||
}
|
||||
|
||||
export async function submitPracticeAttempt({ slug, selectedAnswers }: SubmitAttemptInput) {
|
||||
const user = await requireUser();
|
||||
if (!user?.id) return { success: false as const, error: "Unauthorized" };
|
||||
|
||||
const practiceModule = mockPracticeModules.find((item) => item.slug === slug && item.isInteractive && item.questions?.length);
|
||||
if (!practiceModule || !practiceModule.questions) return { success: false as const, error: "Practice module not found" };
|
||||
|
||||
const correctCount = practiceModule.questions.reduce((acc, question, index) => {
|
||||
return acc + (selectedAnswers[index] === question.answerIndex ? 1 : 0);
|
||||
}, 0);
|
||||
const total = practiceModule.questions.length;
|
||||
const scorePercent = Math.round((correctCount / total) * 100);
|
||||
const prismaMini = db as unknown as PracticePrismaClient;
|
||||
|
||||
try {
|
||||
const miniGame = await prismaMini.miniGame.upsert({
|
||||
where: { slug: practiceModule.slug },
|
||||
update: {
|
||||
title: practiceModule.title,
|
||||
description: practiceModule.description,
|
||||
isActive: true,
|
||||
difficulty: toDifficulty(practiceModule.difficulty),
|
||||
},
|
||||
create: {
|
||||
slug: practiceModule.slug,
|
||||
title: practiceModule.title,
|
||||
description: practiceModule.description,
|
||||
isActive: true,
|
||||
difficulty: toDifficulty(practiceModule.difficulty),
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
await prismaMini.miniGameAttempt.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
miniGameId: miniGame.id,
|
||||
scorePercent,
|
||||
correctCount,
|
||||
totalQuestions: total,
|
||||
},
|
||||
});
|
||||
|
||||
await refreshStudyRecommendations(user.id);
|
||||
} catch {
|
||||
return { success: false as const, error: "Mini-game tables are not migrated yet" };
|
||||
}
|
||||
revalidatePath("/profile");
|
||||
|
||||
return { success: true as const, scorePercent, correctCount, total };
|
||||
}
|
||||
|
||||
export async function getPracticeAttempts(slug: string) {
|
||||
const user = await requireUser();
|
||||
if (!user?.id) return [];
|
||||
|
||||
try {
|
||||
const attempts = await (db as unknown as PracticePrismaClient).miniGameAttempt.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
miniGame: {
|
||||
slug,
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
completedAt: "desc",
|
||||
},
|
||||
take: 5,
|
||||
select: {
|
||||
id: true,
|
||||
scorePercent: true,
|
||||
correctCount: true,
|
||||
totalQuestions: true,
|
||||
completedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return attempts;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
90
app/(protected)/practice/[slug]/page.tsx
Executable file → Normal file
90
app/(protected)/practice/[slug]/page.tsx
Executable file → Normal file
@@ -1,17 +1,18 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useState, useTransition } from "react";
|
||||
import Link from "next/link";
|
||||
import { useParams } from "next/navigation";
|
||||
import ProgressBar from "@/components/ProgressBar";
|
||||
import { getPracticeBySlug, mockPracticeModules } from "@/lib/data/mockPractice";
|
||||
|
||||
const attemptsKey = (slug: string) => `acve.practice-attempts.${slug}`;
|
||||
import { getPracticeAttempts, submitPracticeAttempt } from "@/app/(protected)/practice/[slug]/actions";
|
||||
|
||||
type AttemptRecord = {
|
||||
completedAt: string;
|
||||
score: number;
|
||||
total: number;
|
||||
id: string;
|
||||
scorePercent: number;
|
||||
correctCount: number;
|
||||
totalQuestions: number;
|
||||
completedAt: Date;
|
||||
};
|
||||
|
||||
export default function PracticeExercisePage() {
|
||||
@@ -23,21 +24,16 @@ export default function PracticeExercisePage() {
|
||||
const [score, setScore] = useState(0);
|
||||
const [finished, setFinished] = useState(false);
|
||||
const [selected, setSelected] = useState<number | null>(null);
|
||||
const [selectedAnswers, setSelectedAnswers] = useState<number[]>([]);
|
||||
const [attempts, setAttempts] = useState<AttemptRecord[]>([]);
|
||||
const [isSaving, startTransition] = useTransition();
|
||||
|
||||
const loadAttempts = () => {
|
||||
if (!practiceModule) return;
|
||||
if (typeof window === "undefined") return;
|
||||
const raw = window.localStorage.getItem(attemptsKey(practiceModule.slug));
|
||||
if (!raw) {
|
||||
setAttempts([]);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setAttempts(JSON.parse(raw) as AttemptRecord[]);
|
||||
} catch {
|
||||
setAttempts([]);
|
||||
}
|
||||
startTransition(async () => {
|
||||
const result = await getPracticeAttempts(practiceModule.slug);
|
||||
setAttempts(result as AttemptRecord[]);
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@@ -79,21 +75,23 @@ export default function PracticeExercisePage() {
|
||||
};
|
||||
|
||||
const next = () => {
|
||||
if (selected === null) return;
|
||||
setSelectedAnswers((prev) => {
|
||||
const nextAnswers = [...prev];
|
||||
nextAnswers[index] = selected;
|
||||
return nextAnswers;
|
||||
});
|
||||
|
||||
if (index + 1 >= total) {
|
||||
if (typeof window !== "undefined") {
|
||||
const raw = window.localStorage.getItem(attemptsKey(practiceModule.slug));
|
||||
const parsed = raw ? ((JSON.parse(raw) as AttemptRecord[]) ?? []) : [];
|
||||
const nextAttempts = [
|
||||
{
|
||||
completedAt: new Date().toISOString(),
|
||||
score,
|
||||
total,
|
||||
},
|
||||
...parsed,
|
||||
].slice(0, 5);
|
||||
window.localStorage.setItem(attemptsKey(practiceModule.slug), JSON.stringify(nextAttempts));
|
||||
setAttempts(nextAttempts);
|
||||
}
|
||||
const finalAnswers = [...selectedAnswers];
|
||||
finalAnswers[index] = selected;
|
||||
startTransition(async () => {
|
||||
await submitPracticeAttempt({
|
||||
slug: practiceModule.slug,
|
||||
selectedAnswers: finalAnswers,
|
||||
});
|
||||
loadAttempts();
|
||||
});
|
||||
setFinished(true);
|
||||
return;
|
||||
}
|
||||
@@ -106,6 +104,7 @@ export default function PracticeExercisePage() {
|
||||
setIndex(0);
|
||||
setScore(0);
|
||||
setSelected(null);
|
||||
setSelectedAnswers([]);
|
||||
setFinished(false);
|
||||
};
|
||||
|
||||
@@ -115,6 +114,7 @@ export default function PracticeExercisePage() {
|
||||
setIndex(0);
|
||||
setScore(0);
|
||||
setSelected(null);
|
||||
setSelectedAnswers([]);
|
||||
};
|
||||
|
||||
if (finished) {
|
||||
@@ -130,9 +130,6 @@ export default function PracticeExercisePage() {
|
||||
<button className="acve-button-primary px-5 py-2 text-sm font-semibold hover:brightness-105" onClick={restart} type="button">
|
||||
Retake quiz
|
||||
</button>
|
||||
<button className="rounded-md border border-slate-300 px-5 py-2 text-sm font-semibold text-slate-700" type="button">
|
||||
Review answers (placeholder)
|
||||
</button>
|
||||
<Link className="rounded-md border border-slate-300 px-5 py-2 text-sm font-semibold text-slate-700" href="/practice">
|
||||
Back to modules
|
||||
</Link>
|
||||
@@ -140,14 +137,15 @@ export default function PracticeExercisePage() {
|
||||
</section>
|
||||
|
||||
<section className="acve-panel p-4">
|
||||
<h2 className="text-base font-semibold text-slate-800">Attempt History (Mock)</h2>
|
||||
<h2 className="text-base font-semibold text-slate-800">Attempt History</h2>
|
||||
{attempts.length === 0 ? (
|
||||
<p className="mt-2 text-sm text-slate-600">No attempts recorded yet.</p>
|
||||
) : (
|
||||
<ul className="mt-2 space-y-2">
|
||||
{attempts.map((attempt) => (
|
||||
<li key={attempt.completedAt} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
Score {attempt.score}/{attempt.total} on {new Date(attempt.completedAt).toLocaleString()}
|
||||
<li key={attempt.id} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
Score {attempt.correctCount}/{attempt.totalQuestions} ({attempt.scorePercent}%) on{" "}
|
||||
{new Date(attempt.completedAt).toLocaleString()}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
@@ -175,7 +173,7 @@ export default function PracticeExercisePage() {
|
||||
</div>
|
||||
<div className="rounded-xl border border-slate-200 bg-white p-3">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Difficulty</p>
|
||||
<p className="mt-1 text-2xl font-semibold text-slate-900">Intermediate</p>
|
||||
<p className="mt-1 text-2xl font-semibold text-slate-900">{practiceModule.difficulty ?? "Intermediate"}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-5 flex flex-wrap items-center gap-2">
|
||||
@@ -189,14 +187,15 @@ export default function PracticeExercisePage() {
|
||||
</section>
|
||||
|
||||
<section className="acve-panel p-4">
|
||||
<h2 className="text-base font-semibold text-slate-800">Attempt History (Mock)</h2>
|
||||
<h2 className="text-base font-semibold text-slate-800">Attempt History</h2>
|
||||
{attempts.length === 0 ? (
|
||||
<p className="mt-2 text-sm text-slate-600">No attempts recorded yet for this module.</p>
|
||||
) : (
|
||||
<ul className="mt-2 space-y-2">
|
||||
{attempts.map((attempt) => (
|
||||
<li key={attempt.completedAt} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
Score {attempt.score}/{attempt.total} on {new Date(attempt.completedAt).toLocaleString()}
|
||||
<li key={attempt.id} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
Score {attempt.correctCount}/{attempt.totalQuestions} ({attempt.scorePercent}%) on{" "}
|
||||
{new Date(attempt.completedAt).toLocaleString()}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
@@ -237,14 +236,15 @@ export default function PracticeExercisePage() {
|
||||
</section>
|
||||
|
||||
<section className="acve-panel p-4">
|
||||
<h2 className="text-lg font-semibold text-slate-800">Attempt History (Mock)</h2>
|
||||
<h2 className="text-lg font-semibold text-slate-800">Attempt History</h2>
|
||||
{attempts.length === 0 ? (
|
||||
<p className="mt-2 text-sm text-slate-600">No attempts recorded yet.</p>
|
||||
) : (
|
||||
<ul className="mt-2 space-y-2">
|
||||
{attempts.map((attempt) => (
|
||||
<li key={attempt.completedAt} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
Score {attempt.score}/{attempt.total} on {new Date(attempt.completedAt).toLocaleString()}
|
||||
<li key={attempt.id} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
Score {attempt.correctCount}/{attempt.totalQuestions} ({attempt.scorePercent}%) on{" "}
|
||||
{new Date(attempt.completedAt).toLocaleString()}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
@@ -285,7 +285,7 @@ export default function PracticeExercisePage() {
|
||||
|
||||
<button
|
||||
className="acve-button-primary mt-6 px-6 py-2 text-sm font-semibold hover:brightness-105 disabled:opacity-50"
|
||||
disabled={selected === null}
|
||||
disabled={selected === null || isSaving}
|
||||
onClick={next}
|
||||
type="button"
|
||||
>
|
||||
|
||||
375
app/(protected)/profile/page.tsx
Normal file
375
app/(protected)/profile/page.tsx
Normal file
@@ -0,0 +1,375 @@
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { UserRole } from "@prisma/client";
|
||||
import { requireUser } from "@/lib/auth/requireUser";
|
||||
import { db } from "@/lib/prisma";
|
||||
import { getActiveRecommendations, getMiniGameGrade } from "@/lib/recommendations";
|
||||
|
||||
type ProfilePrismaClient = {
|
||||
miniGameAttempt: {
|
||||
findMany: (args: object) => Promise<
|
||||
{
|
||||
miniGameId: string;
|
||||
scorePercent: number;
|
||||
miniGame: { id: string; title: string; slug: string };
|
||||
}[]
|
||||
>;
|
||||
};
|
||||
};
|
||||
|
||||
function getText(value: unknown): string {
|
||||
if (!value) return "";
|
||||
if (typeof value === "string") return value;
|
||||
if (typeof value === "object") {
|
||||
const record = value as Record<string, unknown>;
|
||||
if (typeof record.en === "string") return record.en;
|
||||
if (typeof record.es === "string") return record.es;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
const levelLabel: Record<string, string> = {
|
||||
BEGINNER: "Beginner",
|
||||
INTERMEDIATE: "Intermediate",
|
||||
ADVANCED: "Advanced",
|
||||
EXPERT: "Expert",
|
||||
};
|
||||
|
||||
export default async function ProfilePage() {
|
||||
const user = await requireUser();
|
||||
if (!user?.id) {
|
||||
redirect("/auth/login?redirectTo=/profile");
|
||||
}
|
||||
|
||||
const isTeacher = user.role === UserRole.TEACHER || user.role === UserRole.SUPER_ADMIN;
|
||||
|
||||
if (isTeacher) {
|
||||
const authoredCourses = await db.course
|
||||
.findMany({
|
||||
where: { authorId: user.id },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
slug: true,
|
||||
tags: true,
|
||||
level: true,
|
||||
status: true,
|
||||
learningOutcomes: true,
|
||||
updatedAt: true,
|
||||
_count: {
|
||||
select: {
|
||||
modules: true,
|
||||
enrollments: true,
|
||||
},
|
||||
},
|
||||
modules: {
|
||||
select: {
|
||||
lessons: {
|
||||
select: {
|
||||
id: true,
|
||||
videoUrl: true,
|
||||
youtubeUrl: true,
|
||||
isFreePreview: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { updatedAt: "desc" },
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to load authored courses for profile.", error);
|
||||
return [];
|
||||
});
|
||||
|
||||
const totalCourses = authoredCourses.length;
|
||||
const publishedCourses = authoredCourses.filter((course) => course.status === "PUBLISHED").length;
|
||||
const totalModules = authoredCourses.reduce((acc, course) => acc + course._count.modules, 0);
|
||||
const totalStudents = authoredCourses.reduce((acc, course) => acc + course._count.enrollments, 0);
|
||||
|
||||
const lessons = authoredCourses.flatMap((course) => course.modules.flatMap((module) => module.lessons));
|
||||
const totalLessons = lessons.length;
|
||||
const uploadedLessons = lessons.filter((lesson) => lesson.videoUrl || lesson.youtubeUrl).length;
|
||||
const previewLessons = lessons.filter((lesson) => lesson.isFreePreview).length;
|
||||
|
||||
const topicSet = new Set<string>();
|
||||
const outcomeSet = new Set<string>();
|
||||
for (const course of authoredCourses) {
|
||||
for (const tag of course.tags) {
|
||||
if (tag.trim()) topicSet.add(tag.trim());
|
||||
}
|
||||
if (Array.isArray(course.learningOutcomes)) {
|
||||
for (const outcome of course.learningOutcomes) {
|
||||
if (typeof outcome === "string" && outcome.trim()) {
|
||||
outcomeSet.add(outcome.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const masteredTopics = topicSet.size > 0 ? [...topicSet] : [...new Set(authoredCourses.map((course) => levelLabel[course.level] ?? course.level))];
|
||||
const strengths = [...outcomeSet].slice(0, 8);
|
||||
|
||||
return (
|
||||
<div className="acve-page">
|
||||
<section className="acve-panel acve-section-base">
|
||||
<p className="acve-pill mb-3 w-fit">Profile</p>
|
||||
<h1 className="text-3xl font-semibold leading-tight text-[#222a38] md:text-4xl">{user.fullName || user.email}</h1>
|
||||
<p className="mt-2 text-base text-slate-600">Teacher account | {user.email}</p>
|
||||
</section>
|
||||
|
||||
<section className="grid gap-4 md:grid-cols-4">
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Courses created</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{totalCourses}</p>
|
||||
</article>
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Published</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{publishedCourses}</p>
|
||||
</article>
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Students</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{totalStudents}</p>
|
||||
</article>
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Content units</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{totalModules + totalLessons}</p>
|
||||
<p className="mt-2 text-sm text-slate-600">{totalModules} modules + {totalLessons} lessons</p>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section className="grid gap-4 lg:grid-cols-2">
|
||||
<article className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">Topics mastered</h2>
|
||||
{masteredTopics.length === 0 ? (
|
||||
<p className="mt-3 text-sm text-slate-600">No topic metadata yet. Add tags to your courses.</p>
|
||||
) : (
|
||||
<div className="mt-3 flex flex-wrap gap-2">
|
||||
{masteredTopics.map((topic) => (
|
||||
<span key={topic} className="rounded-full border border-slate-300 px-3 py-1 text-xs font-semibold text-slate-700">
|
||||
{topic}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{strengths.length > 0 ? (
|
||||
<ul className="mt-4 space-y-2 text-sm text-slate-700">
|
||||
{strengths.map((item) => (
|
||||
<li key={item} className="rounded-lg border border-slate-200 px-3 py-2">
|
||||
{item}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</article>
|
||||
|
||||
<article className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">Upload information</h2>
|
||||
<ul className="mt-3 space-y-2 text-sm text-slate-700">
|
||||
<li className="rounded-lg border border-slate-200 px-3 py-2">
|
||||
Lessons with media: {uploadedLessons}/{totalLessons || 0}
|
||||
</li>
|
||||
<li className="rounded-lg border border-slate-200 px-3 py-2">Preview lessons enabled: {previewLessons}</li>
|
||||
<li className="rounded-lg border border-slate-200 px-3 py-2">Lessons pending upload: {Math.max(totalLessons - uploadedLessons, 0)}</li>
|
||||
</ul>
|
||||
<Link className="mt-4 inline-flex text-sm font-semibold text-brand hover:underline" href="/teacher/uploads">
|
||||
Open upload library
|
||||
</Link>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">Recent course activity</h2>
|
||||
{authoredCourses.length === 0 ? (
|
||||
<p className="mt-3 text-sm text-slate-600">No courses yet. Start by creating a new course.</p>
|
||||
) : (
|
||||
<ul className="mt-3 space-y-2">
|
||||
{authoredCourses.slice(0, 6).map((course) => (
|
||||
<li key={course.id} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
<p className="font-semibold text-slate-900">{getText(course.title) || "Untitled course"}</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
{course.status} | {course._count.modules} modules | {course._count.enrollments} students | updated{" "}
|
||||
{new Date(course.updatedAt).toLocaleDateString()}
|
||||
</p>
|
||||
<Link className="mt-1 inline-flex text-xs font-semibold text-brand hover:underline" href={`/teacher/courses/${course.slug}/edit`}>
|
||||
Open editor
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const [grade, recommendations, certificates] = await Promise.all([
|
||||
getMiniGameGrade(user.id).catch((error) => {
|
||||
console.error("Failed to load mini-game grade.", error);
|
||||
return 0;
|
||||
}),
|
||||
getActiveRecommendations(user.id).catch((error) => {
|
||||
console.error("Failed to load recommendations.", error);
|
||||
return [];
|
||||
}),
|
||||
db.certificate
|
||||
.findMany({
|
||||
where: { userId: user.id },
|
||||
select: {
|
||||
id: true,
|
||||
issuedAt: true,
|
||||
metadataSnapshot: true,
|
||||
course: {
|
||||
select: {
|
||||
title: true,
|
||||
slug: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { issuedAt: "desc" },
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to load certificates for profile.", error);
|
||||
return [];
|
||||
}),
|
||||
]);
|
||||
|
||||
let attempts: {
|
||||
miniGameId: string;
|
||||
scorePercent: number;
|
||||
miniGame: { id: string; title: string; slug: string };
|
||||
}[] = [];
|
||||
try {
|
||||
attempts = await (db as unknown as ProfilePrismaClient).miniGameAttempt.findMany({
|
||||
where: { userId: user.id },
|
||||
include: {
|
||||
miniGame: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
slug: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { completedAt: "desc" },
|
||||
});
|
||||
} catch {
|
||||
attempts = [];
|
||||
}
|
||||
|
||||
const latestByGame = new Map<string, number>();
|
||||
const bestByGame = new Map<string, number>();
|
||||
const titleByGame = new Map<string, string>();
|
||||
for (const attempt of attempts) {
|
||||
if (!latestByGame.has(attempt.miniGameId)) {
|
||||
latestByGame.set(attempt.miniGameId, attempt.scorePercent);
|
||||
}
|
||||
const best = bestByGame.get(attempt.miniGameId) ?? 0;
|
||||
if (attempt.scorePercent > best) {
|
||||
bestByGame.set(attempt.miniGameId, attempt.scorePercent);
|
||||
}
|
||||
titleByGame.set(attempt.miniGameId, attempt.miniGame.title);
|
||||
}
|
||||
|
||||
const gameRows = [...titleByGame.keys()].map((gameId) => ({
|
||||
gameId,
|
||||
title: titleByGame.get(gameId) ?? "Mini-game",
|
||||
latest: latestByGame.get(gameId) ?? 0,
|
||||
best: bestByGame.get(gameId) ?? 0,
|
||||
}));
|
||||
|
||||
return (
|
||||
<div className="acve-page">
|
||||
<section className="acve-panel acve-section-base">
|
||||
<p className="acve-pill mb-3 w-fit">Profile</p>
|
||||
<h1 className="text-3xl font-semibold leading-tight text-[#222a38] md:text-4xl">{user.fullName || user.email}</h1>
|
||||
<p className="mt-2 text-base text-slate-600">{user.email}</p>
|
||||
</section>
|
||||
|
||||
<section className="grid gap-4 md:grid-cols-3">
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Mini-game grade</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{grade}%</p>
|
||||
<p className="mt-2 text-sm text-slate-600">Average of latest attempts across mini-games.</p>
|
||||
</article>
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Attempts</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{attempts.length}</p>
|
||||
<Link className="mt-2 inline-flex text-sm font-semibold text-brand hover:underline" href="/practice">
|
||||
Play mini-games
|
||||
</Link>
|
||||
</article>
|
||||
<article className="acve-panel p-5">
|
||||
<p className="text-xs font-semibold uppercase tracking-wide text-slate-500">Certificates</p>
|
||||
<p className="mt-2 text-4xl font-semibold text-slate-900">{certificates.length}</p>
|
||||
<Link className="mt-2 inline-flex text-sm font-semibold text-brand hover:underline" href="/my-courses">
|
||||
View my courses
|
||||
</Link>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section className="grid gap-4 lg:grid-cols-2">
|
||||
<article className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">Mini-game breakdown</h2>
|
||||
{gameRows.length === 0 ? (
|
||||
<p className="mt-3 text-sm text-slate-600">No attempts yet. Complete a mini-game to generate your grade.</p>
|
||||
) : (
|
||||
<ul className="mt-3 space-y-2">
|
||||
{gameRows.map((row) => (
|
||||
<li key={row.gameId} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
<span className="font-semibold text-slate-900">{row.title}</span> | latest: {row.latest}% | best: {row.best}%
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</article>
|
||||
|
||||
<article className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">Recommended next</h2>
|
||||
{recommendations.length === 0 ? (
|
||||
<p className="mt-3 text-sm text-slate-600">No recommendations yet. Complete a mini-game or enroll in a course first.</p>
|
||||
) : (
|
||||
<ul className="mt-3 space-y-2">
|
||||
{recommendations.map((recommendation) => (
|
||||
<li key={recommendation.courseId} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
<p className="font-semibold text-slate-900">{recommendation.title}</p>
|
||||
<p className="text-xs text-slate-500">{recommendation.reason}</p>
|
||||
<Link className="mt-1 inline-flex text-xs font-semibold text-brand hover:underline" href={`/courses/${recommendation.slug}`}>
|
||||
Open course
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section className="acve-panel p-5">
|
||||
<h2 className="text-2xl font-semibold text-slate-900">Certificates</h2>
|
||||
{certificates.length === 0 ? (
|
||||
<p className="mt-3 text-sm text-slate-600">No certificates issued yet.</p>
|
||||
) : (
|
||||
<ul className="mt-3 space-y-2">
|
||||
{certificates.map((certificate) => {
|
||||
const certificateNumber =
|
||||
(certificate as Record<string, unknown>).certificateNumber ??
|
||||
(certificate.metadataSnapshot as Record<string, unknown> | null)?.certificateNumber ??
|
||||
"N/A";
|
||||
return (
|
||||
<li key={certificate.id} className="rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
|
||||
<p className="font-semibold text-slate-900">{getText(certificate.course.title) || "Course"}</p>
|
||||
<p className="text-xs text-slate-500">
|
||||
Certificate #{String(certificateNumber)} | issued {new Date(certificate.issuedAt).toLocaleDateString()}
|
||||
</p>
|
||||
<Link className="mt-1 inline-flex text-xs font-semibold text-brand hover:underline" href={`/api/certificates/${certificate.id}/pdf`}>
|
||||
Download PDF
|
||||
</Link>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -5,6 +5,11 @@ import { requireTeacher } from "@/lib/auth/requireTeacher";
|
||||
import { z } from "zod";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { ContentStatus, Prisma, ProficiencyLevel } from "@prisma/client";
|
||||
import {
|
||||
buildLessonDescriptionMeta,
|
||||
parseLessonDescriptionMeta,
|
||||
type LessonContentType,
|
||||
} from "@/lib/courses/lessonContent";
|
||||
|
||||
// --- VALIDATION SCHEMAS (Zod) ---
|
||||
|
||||
@@ -129,6 +134,17 @@ export async function deleteCourse(courseId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function parseLearningOutcomes(raw: FormDataEntryValue | null): string[] {
|
||||
if (raw == null || typeof raw !== "string") return [];
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as unknown;
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.filter((x): x is string => typeof x === "string");
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateCourse(courseId: string, courseSlug: string, formData: FormData) {
|
||||
const user = await requireTeacher();
|
||||
if (!user) return { success: false, error: "Unauthorized" };
|
||||
@@ -140,6 +156,7 @@ export async function updateCourse(courseId: string, courseSlug: string, formDat
|
||||
const level = formData.get("level") as ProficiencyLevel;
|
||||
const status = formData.get("status") as ContentStatus;
|
||||
const price = parseFloat(formData.get("price") as string) || 0;
|
||||
const learningOutcomes = parseLearningOutcomes(formData.get("learningOutcomes"));
|
||||
|
||||
await db.course.update({
|
||||
where: { id: courseId, authorId: user.id },
|
||||
@@ -149,12 +166,16 @@ export async function updateCourse(courseId: string, courseSlug: string, formDat
|
||||
level,
|
||||
status,
|
||||
price,
|
||||
},
|
||||
learningOutcomes:
|
||||
learningOutcomes.length > 0 ? (learningOutcomes as Prisma.InputJsonValue) : Prisma.JsonNull,
|
||||
} as Prisma.CourseUpdateInput,
|
||||
});
|
||||
|
||||
// Revalidate both the list and the editor (edit route uses slug, not id)
|
||||
// Revalidate teacher list, editor page + layout (so router.refresh() gets fresh data), and public catalog
|
||||
revalidatePath("/teacher/courses");
|
||||
revalidatePath(`/teacher/courses/${courseSlug}/edit`, "page");
|
||||
revalidatePath(`/teacher/courses/${courseSlug}/edit`, "layout");
|
||||
revalidatePath("/courses");
|
||||
return { success: true };
|
||||
} catch {
|
||||
return { success: false, error: "Failed to update" };
|
||||
@@ -163,8 +184,13 @@ export async function updateCourse(courseId: string, courseSlug: string, formDat
|
||||
|
||||
export async function updateLesson(lessonId: string, data: {
|
||||
title?: string;
|
||||
description?: Prisma.InputJsonValue | null;
|
||||
description?: string;
|
||||
videoUrl?: string;
|
||||
youtubeUrl?: string;
|
||||
materialUrl?: string;
|
||||
contentType?: LessonContentType;
|
||||
estimatedDurationMinutes?: number;
|
||||
isPreview?: boolean; // maps to DB field isFreePreview
|
||||
isPublished?: boolean; // optional: for later
|
||||
}) {
|
||||
const user = await requireTeacher();
|
||||
@@ -173,8 +199,30 @@ export async function updateLesson(lessonId: string, data: {
|
||||
try {
|
||||
const updateData: Prisma.LessonUpdateInput = { updatedAt: new Date() };
|
||||
if (data.title !== undefined) updateData.title = data.title;
|
||||
if (data.description !== undefined) updateData.description = data.description === null ? Prisma.JsonNull : data.description;
|
||||
if (data.videoUrl !== undefined) updateData.videoUrl = data.videoUrl;
|
||||
if (data.youtubeUrl !== undefined) updateData.youtubeUrl = data.youtubeUrl;
|
||||
if (data.estimatedDurationMinutes !== undefined) {
|
||||
const minutes = Math.max(0, Math.round(data.estimatedDurationMinutes));
|
||||
updateData.estimatedDuration = minutes * 60;
|
||||
}
|
||||
if (data.isPreview !== undefined) updateData.isFreePreview = data.isPreview;
|
||||
|
||||
const shouldUpdateMeta =
|
||||
data.description !== undefined || data.contentType !== undefined || data.materialUrl !== undefined;
|
||||
|
||||
if (shouldUpdateMeta) {
|
||||
const lesson = await db.lesson.findUnique({
|
||||
where: { id: lessonId },
|
||||
select: { description: true },
|
||||
});
|
||||
|
||||
const existingMeta = parseLessonDescriptionMeta(lesson?.description);
|
||||
updateData.description = buildLessonDescriptionMeta({
|
||||
text: data.description ?? existingMeta.text,
|
||||
contentType: data.contentType ?? existingMeta.contentType,
|
||||
materialUrl: data.materialUrl ?? existingMeta.materialUrl,
|
||||
});
|
||||
}
|
||||
|
||||
await db.lesson.update({
|
||||
where: { id: lessonId },
|
||||
@@ -211,7 +259,7 @@ export async function createModule(courseId: string) {
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/teacher/courses/${courseId}/edit`, "page");
|
||||
revalidatePath("/teacher/courses");
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Create Module Error:", error);
|
||||
@@ -219,6 +267,38 @@ export async function createModule(courseId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UPDATE MODULE TITLE
|
||||
*/
|
||||
export async function updateModuleTitle(moduleId: string, title: string) {
|
||||
const user = await requireTeacher();
|
||||
if (!user) return { success: false, error: "Unauthorized" };
|
||||
|
||||
const trimmed = title?.trim() || "";
|
||||
if (!trimmed) return { success: false, error: "El título no puede estar vacío" };
|
||||
|
||||
try {
|
||||
const moduleRow = await db.module.findFirst({
|
||||
where: { id: moduleId, course: { authorId: user.id } },
|
||||
select: { id: true, course: { select: { slug: true } } },
|
||||
});
|
||||
if (!moduleRow) return { success: false, error: "Módulo no encontrado" };
|
||||
|
||||
await db.module.update({
|
||||
where: { id: moduleId },
|
||||
data: { title: trimmed },
|
||||
});
|
||||
|
||||
revalidatePath("/teacher/courses");
|
||||
revalidatePath(`/teacher/courses/${moduleRow.course.slug}/edit`, "page");
|
||||
revalidatePath(`/teacher/courses/${moduleRow.course.slug}/edit`, "layout");
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Update Module Title Error:", error);
|
||||
return { success: false, error: "No se pudo actualizar el título" };
|
||||
}
|
||||
}
|
||||
|
||||
// 2. CREATE LESSON
|
||||
export async function createLesson(moduleId: string) {
|
||||
const user = await requireTeacher();
|
||||
@@ -238,6 +318,9 @@ export async function createLesson(moduleId: string) {
|
||||
data: {
|
||||
moduleId,
|
||||
title: "Nueva Lección",
|
||||
description: {
|
||||
contentType: "VIDEO",
|
||||
},
|
||||
orderIndex: newOrder,
|
||||
estimatedDuration: 0,
|
||||
version: 1,
|
||||
@@ -383,4 +466,4 @@ export async function reorderLessons(lessonId: string, direction: "up" | "down")
|
||||
|
||||
revalidatePath(`/teacher/courses/${currentLesson.module.course.slug}/edit`, "page");
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ import { requireTeacher } from "@/lib/auth/requireTeacher";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import TeacherEditCourseForm from "@/components/teacher/TeacherEditCourseForm";
|
||||
|
||||
// Always fetch fresh course data (no Full Route Cache) so save + router.refresh() shows updated level/status
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function CourseEditPage({ params }: { params: Promise<{ slug: string }> }) {
|
||||
const user = await requireTeacher();
|
||||
if (!user) redirect("/auth/login");
|
||||
@@ -37,5 +40,6 @@ export default async function CourseEditPage({ params }: { params: Promise<{ slu
|
||||
price: course.price.toNumber(),
|
||||
};
|
||||
|
||||
return <TeacherEditCourseForm course={courseData} />;
|
||||
// Key forces remount when course updates so uncontrolled inputs (level, status) show new defaultValues after save + router.refresh()
|
||||
return <TeacherEditCourseForm key={`${course.id}-${course.updatedAt.toISOString()}`} course={courseData} />;
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import { useRouter } from "next/navigation";
|
||||
import { toast } from "sonner";
|
||||
import { updateLesson } from "@/app/(protected)/teacher/actions";
|
||||
import VideoUpload from "@/components/teacher/VideoUpload"; // The component you created earlier
|
||||
import { getClientLocale } from "@/lib/i18n/clientLocale";
|
||||
import { getLessonContentTypeLabel, lessonContentTypes, type LessonContentType } from "@/lib/courses/lessonContent";
|
||||
|
||||
interface LessonEditorFormProps {
|
||||
lesson: {
|
||||
@@ -12,6 +14,11 @@ interface LessonEditorFormProps {
|
||||
title: string;
|
||||
description?: string | null;
|
||||
videoUrl?: string | null;
|
||||
youtubeUrl?: string | null;
|
||||
isFreePreview?: boolean;
|
||||
contentType: LessonContentType;
|
||||
materialUrl?: string | null;
|
||||
estimatedDurationMinutes: number;
|
||||
};
|
||||
courseSlug: string;
|
||||
}
|
||||
@@ -21,6 +28,29 @@ export function LessonEditorForm({ lesson, courseSlug }: LessonEditorFormProps)
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [title, setTitle] = useState(lesson.title);
|
||||
const [description, setDescription] = useState(lesson.description ?? "");
|
||||
const [youtubeUrl, setYoutubeUrl] = useState(lesson.youtubeUrl ?? "");
|
||||
const [isFreePreview, setIsFreePreview] = useState(lesson.isFreePreview ?? false);
|
||||
const [contentType, setContentType] = useState<LessonContentType>(lesson.contentType);
|
||||
const [materialUrl, setMaterialUrl] = useState(lesson.materialUrl ?? "");
|
||||
const [durationMinutes, setDurationMinutes] = useState(lesson.estimatedDurationMinutes);
|
||||
|
||||
const showSavedToast = () => {
|
||||
const isSpanish = getClientLocale() === "es";
|
||||
toast.success(isSpanish ? "Cambios guardados" : "Changes saved", {
|
||||
description: isSpanish
|
||||
? "Puedes seguir editando o volver al panel de cursos."
|
||||
: "You can keep editing or go back to the courses dashboard.",
|
||||
action: {
|
||||
label: isSpanish ? "Volver a cursos" : "Back to courses",
|
||||
onClick: () => router.push("/teacher"),
|
||||
},
|
||||
cancel: {
|
||||
label: isSpanish ? "Seguir editando" : "Keep editing",
|
||||
onClick: () => {},
|
||||
},
|
||||
duration: 7000,
|
||||
});
|
||||
};
|
||||
|
||||
// 1. Auto-save Video URL when upload finishes
|
||||
const handleVideoUploaded = async (url: string) => {
|
||||
@@ -36,12 +66,20 @@ export function LessonEditorForm({ lesson, courseSlug }: LessonEditorFormProps)
|
||||
}
|
||||
};
|
||||
|
||||
// 2. Save Text Changes (Title/Desc)
|
||||
// 2. Save Text Changes (Title/Desc/YouTube/Preview)
|
||||
const handleSave = async () => {
|
||||
setLoading(true);
|
||||
const res = await updateLesson(lesson.id, { title, description });
|
||||
const res = await updateLesson(lesson.id, {
|
||||
title,
|
||||
description,
|
||||
youtubeUrl: youtubeUrl.trim() || undefined,
|
||||
materialUrl: materialUrl.trim() || undefined,
|
||||
contentType,
|
||||
estimatedDurationMinutes: durationMinutes,
|
||||
isPreview: isFreePreview,
|
||||
});
|
||||
if (res.success) {
|
||||
toast.success("Cambios guardados");
|
||||
showSavedToast();
|
||||
router.refresh();
|
||||
} else {
|
||||
toast.error("Error al guardar cambios");
|
||||
@@ -68,6 +106,17 @@ export function LessonEditorForm({ lesson, courseSlug }: LessonEditorFormProps)
|
||||
onUploadComplete={handleVideoUploaded}
|
||||
/>
|
||||
</div>
|
||||
<div className="border-t border-slate-100 p-6">
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">YouTube URL</label>
|
||||
<input
|
||||
type="url"
|
||||
value={youtubeUrl}
|
||||
onChange={(e) => setYoutubeUrl(e.target.value)}
|
||||
placeholder="https://www.youtube.com/watch?v=..."
|
||||
className="w-full rounded-md border border-slate-300 px-3 py-2 outline-none focus:border-black"
|
||||
/>
|
||||
<p className="mt-1 text-xs text-slate-500">Si se proporciona, se usará en lugar del video subido en la lección.</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Text Content */}
|
||||
@@ -91,11 +140,64 @@ export function LessonEditorForm({ lesson, courseSlug }: LessonEditorFormProps)
|
||||
placeholder="Escribe aquí el contenido de la lección..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-1">URL de material (PDF / actividad / lectura)</label>
|
||||
<input
|
||||
type="url"
|
||||
value={materialUrl}
|
||||
onChange={(e) => setMaterialUrl(e.target.value)}
|
||||
placeholder="https://.../material.pdf"
|
||||
className="w-full rounded-md border border-slate-300 px-3 py-2 outline-none focus:border-black"
|
||||
/>
|
||||
<p className="mt-1 text-xs text-slate-500">Se usará en la vista del alumno como lectura o actividad descargable.</p>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
{/* RIGHT: Settings / Actions */}
|
||||
<div className="space-y-6">
|
||||
<div className="bg-white rounded-xl border border-slate-200 shadow-sm p-6">
|
||||
<h3 className="font-semibold text-slate-900 mb-4">Tipo de contenido</h3>
|
||||
<label className="block text-sm text-slate-700 mb-2">Formato de la lección</label>
|
||||
<select
|
||||
value={contentType}
|
||||
onChange={(e) => setContentType(e.target.value as LessonContentType)}
|
||||
className="w-full rounded-md border border-slate-300 px-3 py-2 outline-none focus:border-black"
|
||||
>
|
||||
{lessonContentTypes.map((type) => (
|
||||
<option key={type} value={type}>
|
||||
{getLessonContentTypeLabel(type)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<p className="mt-2 text-xs text-slate-500">
|
||||
Usa “Evaluación final” para marcar el examen obligatorio del curso.
|
||||
</p>
|
||||
|
||||
<label className="block text-sm text-slate-700 mb-1 mt-4">Duración estimada (minutos)</label>
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
value={durationMinutes}
|
||||
onChange={(e) => setDurationMinutes(Math.max(0, Number(e.target.value)))}
|
||||
className="w-full rounded-md border border-slate-300 px-3 py-2 outline-none focus:border-black"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="bg-white rounded-xl border border-slate-200 shadow-sm p-6">
|
||||
<h3 className="font-semibold text-slate-900 mb-4">Vista previa gratuita</h3>
|
||||
<label className="flex cursor-pointer items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={isFreePreview}
|
||||
onChange={(e) => setIsFreePreview(e.target.checked)}
|
||||
className="h-4 w-4 rounded border-slate-300"
|
||||
/>
|
||||
<span className="text-sm text-slate-700">Accesible sin inscripción (teaser)</span>
|
||||
</label>
|
||||
<p className="mt-2 text-xs text-slate-500">Los no inscritos podrán ver esta lección sin comprar el curso.</p>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border border-slate-200 shadow-sm p-6">
|
||||
<h3 className="font-semibold text-slate-900 mb-4">Acciones</h3>
|
||||
<button
|
||||
@@ -117,4 +219,4 @@ export function LessonEditorForm({ lesson, courseSlug }: LessonEditorFormProps)
|
||||
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { requireTeacher } from "@/lib/auth/requireTeacher";
|
||||
import { redirect, notFound } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import { LessonEditorForm } from "./LessonEditorForm";
|
||||
import { parseLessonDescriptionMeta } from "@/lib/courses/lessonContent";
|
||||
|
||||
function getText(value: unknown): string {
|
||||
if (!value) return "";
|
||||
@@ -42,6 +43,8 @@ export default async function LessonPage({ params }: PageProps) {
|
||||
if (!lesson) notFound();
|
||||
if (lesson.module.course.authorId !== user.id) redirect("/teacher");
|
||||
|
||||
const lessonMeta = parseLessonDescriptionMeta(lesson.description);
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl mx-auto p-6">
|
||||
{/* Breadcrumbs */}
|
||||
@@ -64,10 +67,13 @@ export default async function LessonPage({ params }: PageProps) {
|
||||
lesson={{
|
||||
...lesson,
|
||||
title: getText(lesson.title),
|
||||
description: getText(lesson.description),
|
||||
description: lessonMeta.text,
|
||||
contentType: lessonMeta.contentType,
|
||||
materialUrl: lessonMeta.materialUrl,
|
||||
estimatedDurationMinutes: Math.max(0, Math.ceil((lesson.estimatedDuration ?? 0) / 60)),
|
||||
}}
|
||||
courseSlug={slug}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export default async function TeacherDashboardPage() {
|
||||
|
||||
if (!user) {
|
||||
logger.info("User not authorized as teacher, redirecting");
|
||||
redirect("/login");
|
||||
redirect("/auth/login?role=teacher");
|
||||
}
|
||||
|
||||
|
||||
@@ -104,4 +104,4 @@ export default async function TeacherDashboardPage() {
|
||||
logger.error("Critical error in TeacherDashboardPage", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user