import { useState } from 'react'; interface User { id: number; username: string; isAdmin: boolean; } interface UserDeletionProps { onUserDeleted?: () => void; } const UserDeletion = ({ onUserDeleted }: UserDeletionProps) => { const [userId, setUserId] = useState(''); const [userToDelete, setUserToDelete] = useState(null); const [isSearching, setIsSearching] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [showConfirmation, setShowConfirmation] = useState(false); const [error, setError] = useState(null); const handleSearchUser = async () => { if (!userId.trim()) { setError('Please enter a user ID'); return; } const id = parseInt(userId); if (isNaN(id) || id <= 0) { setError('Please enter a valid user ID'); return; } setIsSearching(true); setError(null); setUserToDelete(null); try { const response = await fetch(`${import.meta.env.VITE_API_URL}/me?userId=${id}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (!response.ok) { if (response.status === 404) { throw new Error('User not found'); } const error = await response.json(); throw new Error(error.error || 'Failed to find user'); } const data = await response.json(); setUserToDelete(data.user); } catch (error) { console.error('Failed to search user:', error); setError(error instanceof Error ? error.message : 'Failed to search user'); } finally { setIsSearching(false); } }; const handleDeleteUser = async () => { if (!userToDelete) return; setIsDeleting(true); setError(null); try { const response = await fetch(`${import.meta.env.VITE_API_URL}/admin/user/${userToDelete.id}`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to delete user'); } alert(`User "${userToDelete.username}" has been successfully deleted.`); // Reset form setUserId(''); setUserToDelete(null); setShowConfirmation(false); // Call callback if provided onUserDeleted?.(); } catch (error) { console.error('Failed to delete user:', error); setError(error instanceof Error ? error.message : 'Failed to delete user'); } finally { setIsDeleting(false); } }; const resetForm = () => { setUserId(''); setUserToDelete(null); setShowConfirmation(false); setError(null); }; return ( <>

Warning: This action is irreversible. Deleting a user will permanently remove their account and all associated data including scores and statistics.

{error && (

{error}

)}
{/* Search User Section */}
setUserId(e.target.value)} className="flex-1 px-3 py-2 bg-slate-800 border border-slate-600 rounded-md text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500 focus:border-transparent" placeholder="Enter user ID to search" min="1" disabled={isSearching} />
{/* User Details Section */} {userToDelete && !showConfirmation && (

User Found

ID: {userToDelete.id}

Username: {userToDelete.username}

Admin Status:{' '} {userToDelete.isAdmin ? 'Admin' : 'Regular User'}

)} {/* Confirmation Section */} {showConfirmation && userToDelete && (

Confirm Deletion

Are you absolutely sure you want to delete the user "{userToDelete.username}"?

This action cannot be undone. All user data, scores, and statistics will be permanently deleted.

)}
); }; export default UserDeletion;