"use client"; import Link from "next/link"; import { FormEvent, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { getTeacherCourseBySlug, teacherCoursesUpdatedEventName, updateTeacherCourse } from "@/lib/data/teacherCourses"; import type { Course, CourseLevel } from "@/types/course"; const levels: CourseLevel[] = ["Beginner", "Intermediate", "Advanced"]; type TeacherEditCourseFormProps = { slug: string; }; export default function TeacherEditCourseForm({ slug }: TeacherEditCourseFormProps) { const router = useRouter(); const [course, setCourse] = useState(null); const [title, setTitle] = useState(""); const [level, setLevel] = useState("Beginner"); const [summary, setSummary] = useState(""); const [instructor, setInstructor] = useState(""); const [weeks, setWeeks] = useState(4); const [rating, setRating] = useState(5); const [saved, setSaved] = useState(false); useEffect(() => { const load = () => { const found = getTeacherCourseBySlug(slug) ?? null; setCourse(found); if (!found) return; setTitle(found.title); setLevel(found.level); setSummary(found.summary); setInstructor(found.instructor); setWeeks(found.weeks); setRating(found.rating); }; load(); window.addEventListener(teacherCoursesUpdatedEventName, load); return () => window.removeEventListener(teacherCoursesUpdatedEventName, load); }, [slug]); const submit = (event: FormEvent) => { event.preventDefault(); if (!course) return; updateTeacherCourse(course.slug, { title: title.trim(), level, summary: summary.trim(), instructor: instructor.trim(), weeks: Math.max(1, weeks), rating: Math.min(5, Math.max(0, rating)), }); setSaved(true); window.setTimeout(() => setSaved(false), 1200); }; if (!course) { return (

Teacher course not found

This editor only works for courses created in the teacher area.

Back to dashboard
); } return (

Edit Course