"use client"; import { useState, useEffect } from "react"; import WebinarModal from "../../../components/admin/WebinarModal"; interface Webinar { id: string; title: string; description: string; speaker: string; startAt: string; duration: number; priceCents: number; visibility: string; capacity: number; isActive: boolean; _count?: { registrations: number }; } interface PaginationInfo { page: number; pageSize: number; total: number; pages: number; hasMore: boolean; } export default function AdminWebinarsPage() { const [webinars, setWebinars] = useState([]); const [pagination, setPagination] = useState({ page: 1, pageSize: 10, total: 0, pages: 1, hasMore: false, }); const [loading, setLoading] = useState(true); const [isModalOpen, setIsModalOpen] = useState(false); const [editingWebinar, setEditingWebinar] = useState(null); useEffect(() => { fetchWebinars(1); }, []); const fetchWebinars = async (page: number) => { setLoading(true); try { // API uses 0-indexed pages (page 0 is first page) const apiPage = page - 1; const response = await fetch(`/api/webinars?page=${apiPage}&limit=100`); if (response.ok) { const data = await response.json(); setWebinars(data.webinars || []); setPagination({ page, pageSize: data.limit || 10, total: data.total || 0, pages: Math.ceil((data.total || 0) / (data.limit || 10)), hasMore: page < Math.ceil((data.total || 0) / (data.limit || 10)), }); } else { console.error("Failed to fetch webinars:", response.status); setWebinars([]); } } catch (error) { console.error("Failed to fetch webinars:", error); setWebinars([]); } finally { setLoading(false); } }; const handleCreateWebinar = () => { setEditingWebinar(null); setIsModalOpen(true); }; const handleEditWebinar = (webinar: Webinar) => { setEditingWebinar(webinar); setIsModalOpen(true); }; const handleDeleteWebinar = async (id: string) => { if (!confirm("Are you sure you want to delete this webinar?")) return; try { const response = await fetch(`/api/webinars/${id}`, { method: "DELETE", }); if (response.ok) { fetchWebinars(pagination.page); } } catch (error) { console.error("Failed to delete webinar:", error); } }; return (
🎬 Webinars

Webinars Management

Create and manage your webinars ({pagination.total} total)

{loading ? (

Loading webinars...

) : ( <>
{webinars.length === 0 ? (
πŸ“Ή

No webinars yet

Create your first webinar to get started

) : ( webinars.map((webinar) => (

{webinar.title}

{webinar.isActive ? "βœ… Active" : "⏸️ Inactive"} {webinar.visibility === "PUBLIC" ? "πŸ”“ Public" : "πŸ”’ Private"}

{webinar.description}

Speaker:

{webinar.speaker}

Date:

{new Date(webinar.startAt).toLocaleDateString()}

Price:

{webinar.priceCents === 0 ? "FREE" : `$${(webinar.priceCents / 100).toFixed(2)}`}

Capacity:

{webinar.capacity} seats

)) )}
{/* Pagination */} {pagination.pages > 1 && (
{Array.from({ length: pagination.pages }, (_, i) => i + 1).map((page) => ( ))}
)} )} {isModalOpen && ( { setIsModalOpen(false); setEditingWebinar(null); }} onSave={() => { setIsModalOpen(false); setEditingWebinar(null); fetchWebinars(pagination.page); }} /> )}
); }