blob: 7939da1e69fedc4645bf104f6e1e0f44b00039ad [file] [log] [blame]
2230113338fd3882025-06-06 23:33:57 +08001import React, { useState, useEffect } from "react";
wht6a1b6782025-06-06 19:14:59 +08002import { useNavigate } from "react-router-dom";
2230113338fd3882025-06-06 23:33:57 +08003import { API_BASE_URL } from "./config";
wht6a1b6782025-06-06 19:14:59 +08004
5// 示例数据
6const 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
29export default function ForumPage() {
30 const navigate = useNavigate();
2230113338fd3882025-06-06 23:33:57 +080031 const [posts, setPosts] = useState([]);
223011339e292152025-06-08 00:34:37 +080032 const [searchQuery, setSearchQuery] = useState('');
2230113338fd3882025-06-06 23:33:57 +080033
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);
223011339e292152025-06-08 00:34:37 +080039 // if (!userId) return;
40 console.log("Fetching forum posts...");
2230113338fd3882025-06-06 23:33:57 +080041 fetch(`${API_BASE_URL}/api/forum`)
223011339e292152025-06-08 00:34:37 +080042 .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 })
2230113338fd3882025-06-06 23:33:57 +080060 .catch(() => setPosts([]));
61 }, []);
223011339e292152025-06-08 00:34:37 +080062
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 };
2230113338fd3882025-06-06 23:33:57 +080082
wht6a1b6782025-06-06 19:14:59 +080083 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="搜索帖子内容"
223011339e292152025-06-08 00:34:37 +080090 value={searchQuery}
91 onChange={e => setSearchQuery(e.target.value)}
wht6a1b6782025-06-06 19:14:59 +080092 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 }}
223011339e292152025-06-08 00:34:37 +0800112 onClick={handleSearch}
wht6a1b6782025-06-06 19:14:59 +0800113 >
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}