Pending course, rest ready for launch
This commit is contained in:
@@ -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