1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
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<User | null>(null);
const [isSearching, setIsSearching] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [showConfirmation, setShowConfirmation] = useState(false);
const [error, setError] = useState<string | null>(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 (
<>
<p className="text-slate-300 leading-relaxed mb-6 p-4 bg-slate-800/50 rounded-md border-l-4 border-red-500">
<strong className="text-red-400">Warning:</strong> This action is irreversible. Deleting a user will permanently remove their account and all associated data including scores and statistics.
</p>
{error && (
<div className="mb-6 p-4 bg-red-900/30 border border-red-700 rounded-md">
<p className="text-red-400">{error}</p>
</div>
)}
<div className="space-y-6">
{/* Search User Section */}
<div className="space-y-4">
<div>
<label htmlFor="userId" className="block text-sm font-medium text-slate-300 mb-2">
User ID
</label>
<div className="flex gap-3">
<input
type="number"
id="userId"
value={userId}
onChange={(e) => 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}
/>
<button
onClick={handleSearchUser}
disabled={isSearching || !userId.trim()}
className="bg-violet-600 hover:bg-violet-700 disabled:bg-violet-800 disabled:cursor-not-allowed text-white font-medium px-4 py-2 rounded-md transition-colors"
>
{isSearching ? 'Searching...' : 'Search'}
</button>
</div>
</div>
</div>
{/* User Details Section */}
{userToDelete && !showConfirmation && (
<div className="p-4 bg-slate-800 border border-slate-600 rounded-md">
<h3 className="text-lg font-semibold text-white mb-3">User Found</h3>
<div className="space-y-2 text-sm">
<p className="text-slate-300">
<span className="text-slate-400">ID:</span> {userToDelete.id}
</p>
<p className="text-slate-300">
<span className="text-slate-400">Username:</span> {userToDelete.username}
</p>
<p className="text-slate-300">
<span className="text-slate-400">Admin Status:</span>{' '}
<span className={userToDelete.isAdmin ? 'text-yellow-400' : 'text-green-400'}>
{userToDelete.isAdmin ? 'Admin' : 'Regular User'}
</span>
</p>
</div>
<div className="flex gap-3 mt-4">
<button
onClick={() => setShowConfirmation(true)}
className="bg-red-600 hover:bg-red-700 text-white font-medium px-4 py-2 rounded-md transition-colors"
>
Delete User
</button>
<button
onClick={resetForm}
className="bg-slate-600 hover:bg-slate-700 text-white font-medium px-4 py-2 rounded-md transition-colors"
>
Cancel
</button>
</div>
</div>
)}
{/* Confirmation Section */}
{showConfirmation && userToDelete && (
<div className="p-4 bg-red-900/20 border border-red-600 rounded-md">
<h3 className="text-lg font-semibold text-red-400 mb-3">Confirm Deletion</h3>
<p className="text-slate-300 mb-4">
Are you absolutely sure you want to delete the user <strong className="text-white">"{userToDelete.username}"</strong>?
</p>
<p className="text-sm text-red-300 mb-4">
This action cannot be undone. All user data, scores, and statistics will be permanently deleted.
</p>
<div className="flex gap-3">
<button
onClick={handleDeleteUser}
disabled={isDeleting}
className="bg-red-600 hover:bg-red-700 disabled:bg-red-800 disabled:cursor-not-allowed text-white font-medium px-4 py-2 rounded-md transition-colors"
>
{isDeleting ? 'Deleting...' : 'Yes, Delete User'}
</button>
<button
onClick={() => setShowConfirmation(false)}
disabled={isDeleting}
className="bg-slate-600 hover:bg-slate-700 disabled:bg-slate-700 disabled:cursor-not-allowed text-white font-medium px-4 py-2 rounded-md transition-colors"
>
Cancel
</button>
</div>
</div>
)}
</div>
</>
);
};
export default UserDeletion;
|