"use client"; import { useState, useTransition } from "react"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; import { updateCourse, deleteCourse } from "@/app/admin/courses/actions"; interface Course { id: string; title: string; slug: string; description: string | null; coverImage: string | null; published: boolean; allowAudio: boolean; categoryId: string | null; } interface Category { id: string; title: string; } export function CourseEditForm({ course, categories = [] }: { course: Course; categories?: Category[] }) { const [published, setPublished] = useState(course.published); const [allowAudio, setAllowAudio] = useState(course.allowAudio); const [coverImage, setCoverImage] = useState(course.coverImage ?? ""); const [categoryId, setCategoryId] = useState(course.categoryId ?? ""); const [uploading, setUploading] = useState(false); const [pending, startTransition] = useTransition(); async function handleImageUpload(e: React.ChangeEvent) { const file = e.target.files?.[0]; if (!file) return; setUploading(true); const fd = new FormData(); fd.append("file", file); const res = await fetch("/api/admin/upload", { method: "POST", body: fd }); const data = await res.json(); if (data.url) setCoverImage(data.url); setUploading(false); } function handleSubmit(e: React.FormEvent) { e.preventDefault(); const fd = new FormData(e.currentTarget); fd.set("published", String(published)); fd.set("allowAudio", String(allowAudio)); fd.set("coverImage", coverImage); fd.set("categoryId", categoryId); startTransition(() => updateCourse(course.id, fd)); } function handleDelete() { if (!confirm("Удалить курс? Это действие нельзя отменить.")) return; startTransition(() => deleteCourse(course.id)); } return (