blob: 10a48407d6bb23ef3fa1e3f936ec26c64a61c19d [file] [log] [blame]
刘嘉昕9cdeca22025-06-03 16:54:30 +08001import React, { useState, useEffect } from 'react';
2import {
3 createPost,
4 findPinnedPosts,
5 likePost,
6 unlikePost,
7 searchPosts,
8 getAllPostsSorted,
9 findPostsByUserId,
10} from '../api/post';
11import Comment from './Comment';
12
13const Post = () => {
14 const [title, setTitle] = useState('');
15 const [content, setContent] = useState('');
16 const [tags, setTags] = useState('');
17 const [photo, setPhoto] = useState(null);
18 const [posts, setPosts] = useState([]);
19 const [searchKeyword, setSearchKeyword] = useState('');
20
21 const currentUser = { id: 1, username: '测试用户' };
22
23 useEffect(() => {
24 loadPinnedPosts(); // 初始加载置顶帖子
25 }, []);
26
27 const loadPinnedPosts = async () => {
28 const data = await findPinnedPosts();
29 setPosts(data);
30 };
31
32 const loadAllPosts = async () => {
33 const data = await getAllPostsSorted();
34 setPosts(data);
35 };
36
37 const loadMyPosts = async () => {
38 const data = await findPostsByUserId(currentUser.id);
39 setPosts(data);
40 };
41
42 const handleCreate = async () => {
43 const formData = new FormData();
44 formData.append('userid', currentUser.id);
45 formData.append('post_title', title);
46 formData.append('post_content', content);
47 formData.append('is_pinned', true);
48 formData.append('tags', tags);
49 formData.append('rannge', 'public');
50 if (photo) {
51 formData.append('photo', photo);
52 }
53
54 const success = await createPost(formData);
55 if (success) {
56 alert('帖子创建成功');
57 loadPinnedPosts();
58 setTitle('');
59 setContent('');
60 setTags('');
61 setPhoto(null);
62 } else {
63 alert('创建失败');
64 }
65 };
66
67 const handleLike = async (postid) => {
68 await likePost(postid);
69 loadPinnedPosts();
70 };
71
72 const handleUnlike = async (postid) => {
73 await unlikePost(postid);
74 loadPinnedPosts();
75 };
76
77 const handleSearch = async () => {
78 const result = await searchPosts(searchKeyword);
79 setPosts(result);
80 };
81
82 return (
83 <div className="p-4 max-w-3xl mx-auto">
84 <h1 className="text-2xl font-bold mb-4">创建帖子</h1>
85 <div className="space-y-3 mb-6">
86 <input
87 type="text"
88 placeholder="标题"
89 value={title}
90 onChange={(e) => setTitle(e.target.value)}
91 className="w-full border p-2 rounded"
92 />
93 <textarea
94 placeholder="内容"
95 value={content}
96 onChange={(e) => setContent(e.target.value)}
97 className="w-full border p-2 rounded"
98 />
99 <input
100 type="text"
101 placeholder="标签(用逗号分隔,如 学习,编程)"
102 value={tags}
103 onChange={(e) => setTags(e.target.value)}
104 className="w-full border p-2 rounded"
105 />
106 <input
107 type="file"
108 onChange={(e) => setPhoto(e.target.files[0])}
109 />
110 <button
111 onClick={handleCreate}
112 className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
113 >
114 发布
115 </button>
116 </div>
117
118 <div className="mb-6 flex gap-3">
119 <input
120 type="text"
121 placeholder="搜索关键词"
122 value={searchKeyword}
123 onChange={(e) => setSearchKeyword(e.target.value)}
124 className="border p-2 rounded flex-grow"
125 />
126 <button
127 onClick={handleSearch}
128 className="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600"
129 >
130 搜索
131 </button>
132 </div>
133
134 {/* 新增三个展示按钮 */}
135 <div className="mb-6 flex gap-3">
136 <button
137 onClick={loadPinnedPosts}
138 className="bg-yellow-500 text-white px-4 py-2 rounded hover:bg-yellow-600"
139 >
140 置顶帖子
141 </button>
142 <button
143 onClick={loadAllPosts}
144 className="bg-purple-500 text-white px-4 py-2 rounded hover:bg-purple-600"
145 >
146 所有帖子
147 </button>
148 <button
149 onClick={loadMyPosts}
150 className="bg-indigo-500 text-white px-4 py-2 rounded hover:bg-indigo-600"
151 >
152 我的帖子
153 </button>
154 </div>
155
156 <h2 className="text-xl font-semibold mb-3">帖子列表</h2>
157 <div className="space-y-6">
158 {posts.map((post) => (
159 <div key={post.postid} className="border rounded p-4 shadow bg-white">
160 <h3 className="text-lg font-bold">{post.postTitle}</h3>
161 <p className="text-gray-700">{post.postContent}</p>
162 {post.photo && (
163 <img
164 src={`http://localhost:8080${post.photo}`}
165 alt="post"
166 className="w-64 h-auto mt-2"
167 />
168 )}
169 <div className="mt-2 text-sm text-gray-500">
170 发布时间:{post.postCreatedTime}
171 </div>
172 <div className="mt-2 text-sm text-gray-500">
173 标签:{post.tags || '无'}
174 </div>
175 <div className="mt-2 flex items-center gap-4">
176 <span>👍 {post.likes}</span>
177 <button
178 onClick={() => handleLike(post.postid)}
179 className="text-blue-500 hover:underline"
180 >
181 点赞
182 </button>
183 <button
184 onClick={() => handleUnlike(post.postid)}
185 className="text-red-500 hover:underline"
186 >
187 取消点赞
188 </button>
189 </div>
190 <div className="mt-4 border-t pt-4">
191 <Comment postId={post.postid} currentUser={currentUser} />
192 </div>
193 </div>
194 ))}
195 </div>
196 </div>
197 );
198};
199
200export default Post;