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 AdminPage() { |
| 5 | const [posts, setPosts] = useState([]); |
| 6 | |
| 7 | useEffect(() => { |
| 8 | // TODO: 替换成你后端真正的接口 |
| 9 | fetch('/apostlist') |
| 10 | .then(res => res.json()) |
| 11 | .then(data => setPosts(data)) |
| 12 | .catch(console.error); |
| 13 | }, []); |
| 14 | |
| 15 | const handleAction = (id, action) => { |
| 16 | // action: 'approve' | 'ban' | ... |
| 17 | fetch(`/api/posts/${id}/${action}`, { method: 'POST' }) |
| 18 | .then(res => { |
| 19 | if (res.ok) { |
| 20 | // 简单地把该条移除或根据返回值更新状态 |
| 21 | setPosts(ps => ps.filter(p => p.id !== id)); |
| 22 | } |
| 23 | }) |
| 24 | .catch(console.error); |
| 25 | }; |
| 26 | |
| 27 | return ( |
| 28 | <div className="admin-container"> |
| 29 | <h1 className="admin-title">小红书 · 管理员审核</h1> |
| 30 | <table className="admin-table"> |
| 31 | <thead> |
| 32 | <tr> |
| 33 | <th>标题</th> |
| 34 | <th>发布时间</th> |
| 35 | <th>内容摘要</th> |
| 36 | <th>状态</th> |
| 37 | <th>操作</th> |
| 38 | </tr> |
| 39 | </thead> |
| 40 | <tbody> |
| 41 | {posts.map(p => { |
| 42 | const brief = |
| 43 | p.content.length > 80 |
| 44 | ? p.content.slice(0, 80) + '...' |
| 45 | : p.content; |
| 46 | return ( |
| 47 | <tr key={p.id}> |
| 48 | <td>{p.title}</td> |
| 49 | <td>{new Date(p.createdAt).toLocaleString()}</td> |
| 50 | <td>{brief}</td> |
| 51 | <td className={`status ${p.status}`}>{p.status}</td> |
| 52 | <td> |
| 53 | <button |
| 54 | className="btn btn-approve" |
| 55 | onClick={() => handleAction(p.id, 'approve')} |
| 56 | > |
| 57 | 审核 |
| 58 | </button> |
| 59 | <button |
| 60 | className="btn btn-ban" |
| 61 | onClick={() => handleAction(p.id, 'ban')} |
| 62 | > |
| 63 | 封禁 |
| 64 | </button> |
| 65 | </td> |
| 66 | </tr> |
| 67 | ); |
| 68 | })} |
| 69 | </tbody> |
| 70 | </table> |
| 71 | </div> |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | export default AdminPage; |