blob: 8fdc64052737b4c71770b24a42bc5f34a365bce9 [file] [log] [blame]
Krishyab5ef96d2025-06-05 13:57:05 +08001import React, { useState } from 'react';
2import { useGroupStore } from '../../context/useGroupStore';
3import { useUser } from '../../context/UserContext';
4import CreatePostForm from './CreatePostForm';
5
6const 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; // ✅ 使用正确字段
14console.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%' }}
22301009e68c0dd2025-06-05 15:28:07 +080023 src={group.coverImage || 'https://picsum.photos/200/200'}
Krishyab5ef96d2025-06-05 13:57:05 +080024 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
22301009e68c0dd2025-06-05 15:28:07 +080062export default GroupItem;