Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 2 | import { useGroupStore } from '../../context/useGroupStore'; |
| 3 | import { useUser } from '../../context/UserContext'; |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 4 | import { useLocation } from 'wouter'; |
| 5 | import './GroupDetail.css'; |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 6 | import AuthButton from '../../components/AuthButton'; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 7 | |
| 8 | const GroupItem = ({ group }) => { |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 9 | const [, setLocation] = useLocation(); |
| 10 | const { handleJoinGroup, joinStatus, setJoinStatus, fetchGroupList } = useGroupStore(); |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 11 | const { user } = useUser(); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 12 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 13 | const userId = user?.userId; |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 14 | const groupId = group.groupId; |
| 15 | |
| 16 | const [isMember, setIsMember] = useState(false); |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 17 | const [loading, setLoading] = useState(false); |
| 18 | const [error, setError] = useState(''); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 19 | |
| 20 | useEffect(() => { |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 21 | setIsMember(joinStatus[groupId] === '加入成功'); |
| 22 | }, [joinStatus, groupId]); |
| 23 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 24 | useEffect(() => { |
| 25 | if (userId && groupId) { |
| 26 | checkMembershipStatus(); |
| 27 | } |
| 28 | }, [userId, groupId]); |
| 29 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 30 | const checkMembershipStatus = async () => { |
| 31 | try { |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 32 | const res = await fetch(`/echo/groups/${groupId}/members`); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 33 | const isMember = res.data.members.some(member => member.user_id === userId); |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 34 | setIsMember(isMember); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 35 | } catch (error) { |
| 36 | console.error('检查成员状态失败:', error); |
| 37 | } |
| 38 | }; |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 39 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 40 | const handleLeaveGroup = async () => { |
| 41 | setLoading(true); |
| 42 | try { |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 43 | const res = await fetch(`/echo/groups/${groupId}/leave`, { |
| 44 | method: 'POST', |
| 45 | headers: { |
| 46 | 'Content-Type': 'application/json', |
| 47 | }, |
| 48 | body: JSON.stringify({ |
| 49 | user_id: userId, |
| 50 | }), |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 51 | }); |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 52 | |
| 53 | if (res.ok && res.data.status === 'success') { |
| 54 | fetchGroupList(); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 55 | setIsMember(false); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 56 | group.memberCount = (group.memberCount || 0) - 1; |
| 57 | } else { |
| 58 | setError(res.data.message || '退出失败'); |
| 59 | } |
| 60 | } catch (error) { |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 61 | // console.error('退出小组失败:', error); |
| 62 | // setError('退出小组失败'); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 63 | } finally { |
| 64 | setLoading(false); |
| 65 | } |
| 66 | }; |
| 67 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 68 | const handleJoin = async () => { |
| 69 | setLoading(true); |
| 70 | try { |
| 71 | const res = await handleJoinGroup(groupId, userId); |
| 72 | if (res && res.status === 'success') { |
| 73 | setJoinStatus(groupId, '加入成功'); |
| 74 | setIsMember(true); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 75 | group.memberCount = (group.memberCount || 0) + 1; |
| 76 | } else { |
| 77 | setError(res?.message || '加入失败'); |
| 78 | } |
| 79 | } catch (error) { |
| 80 | console.error('加入小组失败:', error); |
| 81 | setError('加入小组失败'); |
| 82 | } finally { |
| 83 | setLoading(false); |
| 84 | } |
| 85 | }; |
| 86 | |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 87 | return ( |
| 88 | <div className="group-item"> |
| 89 | <div className="group-content"> |
| 90 | <img |
| 91 | style={{ width: '40%', height: '40%' }} |
22301009 | e68c0dd | 2025-06-05 15:28:07 +0800 | [diff] [blame] | 92 | src={group.coverImage || 'https://picsum.photos/200/200'} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 93 | alt={group.groupName} |
| 94 | className="group-cover" |
| 95 | /> |
| 96 | <div className="group-info-right"> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 97 | <h3>{group.groupName}</h3> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 98 | <p style={{ color: '#BA929A' }}>{group.memberCount || 0}人加入了小组</p> |
| 99 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 100 | {userId && ( |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 101 | <AuthButton |
| 102 | roles={["chocolate", "ice-cream"]} |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 103 | style={{ color: '#2167c9', background: 'none', border: 'none', padding: 0, cursor: 'pointer', fontSize: '16px' }} |
| 104 | onClick={(e) => { |
| 105 | e.stopPropagation(); |
| 106 | isMember ? handleLeaveGroup() : handleJoin(); |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 107 | }} |
| 108 | disabled={loading} |
| 109 | > |
| 110 | {loading ? '处理中...' : isMember ? '退出小组' : '+加入小组'} |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 111 | </AuthButton> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 112 | )} |
| 113 | {!userId && <button disabled>请登录</button>} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 114 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 115 | {error && <p style={{ color: 'red' }}>{error}</p>} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 116 | </div> |
| 117 | </div> |
| 118 | |
| 119 | <div className="group-description"> |
| 120 | <p>{group.description}</p> |
| 121 | </div> |
| 122 | <p>分类:{group.category}</p> |
| 123 | |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 124 | <button |
| 125 | style={{ |
| 126 | background: 'none', |
| 127 | border: 'none', |
| 128 | color: '#985F6F', |
| 129 | fontSize: '16px', |
| 130 | cursor: 'pointer', |
| 131 | textDecoration: 'underline', |
| 132 | marginBottom: '8px', |
| 133 | float: 'right', |
| 134 | clear: 'both', |
| 135 | }} |
| 136 | onClick={() => setLocation(`/group/${groupId}`)} |
| 137 | > |
| 138 | 查看详情 |
| 139 | </button> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 140 | </div> |
| 141 | ); |
| 142 | }; |
| 143 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 144 | export default GroupItem; |