Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 1 | import React, { useEffect, useState } from 'react'; |
| 2 | import { useParams } from 'wouter'; |
| 3 | import { useUser } from '../../context/UserContext'; |
| 4 | import GroupMembers from './GroupMembers'; |
| 5 | import GroupPosts from './GroupPosts'; |
| 6 | import CreatePostForm from './CreatePostForm'; |
| 7 | import './GroupDetail.css'; |
| 8 | |
| 9 | const GroupDetail = () => { |
| 10 | const { groupId } = useParams(); |
| 11 | console.log('GroupDetail groupId:', groupId); |
| 12 | |
| 13 | const { user } = useUser(); |
| 14 | const userId = user?.userId; |
| 15 | |
| 16 | const [members, setMembers] = useState([]); |
| 17 | const [posts, setPosts] = useState([]); |
| 18 | const [loading, setLoading] = useState(true); |
| 19 | const [error, setError] = useState(null); |
| 20 | const [showCommentForm, setShowCommentForm] = useState(null); |
| 21 | const [refetchKey, setRefetchKey] = useState(0); |
| 22 | const [showCreatePost, setShowCreatePost] = useState(false); |
| 23 | const [isMember, setIsMember] = useState(false); |
| 24 | |
| 25 | useEffect(() => { |
| 26 | if (!groupId) { |
| 27 | setError('小组ID缺失'); |
| 28 | setLoading(false); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const fetchGroupData = async () => { |
| 33 | try { |
| 34 | const res1 = await fetch(`/echo/groups/${groupId}/members`); |
| 35 | const membersData = await res1.json(); |
| 36 | |
| 37 | const res2 = await fetch(`/echo/groups/${groupId}/getAllPosts`); |
| 38 | const postsData = await res2.json(); |
| 39 | |
| 40 | if (res1.ok) { |
| 41 | setMembers(membersData.members || []); |
| 42 | setIsMember(userId && membersData.members.some(m => m.user_id === userId)); |
| 43 | } |
| 44 | |
| 45 | if (res2.ok) { |
| 46 | const postsWithLikes = postsData.posts?.map(post => ({ |
| 47 | ...post, |
| 48 | userLiked: false, |
| 49 | likes: post.likes || 0 |
| 50 | })) || []; |
| 51 | setPosts(postsWithLikes); |
| 52 | } |
| 53 | } catch (err) { |
| 54 | setError('加载失败,请稍后重试'); |
| 55 | } finally { |
| 56 | setLoading(false); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | fetchGroupData(); |
| 61 | }, [groupId, refetchKey, userId]); |
| 62 | |
| 63 | const toggleLike = async (postId) => { |
| 64 | const postIndex = posts.findIndex(p => p.group_post_id === postId); |
| 65 | if (postIndex === -1) return; |
| 66 | |
| 67 | const currentPost = posts[postIndex]; |
| 68 | const isLiked = currentPost.userLiked; |
| 69 | |
| 70 | try { |
| 71 | const response = await fetch( |
| 72 | `/echo/groups/${postId}/${isLiked ? 'unlike' : 'like'}`, |
| 73 | { |
| 74 | method: 'POST', |
| 75 | headers: { |
| 76 | 'Content-Type': 'application/json', |
| 77 | }, |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | const data = await response.json(); |
| 82 | if (response.ok && data.status === 'success') { |
| 83 | const updatedPosts = [...posts]; |
| 84 | updatedPosts[postIndex] = { |
| 85 | ...currentPost, |
| 86 | userLiked: !isLiked, |
| 87 | likes: isLiked ? currentPost.likes - 1 : currentPost.likes + 1 |
| 88 | }; |
| 89 | setPosts(updatedPosts); |
| 90 | |
| 91 | setTimeout(() => { |
| 92 | setRefetchKey(prev => prev + 1); |
| 93 | }, 200); |
| 94 | } else { |
| 95 | console.error(isLiked ? '取消点赞失败' : '点赞失败:', data.message); |
| 96 | } |
| 97 | } catch (error) { |
| 98 | console.error(isLiked ? '取消点赞请求出错' : '点赞请求出错:', error); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | const handleSubmitComment = async (postId, content) => { |
| 103 | if (!userId) { |
| 104 | alert('请先登录'); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | const response = await fetch(`/echo/groups/${postId}/comment`, { |
| 110 | method: 'POST', |
| 111 | headers: { |
| 112 | 'Content-Type': 'application/json', |
| 113 | }, |
| 114 | body: JSON.stringify({ |
| 115 | userId, |
| 116 | content |
| 117 | }) |
| 118 | }); |
| 119 | |
| 120 | const data = await response.json(); |
| 121 | if (response.ok && data.status === 'success') { |
| 122 | setPosts(prevPosts => prevPosts.map(post => |
| 123 | post.group_post_id === postId ? |
| 124 | {...post, comments: [...(post.comments || []), { |
| 125 | id: Date.now(), |
| 126 | userId, |
| 127 | username: user.username, |
| 128 | content, |
| 129 | time: new Date().toISOString() |
| 130 | }]} : |
| 131 | post |
| 132 | )); |
| 133 | setShowCommentForm(null); |
| 134 | setRefetchKey(prev => prev + 1); |
| 135 | } else { |
| 136 | console.error('评论失败:', data.message); |
| 137 | } |
| 138 | } catch (error) { |
| 139 | console.error('评论请求出错:', error); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | const handlePostCreated = () => { |
| 144 | setShowCreatePost(false); |
| 145 | setRefetchKey(prev => prev + 1); |
| 146 | }; |
| 147 | |
| 148 | if (loading) return <div className="group-detail"><p>加载中...</p></div>; |
| 149 | if (error) return <div className="group-detail"><p>{error}</p></div>; |
| 150 | |
| 151 | return ( |
| 152 | <div className="group-detail"> |
| 153 | <h1 className="group-title">兴趣小组详情</h1> |
| 154 | |
| 155 | <GroupMembers members={members} /> |
| 156 | |
| 157 | <GroupPosts |
| 158 | posts={posts} |
| 159 | members={members} |
| 160 | userId={userId} |
| 161 | toggleLike={toggleLike} |
| 162 | handleSubmitComment={handleSubmitComment} |
| 163 | showCommentForm={showCommentForm} |
| 164 | setShowCommentForm={setShowCommentForm} |
| 165 | isMember={isMember} |
| 166 | onShowCreatePost={() => setShowCreatePost(true)} |
| 167 | loginHint={!userId ? "请登录后发布帖子" : "加入小组后即可发布帖子"} |
| 168 | /> |
| 169 | |
| 170 | |
| 171 | {showCreatePost && ( |
| 172 | <div className="modal-overlay"> |
| 173 | <div className="modal-content"> |
| 174 | <button className="modal-close" onClick={() => setShowCreatePost(false)}>×</button> |
| 175 | <CreatePostForm |
| 176 | groupId={groupId} |
| 177 | onClose={() => setShowCreatePost(false)} |
| 178 | onPostCreated={handlePostCreated} |
| 179 | /> |
| 180 | </div> |
| 181 | </div> |
| 182 | )} |
| 183 | |
| 184 | |
| 185 | </div> |
| 186 | ); |
| 187 | }; |
| 188 | |
| 189 | export default GroupDetail; |