"use client"; import Link from "next/link"; import { useEffect, useState, useTransition } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { createCourse, listTeacherCourses, } from "@/app/(protected)/dashboard/actions/teacher-courses"; interface CourseItem { id: string; title: string; description: string | null; archived: boolean; _count: { enrollments: number; assignments: number; }; } export default function TeacherCoursesPage() { const [courses, setCourses] = useState([]); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); const fetchCourses = async () => { try { const data = await listTeacherCourses(); setCourses( data.map((item) => ({ id: item.id, title: item.title, description: item.description, archived: item.archived, _count: item._count, })) ); } catch (e) { setError(e instanceof Error ? e.message : "加载课程失败"); } }; useEffect(() => { fetchCourses(); }, []); const handleCreateCourse = () => { setError(null); startTransition(async () => { try { await createCourse({ title, description }); setTitle(""); setDescription(""); await fetchCourses(); } catch (e) { setError(e instanceof Error ? e.message : "创建课程失败"); } }); }; return (
创建课程 创建课程后可添加学生并发布作业 setTitle(e.target.value)} placeholder="课程标题" />