Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 1 | // import React, { useState, useEffect } from 'react'; |
| 2 | // import { useGroupStore } from '../../context/useGroupStore'; |
| 3 | // import { useUser } from '../../context/UserContext'; |
| 4 | // import CreatePostForm from './CreatePostForm'; |
| 5 | // import axios from 'axios'; // 新增 |
| 6 | |
| 7 | // const GroupItem = ({ group }) => { |
| 8 | // const { handleJoinGroup, joinStatus, setJoinStatus } = useGroupStore(); // 假设你有 setJoinStatus 方法 |
| 9 | // const { user } = useUser(); |
| 10 | |
| 11 | // const userId = user?.userId; |
| 12 | // const groupId = group.groupId; |
| 13 | |
| 14 | // const [isMember, setIsMember] = useState(false); |
| 15 | |
| 16 | // useEffect(() => { |
| 17 | // setIsMember(joinStatus[groupId] === '加入成功'); |
| 18 | // }, [joinStatus, groupId]); |
| 19 | |
| 20 | // const [showCreatePost, setShowCreatePost] = useState(false); |
| 21 | |
| 22 | // // 退出小组函数(新增) |
| 23 | // const handleLeaveGroup = async () => { |
| 24 | // try { |
| 25 | // const res = await axios.post(`/echo/groups/${groupId}/leave`, { |
| 26 | // user_id: userId, |
| 27 | // }); |
| 28 | // if (res.data.status === 'success') { |
| 29 | // setJoinStatus(groupId, '未加入'); // 更新全局状态(需确保 useGroupStore 中有此方法) |
| 30 | // setIsMember(false); // 本地状态也更新 |
| 31 | // } else { |
| 32 | // alert(res.data.message || '退出失败'); |
| 33 | // } |
| 34 | // } catch (error) { |
| 35 | // console.error('退出小组失败:', error); |
| 36 | // alert('退出小组失败'); |
| 37 | // } |
| 38 | // }; |
| 39 | |
| 40 | // return ( |
| 41 | // <div className="group-item"> |
| 42 | // <div className="group-content"> |
| 43 | // <img |
| 44 | // style={{ width: '40%', height: '40%' }} |
| 45 | // src={group.coverImage || 'https://picsum.photos/200/200'} |
| 46 | // alt={group.groupName} |
| 47 | // className="group-cover" |
| 48 | // /> |
| 49 | // <div className="group-info-right"> |
| 50 | // <h3>{group.groupName}</h3> |
| 51 | // <p style={{ color: '#BA929A' }}>{group.memberCount || 0}人加入了小组</p> |
| 52 | |
| 53 | // {/* 加入/退出按钮逻辑 */} |
| 54 | // {userId && ( |
| 55 | // <button |
| 56 | // onClick={() => { |
| 57 | // if (isMember) { |
| 58 | // handleLeaveGroup(); // 已加入 -> 退出 |
| 59 | // } else { |
| 60 | // handleJoinGroup(groupId, userId); // 未加入 -> 加入 |
| 61 | // } |
| 62 | // }} |
| 63 | // > |
| 64 | // {isMember ? '退出小组' : '+加入小组'} |
| 65 | // </button> |
| 66 | // )} |
| 67 | // {!userId && <button disabled>请登录</button>} |
| 68 | |
| 69 | // {/* 发布帖子按钮 */} |
| 70 | // {userId && isMember && ( |
| 71 | // <button onClick={() => setShowCreatePost(!showCreatePost)}> |
| 72 | // +发布帖子 |
| 73 | // </button> |
| 74 | // )} |
| 75 | // </div> |
| 76 | // </div> |
| 77 | |
| 78 | // <div className="group-description"> |
| 79 | // <p>{group.description}</p> |
| 80 | // </div> |
| 81 | // <p>分类:{group.category}</p> |
| 82 | |
| 83 | // {showCreatePost && ( |
| 84 | // <CreatePostForm |
| 85 | // groupId={groupId} |
| 86 | // onClose={() => setShowCreatePost(false)} |
| 87 | // /> |
| 88 | // )} |
| 89 | // </div> |
| 90 | // ); |
| 91 | // }; |
| 92 | |
| 93 | // export default GroupItem; |
| 94 | |
| 95 | import React, { useState, useEffect } from 'react'; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 96 | import { useGroupStore } from '../../context/useGroupStore'; |
| 97 | import { useUser } from '../../context/UserContext'; |
| 98 | import CreatePostForm from './CreatePostForm'; |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 99 | import axios from 'axios'; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 100 | |
| 101 | const GroupItem = ({ group }) => { |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 102 | const { handleJoinGroup, joinStatus, setJoinStatus } = useGroupStore(); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 103 | const { user } = useUser(); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 104 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 105 | const userId = user?.userId; |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 106 | const groupId = group.groupId; |
| 107 | |
| 108 | const [isMember, setIsMember] = useState(false); |
| 109 | const [loading, setLoading] = useState(false); // 新增:加载状态 |
| 110 | const [error, setError] = useState(''); // 新增:错误信息 |
| 111 | |
| 112 | useEffect(() => { |
| 113 | console.log('joinStatus updated:', joinStatus); |
| 114 | setIsMember(joinStatus[groupId] === '加入成功'); |
| 115 | }, [joinStatus, groupId]); |
| 116 | |
| 117 | // 初始挂载时检查成员状态(新增) |
| 118 | useEffect(() => { |
| 119 | if (userId && groupId) { |
| 120 | checkMembershipStatus(); |
| 121 | } |
| 122 | }, [userId, groupId]); |
| 123 | |
| 124 | // 检查成员状态(新增) |
| 125 | const checkMembershipStatus = async () => { |
| 126 | try { |
| 127 | const res = await axios.get(`/echo/groups/${groupId}/members`); |
| 128 | const isMember = res.data.members.some(member => member.user_id === userId); |
| 129 | setJoinStatus(groupId, isMember ? '加入成功' : '未加入'); |
| 130 | } catch (error) { |
| 131 | console.error('检查成员状态失败:', error); |
| 132 | } |
| 133 | }; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 134 | |
| 135 | const [showCreatePost, setShowCreatePost] = useState(false); |
| 136 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 137 | const handleLeaveGroup = async () => { |
| 138 | setLoading(true); |
| 139 | try { |
| 140 | const res = await axios.post(`/echo/groups/${groupId}/leave`, { |
| 141 | user_id: userId, |
| 142 | }); |
| 143 | if (res.data.status === 'success') { |
| 144 | setJoinStatus(groupId, '未加入'); |
| 145 | setIsMember(false); |
| 146 | // 可选:刷新小组成员计数 |
| 147 | group.memberCount = (group.memberCount || 0) - 1; |
| 148 | } else { |
| 149 | setError(res.data.message || '退出失败'); |
| 150 | } |
| 151 | } catch (error) { |
| 152 | console.error('退出小组失败:', error); |
| 153 | setError('退出小组失败'); |
| 154 | } finally { |
| 155 | setLoading(false); |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | // 修改加入小组逻辑(新增) |
| 160 | const handleJoin = async () => { |
| 161 | setLoading(true); |
| 162 | try { |
| 163 | const res = await handleJoinGroup(groupId, userId); |
| 164 | if (res && res.status === 'success') { |
| 165 | setJoinStatus(groupId, '加入成功'); |
| 166 | setIsMember(true); |
| 167 | // 可选:刷新小组成员计数 |
| 168 | group.memberCount = (group.memberCount || 0) + 1; |
| 169 | } else { |
| 170 | setError(res?.message || '加入失败'); |
| 171 | } |
| 172 | } catch (error) { |
| 173 | console.error('加入小组失败:', error); |
| 174 | setError('加入小组失败'); |
| 175 | } finally { |
| 176 | setLoading(false); |
| 177 | } |
| 178 | }; |
| 179 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 180 | return ( |
| 181 | <div className="group-item"> |
| 182 | <div className="group-content"> |
| 183 | <img |
| 184 | style={{ width: '40%', height: '40%' }} |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame] | 185 | src={group.coverImage || 'https://picsum.photos/200/200'} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 186 | alt={group.groupName} |
| 187 | className="group-cover" |
| 188 | /> |
| 189 | <div className="group-info-right"> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 190 | <h3>{group.groupName}</h3> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 191 | <p style={{ color: '#BA929A' }}>{group.memberCount || 0}人加入了小组</p> |
| 192 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 193 | {/* 加入/退出按钮逻辑 */} |
| 194 | {userId && ( |
| 195 | <button |
| 196 | onClick={() => { |
| 197 | if (isMember) { |
| 198 | handleLeaveGroup(); |
| 199 | } else { |
| 200 | handleJoin(); |
| 201 | } |
| 202 | }} |
| 203 | disabled={loading} |
| 204 | > |
| 205 | {loading ? '处理中...' : isMember ? '退出小组' : '+加入小组'} |
| 206 | </button> |
| 207 | )} |
| 208 | {!userId && <button disabled>请登录</button>} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 209 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 210 | {/* 显示错误信息(新增) */} |
| 211 | {error && <p style={{ color: 'red' }}>{error}</p>} |
| 212 | |
| 213 | {/* 发布帖子按钮 */} |
| 214 | {userId && isMember && ( |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 215 | <button onClick={() => setShowCreatePost(!showCreatePost)}> |
| 216 | +发布帖子 |
| 217 | </button> |
| 218 | )} |
| 219 | </div> |
| 220 | </div> |
| 221 | |
| 222 | <div className="group-description"> |
| 223 | <p>{group.description}</p> |
| 224 | </div> |
| 225 | <p>分类:{group.category}</p> |
| 226 | |
| 227 | {showCreatePost && ( |
| 228 | <CreatePostForm |
| 229 | groupId={groupId} |
| 230 | onClose={() => setShowCreatePost(false)} |
| 231 | /> |
| 232 | )} |
| 233 | </div> |
| 234 | ); |
| 235 | }; |
| 236 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 237 | export default GroupItem; |