blob: b3ae332d4f9eb009c58d743478d389b6f5374d71 [file] [log] [blame]
Krishya73cd8822025-06-07 15:48:41 +08001import React, { useEffect, useState } from 'react';
Krishyab5ef96d2025-06-05 13:57:05 +08002import Header from '../../components/Header';
3import { useGroupStore } from '../../context/useGroupStore';
Krishyab5ef96d2025-06-05 13:57:05 +08004import GroupList from './GroupList';
5import GroupPagination from './GroupPagination';
223010093a876cc2025-04-14 16:22:20 +08006import './InterestGroup.css';
Krishya73cd8822025-06-07 15:48:41 +08007import axios from 'axios';
223010093a876cc2025-04-14 16:22:20 +08008
Krishya73cd8822025-06-07 15:48:41 +08009const InterestGroup = () => {
10 const { fetchGroupList } = useGroupStore();
11
12 const [showModal, setShowModal] = useState(false);
13 const [groupName, setGroupName] = useState('');
14 const [groupDescription, setGroupDescription] = useState('');
15
223010093a876cc2025-04-14 16:22:20 +080016 useEffect(() => {
Krishyab5ef96d2025-06-05 13:57:05 +080017 fetchGroupList();
18 }, [fetchGroupList]);
Krishyac0f7e9b2025-04-22 15:28:28 +080019
Krishya73cd8822025-06-07 15:48:41 +080020 const handleCreateGroup = async () => {
21 try {
22 // ✅ 修改请求体字段名,使用驼峰命名法
23 const res = await axios.post('http://localhost:3011/echo/groups/createGroup', {
24 userId: 1, // 改为驼峰命名 userId
25 groupName: groupName, // 改为驼峰命名 groupName
26 description: groupDescription,
27 // 移除time字段,使用后端生成的时间
28 memberCount: 1, // 添加初始成员数
29 category: '默认分类',
30 coverImage: 'https://picsum.photos/300/200', // 改为驼峰命名 coverImage
31 });
32
33 if (res.status === 200 && res.data.status === 'success') {
34 alert('小组创建成功');
35 setShowModal(false);
36 setGroupName('');
37 setGroupDescription('');
38 fetchGroupList(); // 刷新列表
39 } else {
40 // 显示后端返回的详细错误信息
41 alert('创建失败: ' + res.data.message);
42 }
43 } catch (error) {
44 // 处理网络错误或其他异常
45 alert('创建失败,请检查网络连接或输入内容');
46 console.error('创建小组错误:', error);
47 }
48 };
49
223010093a876cc2025-04-14 16:22:20 +080050 return (
51 <div className="interest-group-container">
223010093a876cc2025-04-14 16:22:20 +080052 <Header />
Krishyac0f7e9b2025-04-22 15:28:28 +080053 <div className="interest-group-card">
Krishya73cd8822025-06-07 15:48:41 +080054 <button className="create-group-btn" onClick={() => setShowModal(true)}>
55 创建小组
56 </button>
57
58 {showModal && (
59 <div className="modal-overlay">
60 <div className="modal-content">
61 <h2>创建新小组</h2>
62 <input
63 type="text"
64 placeholder="小组名称"
65 value={groupName}
66 onChange={(e) => setGroupName(e.target.value)}
67 />
68 <textarea
69 placeholder="小组简介"
70 value={groupDescription}
71 onChange={(e) => setGroupDescription(e.target.value)}
72 />
73 <div className="modal-buttons">
74 <button onClick={handleCreateGroup}>确定</button>
75 <button onClick={() => setShowModal(false)}>取消</button>
76 </div>
77 </div>
78 </div>
79 )}
80
Krishyab5ef96d2025-06-05 13:57:05 +080081 <GroupList />
82 <GroupPagination />
223010093a876cc2025-04-14 16:22:20 +080083 </div>
84 </div>
85 );
86};
87
Krishyac0f7e9b2025-04-22 15:28:28 +080088export default InterestGroup;