Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 1 | // import React, { useState } from 'react'; |
| 2 | // import { useGroupStore } from '../../context/useGroupStore'; |
| 3 | // import { useUser } from '../../context/UserContext'; // ✅ 新增:引入 UserContext |
| 4 | // import CreatePostForm from './CreatePostForm'; |
| 5 | |
| 6 | // const GroupItem = ({ group }) => { |
| 7 | // const { handleJoinGroup, joinStatus } = useGroupStore(); |
| 8 | // const { user } = useUser(); // ✅ 获取 user |
| 9 | // const userId = user?.userId; // ✅ 安全获取 userId |
| 10 | // const [showCreatePost, setShowCreatePost] = useState(false); |
| 11 | |
| 12 | // return ( |
| 13 | // <div className="group-item"> |
| 14 | // <div className="group-content"> |
| 15 | // <img |
| 16 | // style={{ width: '40%', height: '40%' }} |
| 17 | // src={group.cover_image || 'https://picsum.photos/200/200'} |
| 18 | // alt={group.groupName || group.name} |
| 19 | // className="group-cover" |
| 20 | // /> |
| 21 | // <div className="group-info-right"> |
| 22 | // <h3>{group.groupName || group.name}</h3> |
| 23 | // <p style={{ color: '#BA929A' }}>{group.memberCount || 0}人加入了小组</p> |
| 24 | // <button |
| 25 | // onClick={() => handleJoinGroup(group.group_id, userId)} |
| 26 | // disabled={joinStatus[group.group_id] === '加入成功' || !userId} |
| 27 | // > |
| 28 | // {joinStatus[group.group_id] === '加入成功' ? '已加入' : userId ? '+加入小组' : '请登录'} |
| 29 | // </button> |
| 30 | |
| 31 | // {userId && joinStatus[group.group_id] === '加入成功' && ( |
| 32 | // <button onClick={() => setShowCreatePost(!showCreatePost)}> |
| 33 | // +发布帖子 |
| 34 | // </button> |
| 35 | // )} |
| 36 | // </div> |
| 37 | // </div> |
| 38 | |
| 39 | // <div className="group-description"> |
| 40 | // <p>{group.description}</p> |
| 41 | // </div> |
| 42 | // <p>分类:{group.category}</p> |
| 43 | |
| 44 | // {showCreatePost && ( |
| 45 | // <CreatePostForm |
| 46 | // groupId={group.group_id} |
| 47 | // onClose={() => setShowCreatePost(false)} |
| 48 | // /> |
| 49 | // )} |
| 50 | // </div> |
| 51 | // ); |
| 52 | // }; |
| 53 | |
| 54 | // export default GroupItem; |
| 55 | |
| 56 | |
| 57 | import React, { useState } from 'react'; |
| 58 | import { useGroupStore } from '../../context/useGroupStore'; |
| 59 | import { useUser } from '../../context/UserContext'; |
| 60 | import CreatePostForm from './CreatePostForm'; |
| 61 | |
| 62 | const GroupItem = ({ group }) => { |
| 63 | |
| 64 | console.log('group:', group); |
| 65 | const { handleJoinGroup, joinStatus } = useGroupStore(); |
| 66 | const { user } = useUser(); |
| 67 | |
| 68 | const userId = user?.userId; |
| 69 | const groupId = group.groupId; // ✅ 使用正确字段 |
| 70 | console.log('加入小组请求 - groupId:', group.group_id, 'userId:', userId); |
| 71 | |
| 72 | const [showCreatePost, setShowCreatePost] = useState(false); |
| 73 | |
| 74 | return ( |
| 75 | <div className="group-item"> |
| 76 | <div className="group-content"> |
| 77 | <img |
| 78 | style={{ width: '40%', height: '40%' }} |
| 79 | src={group.cover_image || 'https://picsum.photos/200/200'} |
| 80 | alt={group.groupName} |
| 81 | className="group-cover" |
| 82 | /> |
| 83 | <div className="group-info-right"> |
| 84 | <h3>{group.groupName}</h3> {/* ✅ 使用 groupName */} |
| 85 | <p style={{ color: '#BA929A' }}>{group.memberCount || 0}人加入了小组</p> |
| 86 | |
| 87 | <button |
| 88 | onClick={() => handleJoinGroup(groupId, userId)} |
| 89 | disabled={joinStatus[groupId] === '加入成功' || !userId} |
| 90 | > |
| 91 | |
| 92 | {joinStatus[groupId] === '加入成功' ? '已加入' : userId ? '+加入小组' : '请登录'} |
| 93 | </button> |
| 94 | |
| 95 | {userId && joinStatus[groupId] === '加入成功' && ( |
| 96 | <button onClick={() => setShowCreatePost(!showCreatePost)}> |
| 97 | +发布帖子 |
| 98 | </button> |
| 99 | )} |
| 100 | </div> |
| 101 | </div> |
| 102 | |
| 103 | <div className="group-description"> |
| 104 | <p>{group.description}</p> |
| 105 | </div> |
| 106 | <p>分类:{group.category}</p> |
| 107 | |
| 108 | {showCreatePost && ( |
| 109 | <CreatePostForm |
| 110 | groupId={groupId} |
| 111 | onClose={() => setShowCreatePost(false)} |
| 112 | /> |
| 113 | )} |
| 114 | </div> |
| 115 | ); |
| 116 | }; |
| 117 | |
| 118 | export default GroupItem; |
| 119 | |
| 120 | |
| 121 | // import React, { useState } from 'react'; |
| 122 | // import { useGroupStore } from '../../context/useGroupStore'; |
| 123 | // import { useUser } from '../../context/UserContext'; |
| 124 | // import CreatePostForm from './CreatePostForm'; |
| 125 | |
| 126 | // const GroupItem = ({ group }) => { |
| 127 | // console.log('group:', group); |
| 128 | |
| 129 | // const { handleJoinGroup, joinStatus } = useGroupStore(); |
| 130 | // const { user } = useUser(); |
| 131 | // const userId = user?.userId; // 确保使用正确的字段名(取自 localStorage 的结构) |
| 132 | // const [showCreatePost, setShowCreatePost] = useState(false); |
| 133 | |
| 134 | // return ( |
| 135 | // <div className="group-item"> |
| 136 | // <div className="group-content"> |
| 137 | // <img |
| 138 | // style={{ width: '40%', height: '40%' }} |
| 139 | // src={group.cover_image || 'https://picsum.photos/200/200'} |
| 140 | // alt={group.name} |
| 141 | // className="group-cover" |
| 142 | // /> |
| 143 | // <div className="group-info-right"> |
| 144 | // <h3>{group.name}</h3> |
| 145 | // <p style={{ color: '#BA929A' }}>{group.member_count || 0}人加入了小组</p> |
| 146 | |
| 147 | // <button |
| 148 | // onClick={() => handleJoinGroup(group.group_id, userId)} |
| 149 | // disabled={joinStatus[group.group_id] === '加入成功' || !userId} |
| 150 | // > |
| 151 | // {joinStatus[group.group_id] === '加入成功' ? '已加入' : userId ? '+加入小组' : '请登录'} |
| 152 | // </button> |
| 153 | |
| 154 | // {userId && joinStatus[group.group_id] === '加入成功' && ( |
| 155 | // <button onClick={() => setShowCreatePost(!showCreatePost)}> |
| 156 | // +发布帖子 |
| 157 | // </button> |
| 158 | // )} |
| 159 | // </div> |
| 160 | // </div> |
| 161 | |
| 162 | // <div className="group-description"> |
| 163 | // <p>{group.description}</p> |
| 164 | // </div> |
| 165 | // <p>分类:{group.category}</p> |
| 166 | |
| 167 | // {showCreatePost && ( |
| 168 | // <CreatePostForm |
| 169 | // groupId={group.group_id} |
| 170 | // onClose={() => setShowCreatePost(false)} |
| 171 | // /> |
| 172 | // )} |
| 173 | // </div> |
| 174 | // ); |
| 175 | // }; |
| 176 | |
| 177 | // export default GroupItem; |
| 178 | |