import { useState, FormEvent } from 'react'; import { useGetUsers, useUpdateUser } from '../generated/anthoLumeAPIV1'; import { Plus, Trash2 } from 'lucide-react'; export default function AdminUsersPage() { const { data: usersData, isLoading, refetch } = useGetUsers({}); const updateUser = useUpdateUser(); const [showAddForm, setShowAddForm] = useState(false); const [newUsername, setNewUsername] = useState(''); const [newPassword, setNewPassword] = useState(''); const [newIsAdmin, setNewIsAdmin] = useState(false); const users = usersData?.data?.users || []; const handleCreateUser = (e: FormEvent) => { e.preventDefault(); if (!newUsername || !newPassword) return; updateUser.mutate( { data: { operation: 'CREATE', user: newUsername, password: newPassword, is_admin: newIsAdmin, }, }, { onSuccess: () => { setShowAddForm(false); setNewUsername(''); setNewPassword(''); setNewIsAdmin(false); refetch(); }, onError: (error: any) => { alert('Failed to create user: ' + error.message); }, } ); }; const handleDeleteUser = (userId: string) => { updateUser.mutate( { data: { operation: 'DELETE', user: userId, }, }, { onSuccess: () => { refetch(); }, onError: (error: any) => { alert('Failed to delete user: ' + error.message); }, } ); }; const handleUpdatePassword = (userId: string, password: string) => { if (!password) return; updateUser.mutate( { data: { operation: 'UPDATE', user: userId, password: password, }, }, { onSuccess: () => { refetch(); }, onError: (error: any) => { alert('Failed to update password: ' + error.message); }, } ); }; const handleToggleAdmin = (userId: string, isAdmin: boolean) => { updateUser.mutate( { data: { operation: 'UPDATE', user: userId, is_admin: isAdmin, }, }, { onSuccess: () => { refetch(); }, onError: (error: any) => { alert('Failed to update admin status: ' + error.message); }, } ); }; if (isLoading) { return
Loading...
; } return (
{/* Add User Form */} {showAddForm && (
setNewUsername(e.target.value)} placeholder="Username" className="p-2 bg-gray-300 text-black dark:bg-gray-700 dark:text-white" /> setNewPassword(e.target.value)} placeholder="Password" className="p-2 bg-gray-300 text-black dark:bg-gray-700 dark:text-white" />
setNewIsAdmin(e.target.checked)} />
)} {/* Users Table */}
{users.length === 0 ? ( ) : ( users.map((user) => ( {/* Delete Button */} {/* User ID */} {/* Password Reset */} {/* Admin Toggle */} {/* Created Date */} )) )}
User Password Permissions Created
No Results

{user.id}

{user.created_at}

); }