TRM-coding | 130f05c | 2025-06-15 16:05:28 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import './Admin.css'; |
| 3 | |
| 4 | function UserManagement() { |
| 5 | const [users, setUsers] = useState([]); |
| 6 | |
| 7 | useEffect(() => { |
| 8 | fetch('/api/users') |
| 9 | .then(res => res.json()) |
| 10 | .then(data => setUsers(data)) |
| 11 | .catch(console.error); |
| 12 | }, []); |
| 13 | |
| 14 | const handleUserAction = (id, action) => { |
| 15 | fetch(`/api/users/${id}/${action}`, { method: 'POST' }) |
| 16 | .then(res => res.ok && setUsers(us => us.filter(u => u.id !== id))) |
| 17 | .catch(console.error); |
| 18 | }; |
| 19 | |
| 20 | return ( |
| 21 | <div className="admin-container"> |
| 22 | <h2>用户管理</h2> |
| 23 | <table className="admin-table"> |
| 24 | <thead> |
| 25 | <tr><th>用户名</th><th>角色</th><th>操作</th></tr> |
| 26 | </thead> |
| 27 | <tbody> |
| 28 | {users.map(u => ( |
| 29 | <tr key={u.id}> |
| 30 | <td>{u.username}</td> |
| 31 | <td>{u.role}</td> |
| 32 | <td> |
| 33 | <button onClick={() => handleUserAction(u.id, 'ban')}>封禁</button> |
| 34 | <button onClick={() => handleUserAction(u.id, 'promote')}>提升权限</button> |
| 35 | </td> |
| 36 | </tr> |
| 37 | ))} |
| 38 | </tbody> |
| 39 | </table> |
| 40 | </div> |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | export default UserManagement; |