22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from "react"; |
wht | 6a1b678 | 2025-06-06 19:14:59 +0800 | [diff] [blame] | 2 | import { useNavigate } from "react-router-dom"; |
22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 3 | import { API_BASE_URL } from "./config"; |
wht | 6a1b678 | 2025-06-06 19:14:59 +0800 | [diff] [blame] | 4 | |
| 5 | // 示例数据 |
| 6 | const posts = [ |
| 7 | { |
| 8 | post_id: "1", |
| 9 | title: "欢迎来到G10论坛", |
| 10 | content: "这里是G10 PT站的官方论坛,欢迎大家发帖交流!", |
| 11 | author_id: "u1", |
| 12 | author_name: "Alice", |
| 13 | created_at: "2024-06-01 12:00", |
| 14 | reply_count: 3, |
| 15 | view_count: 120, |
| 16 | }, |
| 17 | { |
| 18 | post_id: "2", |
| 19 | title: "经典电影合集", |
| 20 | content: "90年代经典电影大家有什么推荐吗?", |
| 21 | author_id: "u2", |
| 22 | author_name: "Bob", |
| 23 | created_at: "2024-06-02 09:30", |
| 24 | reply_count: 1, |
| 25 | view_count: 45, |
| 26 | }, |
| 27 | ]; |
| 28 | |
| 29 | export default function ForumPage() { |
| 30 | const navigate = useNavigate(); |
22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 31 | const [posts, setPosts] = useState([]); |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame^] | 32 | const [searchQuery, setSearchQuery] = useState(''); |
22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 33 | |
| 34 | useEffect(() => { |
| 35 | // get userId from cookie |
| 36 | const match = document.cookie.match('(^|;)\\s*userId=([^;]+)'); |
| 37 | const userId = match ? match[2] : null; |
| 38 | console.log("User ID from cookie:", userId); |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame^] | 39 | // if (!userId) return; |
| 40 | console.log("Fetching forum posts..."); |
22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 41 | fetch(`${API_BASE_URL}/api/forum`) |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame^] | 42 | .then(res => { |
| 43 | // console.log("fetch raw response:", res); |
| 44 | return res.json(); |
| 45 | }) |
| 46 | .then(data => { |
| 47 | // console.log("fetch forum data:", data); |
| 48 | const formattedPosts = data.map(post => ({ |
| 49 | post_id: post.postid, |
| 50 | title: post.posttitle, |
| 51 | content: post.postcontent, |
| 52 | author_id: post.postuserid, |
| 53 | author_name: post.author?.username || '', |
| 54 | created_at: new Date(post.posttime).toLocaleString(), |
| 55 | reply_count: post.replytime, |
| 56 | view_count: post.readtime, |
| 57 | })); |
| 58 | setPosts(formattedPosts); |
| 59 | }) |
22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 60 | .catch(() => setPosts([])); |
| 61 | }, []); |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame^] | 62 | |
| 63 | // Handler to perform search based on input value |
| 64 | const handleSearch = () => { |
| 65 | fetch(`${API_BASE_URL}/api/search-posts?keyword=${encodeURIComponent(searchQuery)}`) |
| 66 | .then(res => res.json()) |
| 67 | .then(data => { |
| 68 | const formattedPosts = data.map(post => ({ |
| 69 | post_id: post.postid, |
| 70 | title: post.posttitle, |
| 71 | content: post.postcontent, |
| 72 | author_id: post.postuserid, |
| 73 | author_name: post.author?.username || '', |
| 74 | created_at: new Date(post.posttime).toLocaleString(), |
| 75 | reply_count: post.replytime, |
| 76 | view_count: post.readtime, |
| 77 | })); |
| 78 | setPosts(formattedPosts); |
| 79 | }) |
| 80 | .catch(() => setPosts([])); |
| 81 | }; |
22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame] | 82 | |
wht | 6a1b678 | 2025-06-06 19:14:59 +0800 | [diff] [blame] | 83 | return ( |
| 84 | <div style={{ maxWidth: 700, margin: "40px auto" }}> |
| 85 | <div style={{ display: "flex", alignItems: "center", marginBottom: 24 }}> |
| 86 | <h2 style={{ color: "#1a237e", margin: 0, marginRight: 24 }}>G10 论坛</h2> |
| 87 | <input |
| 88 | type="text" |
| 89 | placeholder="搜索帖子内容" |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame^] | 90 | value={searchQuery} |
| 91 | onChange={e => setSearchQuery(e.target.value)} |
wht | 6a1b678 | 2025-06-06 19:14:59 +0800 | [diff] [blame] | 92 | style={{ |
| 93 | flex: 1, |
| 94 | fontSize: 16, |
| 95 | padding: "8px 14px", |
| 96 | border: "1px solid #bfcfff", |
| 97 | borderRadius: 8, |
| 98 | outline: "none", |
| 99 | minWidth: 0, |
| 100 | }} |
| 101 | /> |
| 102 | <button |
| 103 | style={{ |
| 104 | marginLeft: 8, |
| 105 | fontSize: 16, |
| 106 | padding: "8px 18px", |
| 107 | border: "1px solid #bfcfff", |
| 108 | background: "#e0e7ff", |
| 109 | borderRadius: 8, |
| 110 | cursor: "pointer", |
| 111 | }} |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame^] | 112 | onClick={handleSearch} |
wht | 6a1b678 | 2025-06-06 19:14:59 +0800 | [diff] [blame] | 113 | > |
| 114 | 🔍 |
| 115 | </button> |
| 116 | </div> |
| 117 | {posts.map(post => ( |
| 118 | <div |
| 119 | key={post.post_id} |
| 120 | style={{ |
| 121 | background: "#fff", |
| 122 | borderRadius: 12, |
| 123 | boxShadow: "0 2px 8px #e0e7ff", |
| 124 | padding: 24, |
| 125 | marginBottom: 24, |
| 126 | cursor: "pointer", |
| 127 | transition: "box-shadow 0.2s", |
| 128 | }} |
| 129 | onClick={() => navigate(`/forum/${post.post_id}`)} |
| 130 | > |
| 131 | <div style={{ fontSize: 15, color: "#1976d2", marginBottom: 6 }}> |
| 132 | {post.author_name} · {post.created_at} |
| 133 | </div> |
| 134 | <div style={{ fontWeight: 600, fontSize: 20, marginBottom: 8, color: "#222" }}> |
| 135 | {post.title} |
| 136 | </div> |
| 137 | <div style={{ fontSize: 16, color: "#444", marginBottom: 18 }}> |
| 138 | {post.content} |
| 139 | </div> |
| 140 | <div style={{ display: "flex", justifyContent: "flex-start", gap: 32, fontSize: 14, color: "#888" }}> |
| 141 | <span>回复数: {post.reply_count}</span> |
| 142 | <span>观看数: {post.view_count}</span> |
| 143 | </div> |
| 144 | </div> |
| 145 | ))} |
| 146 | </div> |
| 147 | ); |
| 148 | } |