import React, { useState, useEffect } from 'react'; import { X, User, Trash2, Shield, ShieldAlert, Lock, Unlock, Loader2, UserPlus } from 'lucide-react'; interface UserManagementModalProps { isOpen: boolean; onClose: () => void; } export const UserManagementModal: React.FC = ({ isOpen, onClose }) => { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const fetchUsers = async () => { setLoading(true); try { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/users`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (!response.ok) throw new Error('Không thể tải danh sách người dùng'); const data = await response.json(); setUsers(data); } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; useEffect(() => { if (isOpen) fetchUsers(); }, [isOpen]); const handleToggleBlock = async (id: string) => { try { const API_BASE = `http://${window.location.hostname}:3001`; await fetch(`${API_BASE}/api/v1/users/block/${id}`, { method: 'POST', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); fetchUsers(); } catch (err) { alert('Lỗi khi thay đổi trạng thái block'); } }; const handleDelete = async (id: string) => { if (!confirm('Bạn có chắc chắn muốn xóa người dùng này?')) return; try { const API_BASE = `http://${window.location.hostname}:3001`; const res = await fetch(`${API_BASE}/api/v1/users/${id}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (!res.ok) { const data = await res.json(); throw new Error(data.message); } fetchUsers(); } catch (err: any) { alert(err.message); } }; if (!isOpen) return null; return (

Quản lý người dùng

Quản trị viên có quyền thêm, sửa, xóa hoặc khóa tài khoản.

{loading ? (
) : error ? (
{error}
) : ( {users.map(u => ( ))}
Người dùng Vai trò Trạng thái Thao tác
{u.name?.charAt(0) || }
{u.name || 'N/A'}
{u.email}
{u.isAdmin ? ( ADMIN ) : ( USER )} {u.isBlocked ? ( Đã khóa ) : ( Đang hoạt động )}
)}
); };