'use client'; import React, { useEffect, useState } from 'react'; import { SidebarProvider, SidebarInset } from '@/components/ui/sidebar'; import { AppSidebar } from '@/components/app-sidebar'; import { SiteHeader } from '@/components/site-header'; import { useRouter } from 'next/navigation'; import { toast } from 'sonner'; import { Card, CardContent, CardHeader, CardTitle, } from '@/components/ui/card'; import { Field, FieldGroup, FieldLabel, } from '@/components/ui/field'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog'; import { Trash2Icon } from 'lucide-react'; type Course = { id: string; title: string; code?: string }; function CoursesUI() { const router = useRouter(); const [courses, setCourses] = useState([]); const [loading, setLoading] = useState(false); const [courseTitle, setCourseTitle] = useState(''); const [courseCode, setCourseCode] = useState(''); const [deletingId, setDeletingId] = useState(null); useEffect(() => { fetchCourses(); }, []); async function fetchCourses() { try { const res = await fetch('/api/admin/meta'); if (res.ok) { const json = await res.json(); setCourses(json.courses || []); } } catch (err) { console.error('Failed to fetch courses', err); toast.error('Failed to fetch courses'); } } async function createCourse(e: React.FormEvent) { e.preventDefault(); setLoading(true); try { const res = await fetch('/api/admin/create-course', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: courseTitle, code: courseCode }), }); if (res.ok) { toast.success('Course created'); setCourseTitle(''); setCourseCode(''); await fetchCourses(); router.refresh(); } else { const txt = await res.text(); toast.error('Failed to create course: ' + txt); } } catch (err: any) { toast.error('Error: ' + String(err.message ?? err)); } finally { setLoading(false); } } async function deleteCourse(courseId: string) { setDeletingId(courseId); try { const res = await fetch(`/api/admin/delete-course`, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ courseId }), }); if (res.ok) { toast.success('Course deleted'); await fetchCourses(); router.refresh(); } else { const txt = await res.text(); toast.error('Failed to delete course: ' + txt); } } catch (err: any) { toast.error('Error: ' + String(err.message ?? err)); } finally { setDeletingId(null); } } return (
Create Course
Course title setCourseTitle(e.target.value)} placeholder="3D ANIMATION 300" /> Course code setCourseCode(e.target.value)} placeholder="3D100" />
Courses {courses.length === 0 ? (
No courses yet
) : (
Title Code Actions {courses.map((course) => ( {course.title} {course.code || '—'} Delete Course Are you sure? This will delete the course and all associated playlists and videos.
Cancel deleteCourse(course.id)} > Delete
))}
)}
); } export default function CoursesAdminClient() { return (
); }