import { useState, useEffect } from "react" import type { NextPage } from "next" import Head from "next/head" import Link from "next/link" interface User { id: number email: string role: string is_active: boolean created_at: string last_login: string | null sources_moderated: number } const UsersManagement: NextPage = () => { const [users, setUsers] = useState([]) const [loading, setLoading] = useState(true) const [showAddForm, setShowAddForm] = useState(false) const [newUser, setNewUser] = useState({ email: '', password: '', role: 'moderator' }) useEffect(() => { fetchUsers() }, []) const fetchUsers = async () => { try { const response = await fetch('/api/admin/users') const data = await response.json() setUsers(data.users || []) } catch (error) { console.error('Error fetching users:', error) } setLoading(false) } const handleAddUser = async (e: React.FormEvent) => { e.preventDefault() if (!newUser.email || !newUser.password) return try { const response = await fetch('/api/admin/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(newUser) }) if (response.ok) { setNewUser({ email: '', password: '', role: 'moderator' }) setShowAddForm(false) fetchUsers() } else { const error = await response.json() alert('Error: ' + error.error) } } catch (error) { alert('Failed to add user') } } if (loading) return
Loading...
return (
Users Management - Infohliadka
← Back to Admin

Users Management

{showAddForm && (

Add New User

setNewUser({...newUser, email: e.target.value})} required style={{ width: '200px', padding: '5px', marginRight: '10px' }} /> setNewUser({...newUser, password: e.target.value})} required style={{ width: '200px', padding: '5px', marginRight: '10px' }} />
)} {users.map((user) => ( ))}
Email Role Status Sources Moderated Created Last Login
{user.email} {user.role} {user.is_active ? 'Active' : 'Inactive'} {user.sources_moderated} {new Date(user.created_at).toLocaleDateString()} {user.last_login ? new Date(user.last_login).toLocaleDateString() : 'Never'}
{users.length === 0 && (

No users found.

)}
) } export default UsersManagement