blob: 0b5b002aac23f09b6bdee0aed9a7c881fe4dd527 [file] [log] [blame]
Krishya73cd8822025-06-07 15:48:41 +08001// // import React, { useEffect } from 'react';
2// // import Header from '../../components/Header';
3// // import { useGroupStore } from '../../context/useGroupStore';
4// // // import GroupFilters from './GroupFilters';
5// // import GroupList from './GroupList';
6// // import GroupPagination from './GroupPagination';
7// // import './InterestGroup.css';
8// // const InterestGroup = () => {
9// // const { fetchGroupList, setPage, handleSearch } = useGroupStore();
10
11// // // 初始化加载
12// // useEffect(() => {
13// // fetchGroupList();
14// // }, [fetchGroupList]);
15
16// // return (
17// // <div className="interest-group-container">
18// // <Header />
19// // <div className="interest-group-card">
20// // {/* <GroupFilters /> */}
21// // <GroupList />
22// // <GroupPagination />
23// // </div>
24// // </div>
25// // );
26// // };
27
28// // export default InterestGroup;
29
30// import React, { useEffect, useState } from 'react';
31// import Header from '../../components/Header';
32// import { useGroupStore } from '../../context/useGroupStore';
33// import GroupList from './GroupList';
34// import GroupPagination from './GroupPagination';
35// import './InterestGroup.css';
36// import axios from 'axios';
37
38// const InterestGroup = () => {
39// const { fetchGroupList } = useGroupStore();
40
41// const [showModal, setShowModal] = useState(false);
42// const [groupName, setGroupName] = useState('');
43// const [groupDescription, setGroupDescription] = useState('');
44
45// useEffect(() => {
46// fetchGroupList();
47// }, [fetchGroupList]);
48
49// const handleCreateGroup = async () => {
50// try {
51// const res = await axios.post('http://localhost:3011/echo/groups/createGroup', {
52// user_id: 1,
53// group_name: groupName, // ✅ 改为 snake_case
54// description: groupDescription,
55// time: new Date().toISOString(),
56// category: '默认分类',
57// cover_image: 'https://picsum.photos/300/200',
58// });
59
60// if (res.status === 200 && res.data.status === 'success') {
61// alert('小组创建成功');
62// setShowModal(false);
63// setGroupName('');
64// setGroupDescription('');
65// fetchGroupList();
66// } else {
67// alert('创建失败: ' + res.data.message);
68// }
69// } catch (error) {
70// alert('创建失败,请检查网络或输入');
71// console.error(error);
72// }
73// };
74
75
76// // const handleCreateGroup = async () => {
77// // try {
78// // const res = await axios.post('/createGroup', {
79// // groupName,
80// // description: groupDescription,
81// // });
82// // if (res.status === 200) {
83// // alert('小组创建成功');
84// // setShowModal(false);
85// // setGroupName('');
86// // setGroupDescription('');
87// // fetchGroupList(); // 刷新列表
88// // }
89// // } catch (error) {
90// // alert('创建失败,请重试');
91// // }
92// // };
93
94// return (
95// <div className="interest-group-container">
96// <Header />
97// <div className="interest-group-card">
98// <button className="create-group-btn" onClick={() => setShowModal(true)}>
99// 创建小组
100// </button>
101
102// {showModal && (
103// <div className="modal-overlay">
104// <div className="modal-content">
105// <h2>创建新小组</h2>
106// <input
107// type="text"
108// placeholder="小组名称"
109// value={groupName}
110// onChange={(e) => setGroupName(e.target.value)}
111// />
112// <textarea
113// placeholder="小组简介"
114// value={groupDescription}
115// onChange={(e) => setGroupDescription(e.target.value)}
116// />
117// <div className="modal-buttons">
118// <button onClick={handleCreateGroup}>确定</button>
119// <button onClick={() => setShowModal(false)}>取消</button>
120// </div>
121// </div>
122// </div>
123// )}
124
125// <GroupList />
126// <GroupPagination />
127// </div>
128// </div>
129// );
130// };
131
132// export default InterestGroup;
133
134
135import React, { useEffect, useState } from 'react';
Krishyab5ef96d2025-06-05 13:57:05 +0800136import Header from '../../components/Header';
137import { useGroupStore } from '../../context/useGroupStore';
Krishyab5ef96d2025-06-05 13:57:05 +0800138import GroupList from './GroupList';
139import GroupPagination from './GroupPagination';
223010093a876cc2025-04-14 16:22:20 +0800140import './InterestGroup.css';
Krishya73cd8822025-06-07 15:48:41 +0800141import axios from 'axios';
223010093a876cc2025-04-14 16:22:20 +0800142
Krishya73cd8822025-06-07 15:48:41 +0800143const InterestGroup = () => {
144 const { fetchGroupList } = useGroupStore();
145
146 const [showModal, setShowModal] = useState(false);
147 const [groupName, setGroupName] = useState('');
148 const [groupDescription, setGroupDescription] = useState('');
149
223010093a876cc2025-04-14 16:22:20 +0800150 useEffect(() => {
Krishyab5ef96d2025-06-05 13:57:05 +0800151 fetchGroupList();
152 }, [fetchGroupList]);
Krishyac0f7e9b2025-04-22 15:28:28 +0800153
Krishya73cd8822025-06-07 15:48:41 +0800154 const handleCreateGroup = async () => {
155 try {
156 // ✅ 修改请求体字段名,使用驼峰命名法
157 const res = await axios.post('http://localhost:3011/echo/groups/createGroup', {
158 userId: 1, // 改为驼峰命名 userId
159 groupName: groupName, // 改为驼峰命名 groupName
160 description: groupDescription,
161 // 移除time字段,使用后端生成的时间
162 memberCount: 1, // 添加初始成员数
163 category: '默认分类',
164 coverImage: 'https://picsum.photos/300/200', // 改为驼峰命名 coverImage
165 });
166
167 if (res.status === 200 && res.data.status === 'success') {
168 alert('小组创建成功');
169 setShowModal(false);
170 setGroupName('');
171 setGroupDescription('');
172 fetchGroupList(); // 刷新列表
173 } else {
174 // 显示后端返回的详细错误信息
175 alert('创建失败: ' + res.data.message);
176 }
177 } catch (error) {
178 // 处理网络错误或其他异常
179 alert('创建失败,请检查网络连接或输入内容');
180 console.error('创建小组错误:', error);
181 }
182 };
183
223010093a876cc2025-04-14 16:22:20 +0800184 return (
185 <div className="interest-group-container">
223010093a876cc2025-04-14 16:22:20 +0800186 <Header />
Krishyac0f7e9b2025-04-22 15:28:28 +0800187 <div className="interest-group-card">
Krishya73cd8822025-06-07 15:48:41 +0800188 <button className="create-group-btn" onClick={() => setShowModal(true)}>
189 创建小组
190 </button>
191
192 {showModal && (
193 <div className="modal-overlay">
194 <div className="modal-content">
195 <h2>创建新小组</h2>
196 <input
197 type="text"
198 placeholder="小组名称"
199 value={groupName}
200 onChange={(e) => setGroupName(e.target.value)}
201 />
202 <textarea
203 placeholder="小组简介"
204 value={groupDescription}
205 onChange={(e) => setGroupDescription(e.target.value)}
206 />
207 <div className="modal-buttons">
208 <button onClick={handleCreateGroup}>确定</button>
209 <button onClick={() => setShowModal(false)}>取消</button>
210 </div>
211 </div>
212 </div>
213 )}
214
Krishyab5ef96d2025-06-05 13:57:05 +0800215 <GroupList />
216 <GroupPagination />
223010093a876cc2025-04-14 16:22:20 +0800217 </div>
218 </div>
219 );
220};
221
Krishyac0f7e9b2025-04-22 15:28:28 +0800222export default InterestGroup;