"use client"; import { useEffect, useState } from "react"; interface WebinarModalProps { webinar?: any; onClose: () => void; onSave: () => void; } export default function WebinarModal({ webinar, onClose, onSave }: WebinarModalProps) { const isEdit = !!webinar; const [form, setForm] = useState({ title: "", description: "", speaker: "", startAt: new Date(Date.now() + 86400000).toISOString().slice(0, 16), duration: 90, bannerUrl: "", category: "Basics", visibility: "PUBLIC", isActive: true, capacity: 25, priceCents: 0, }); const [learningPoints, setLearningPoints] = useState([""]); const [msg, setMsg] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { if (webinar) { setForm({ title: webinar.title || "", description: webinar.description || "", speaker: webinar.speaker || "", startAt: webinar.startAt ? new Date(webinar.startAt).toISOString().slice(0, 16) : "", duration: webinar.duration || 90, bannerUrl: webinar.bannerUrl || "", category: webinar.category || "Basics", visibility: webinar.visibility || "PUBLIC", isActive: webinar.isActive ?? true, capacity: webinar.capacity || 25, priceCents: webinar.priceCents || 0, }); // Load learning points if available if (webinar.learningPoints && Array.isArray(webinar.learningPoints) && webinar.learningPoints.length > 0) { setLearningPoints(webinar.learningPoints); } else { setLearningPoints([""]); } } }, [webinar]); function addLearningPoint() { setLearningPoints([...learningPoints, ""]); } function removeLearningPoint(index: number) { if (learningPoints.length > 1) { setLearningPoints(learningPoints.filter((_, i) => i !== index)); } } function updateLearningPoint(index: number, value: string) { const updated = [...learningPoints]; updated[index] = value; setLearningPoints(updated); } async function handleSubmit() { setMsg(null); setLoading(true); // Validate required fields if (!form.title.trim()) { setLoading(false); setMsg("❌ Title is required"); return; } if (!form.description.trim()) { setLoading(false); setMsg("❌ Description is required"); return; } if (!form.speaker.trim()) { setLoading(false); setMsg("❌ Speaker is required"); return; } if (form.duration < 15) { setLoading(false); setMsg("❌ Duration must be at least 15 minutes"); return; } if (form.capacity < 1) { setLoading(false); setMsg("❌ Capacity must be at least 1"); return; } // Filter out empty learning points const filteredPoints = learningPoints.filter(p => p.trim() !== ""); const payload = { ...form, duration: Number(form.duration), capacity: Number(form.capacity), priceCents: Number(form.priceCents), startAt: new Date(form.startAt).toISOString(), learningPoints: filteredPoints, }; const res = isEdit ? await fetch(`/api/webinars/${webinar.id}`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), }) : await fetch("/api/webinars", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload), }); const d = await res.json(); setLoading(false); if (!d.ok) { setMsg(d.message); return; } setMsg(isEdit ? "Webinar updated!" : "Webinar created!"); setTimeout(() => { onSave(); onClose(); }, 1000); } return (
e.stopPropagation()}>

{isEdit ? "✏️ Edit Webinar" : "🎓 Create New Webinar"}

{msg && (
{msg}
)}
setForm({ ...form, title: e.target.value })} />