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'; |
| 4 | import CreatePostForm from './CreatePostForm'; |
| 5 | |
| 6 | const GroupItem = ({ group }) => { |
| 7 | |
| 8 | console.log('group:', group); |
| 9 | const { handleJoinGroup, joinStatus } = useGroupStore(); |
| 10 | const { user } = useUser(); |
| 11 | |
| 12 | const userId = user?.userId; |
| 13 | const groupId = group.groupId; // ✅ 使用正确字段 |
| 14 | console.log('加入小组请求 - groupId:', group.group_id, 'userId:', userId); |
| 15 | |
| 16 | const [showCreatePost, setShowCreatePost] = useState(false); |
| 17 | |
| 18 | return ( |
| 19 | <div className="group-item"> |
| 20 | <div className="group-content"> |
| 21 | <img |
| 22 | style={{ width: '40%', height: '40%' }} |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame^] | 23 | src={group.coverImage || 'https://picsum.photos/200/200'} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 24 | alt={group.groupName} |
| 25 | className="group-cover" |
| 26 | /> |
| 27 | <div className="group-info-right"> |
| 28 | <h3>{group.groupName}</h3> {/* ✅ 使用 groupName */} |
| 29 | <p style={{ color: '#BA929A' }}>{group.memberCount || 0}人加入了小组</p> |
| 30 | |
| 31 | <button |
| 32 | onClick={() => handleJoinGroup(groupId, userId)} |
| 33 | disabled={joinStatus[groupId] === '加入成功' || !userId} |
| 34 | > |
| 35 | |
| 36 | {joinStatus[groupId] === '加入成功' ? '已加入' : userId ? '+加入小组' : '请登录'} |
| 37 | </button> |
| 38 | |
| 39 | {userId && joinStatus[groupId] === '加入成功' && ( |
| 40 | <button onClick={() => setShowCreatePost(!showCreatePost)}> |
| 41 | +发布帖子 |
| 42 | </button> |
| 43 | )} |
| 44 | </div> |
| 45 | </div> |
| 46 | |
| 47 | <div className="group-description"> |
| 48 | <p>{group.description}</p> |
| 49 | </div> |
| 50 | <p>分类:{group.category}</p> |
| 51 | |
| 52 | {showCreatePost && ( |
| 53 | <CreatePostForm |
| 54 | groupId={groupId} |
| 55 | onClose={() => setShowCreatePost(false)} |
| 56 | /> |
| 57 | )} |
| 58 | </div> |
| 59 | ); |
| 60 | }; |
| 61 | |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame^] | 62 | export default GroupItem; |