"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; interface WebinarRegistration { id: string; status: string; webinar: { id: string; title: string; startAt: string; }; } interface User { id: string; email: string; firstName: string; lastName: string; role: string; isActive: boolean; emailVerified: boolean; createdAt: string; gender?: string; dob?: string; address?: string; image?: string; _count: { webinarRegistrations: number; }; registeredWebinars: WebinarRegistration[]; } interface PaginationInfo { page: number; pageSize: number; total: number; pages: number; hasMore: boolean; } export default function AdminUsersPage() { const [users, setUsers] = useState([]); const [pagination, setPagination] = useState({ page: 1, pageSize: 10, total: 0, pages: 1, hasMore: false, }); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(""); const [message, setMessage] = useState(""); const [updatingUserId, setUpdatingUserId] = useState(null); useEffect(() => { fetchUsers(1); }, [searchQuery]); const fetchUsers = async (page: number) => { setLoading(true); try { const params = new URLSearchParams({ page: String(page) }); if (searchQuery) params.append("search", searchQuery); const response = await fetch(`/api/admin/users?${params}`); if (response.ok) { const data = await response.json(); if (data.ok) { setUsers(data.users); setPagination(data.pagination); } } } catch (error) { console.error("Failed to fetch users:", error); setMessage("❌ Failed to load users"); } finally { setLoading(false); } }; const handleBlockUnblock = async (userId: string, currentlyActive: boolean) => { setUpdatingUserId(userId); try { const response = await fetch("/api/admin/users", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userId, isActive: !currentlyActive, }), }); const data = await response.json(); if (data.ok) { setMessage(`✅ ${data.message}`); fetchUsers(pagination.page); setTimeout(() => setMessage(""), 3000); } else { setMessage(`❌ ${data.message}`); } } catch (error) { setMessage("❌ Failed to update user"); } finally { setUpdatingUserId(null); } }; const handleRoleChange = async (userId: string, newRole: "USER" | "ADMIN") => { setUpdatingUserId(userId); try { const response = await fetch("/api/admin/users", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userId, role: newRole, }), }); const data = await response.json(); if (data.ok) { setMessage(`✅ ${data.message}`); fetchUsers(pagination.page); setTimeout(() => setMessage(""), 3000); } else { setMessage(`❌ ${data.message}`); } } catch (error) { setMessage("❌ Failed to update user role"); } finally { setUpdatingUserId(null); } }; return (
👥 User Management

Users

Manage user accounts, roles, and permissions ({pagination.total} total)

{message && (
{message}
)}
setSearchQuery(e.target.value)} />
{loading ? (

Loading users...

) : users.length === 0 ? (

No users found

) : ( <>
{users.map((user) => (
{user.image ? ( {user.firstName} ) : (
{user.firstName[0]}{user.lastName[0]}
)}

{user.firstName} {user.lastName}

{user.email}

{!user.isActive && ( 🚫 BLOCKED )} {user.emailVerified && ( ✅ Verified )} {user.role === "ADMIN" && ( 👑 Admin )}

Joined: {new Date(user.createdAt).toLocaleDateString()}

Registrations: {user._count.webinarRegistrations}

{/* Registered Webinars */} {user.registeredWebinars.length > 0 && (

📚 Recent Webinars ({user.registeredWebinars.length})

{user.registeredWebinars.map((reg) => (
{reg.webinar.title} ({new Date(reg.webinar.startAt).toLocaleDateString()}) {reg.status}
))}
)} {/* Actions */}
))}
{/* Pagination */} {pagination.pages > 1 && (
{Array.from({ length: pagination.pages }, (_, i) => i + 1).map((page) => ( ))}
)} )}
); }