blob: ce80a0a8d517c33bb0fc855c59689421bb088851 [file] [log] [blame]
Krishyaaffe8102025-06-08 00:44:46 +08001// import React, { useState } from 'react';
2// import { useGroupStore } from '../../context/useGroupStore';
3
4// const CreatePostForm = ({ groupId, onClose, onPostCreated }) => {
5// const { userId, handleCreatePost } = useGroupStore();
6// const [title, setTitle] = useState('');
7// const [content, setContent] = useState('');
8// const [images, setImages] = useState([]);
9// const [loading, setLoading] = useState(false);
10// const [error, setError] = useState('');
11// const [formError, setFormError] = useState({});
12
13// // 表单验证
14// const validateForm = () => {
15// const errors = {};
16// let isValid = true;
17
18// if (!title.trim()) {
19// errors.title = '请输入帖子标题';
20// isValid = false;
21// }
22
23// if (!content.trim()) {
24// errors.content = '请输入帖子内容';
25// isValid = false;
26// }
27
28// setFormError(errors);
29// return isValid;
30// };
31
32// const handleSubmit = async (e) => {
33// e.preventDefault();
34
35// // 先进行表单验证
36// if (!validateForm()) {
37// return;
38// }
39
40// console.log('点击发布,准备发送请求');
41// setLoading(true);
42// setError('');
43
44// try {
45// // 检查必要条件
46// if (!groupId) {
47// throw new Error('小组ID缺失');
48// }
49
50// if (!userId) {
51// throw new Error('用户ID缺失,请先登录');
52// }
53
54// // 打印关键变量进行调试
55// console.log('准备发布帖子:', {
56// groupId,
57// userId,
58// title,
59// content,
60// imagesCount: images.length
61// });
62
63// // 调用创建帖子的方法
64// const success = await handleCreatePost(groupId, userId, content, title, images);
65
66// if (success) {
67// alert('帖子发布成功');
68// onPostCreated(); // 触发刷新
69// onClose(); // 关闭弹窗
70// } else {
71// setError('帖子发布失败,请重试');
72// }
73// } catch (error) {
74// console.error('发布帖子错误:', error);
75// setError(error.message || '帖子发布失败');
76// } finally {
77// setLoading(false);
78// }
79// };
80
81// return (
82// <div className="create-post-form">
83// <h4>发布新帖子</h4>
84
85// <div className="form-group">
86// <input
87// type="text"
88// placeholder="帖子标题"
89// value={title}
90// onChange={(e) => setTitle(e.target.value)}
91// required
92// />
93// {formError.title && <p className="error-message">{formError.title}</p>}
94// </div>
95
96// <div className="form-group">
97// <textarea
98// placeholder="帖子内容"
99// value={content}
100// onChange={(e) => setContent(e.target.value)}
101// required
102// />
103// {formError.content && <p className="error-message">{formError.content}</p>}
104// </div>
105
106// <div className="form-group">
107// <input
108// type="file"
109// multiple
110// onChange={(e) => setImages(e.target.files)}
111// />
112// </div>
113
114// {error && <p className="error-message">{error}</p>}
115
116// <div className="button-group">
117// <button onClick={handleSubmit} disabled={loading}>
118// {loading ? '发布中...' : '发布'}
119// </button>
120// <button onClick={onClose} disabled={loading}>
121// 取消
122// </button>
123// </div>
124// </div>
125// );
126// };
127
128// export default CreatePostForm;
129
130import React, { useState, useEffect } from 'react';
131import { useUser } from '../../context/UserContext';
Krishyab5ef96d2025-06-05 13:57:05 +0800132import { useGroupStore } from '../../context/useGroupStore';
133
Krishyaaffe8102025-06-08 00:44:46 +0800134const CreatePostForm = ({ groupId, onClose, onPostCreated }) => {
135 const { user, loading: userLoading } = useUser();
136 const { handleCreatePost } = useGroupStore();
137
Krishyab5ef96d2025-06-05 13:57:05 +0800138 const [title, setTitle] = useState('');
139 const [content, setContent] = useState('');
140 const [images, setImages] = useState([]);
141 const [loading, setLoading] = useState(false);
142 const [error, setError] = useState('');
Krishyaaffe8102025-06-08 00:44:46 +0800143 const [formError, setFormError] = useState({});
144
145 // 处理用户状态加载中或未登录的情况
146 if (userLoading) {
147 return <div className="create-post-form loading">加载用户信息...</div>;
148 }
149
150 if (!user) {
151 return (
152 <div className="create-post-form">
153 <div className="error-message">请先登录以发布帖子</div>
154 <button className="close-btn" onClick={onClose}>
155 关闭
156 </button>
157 </div>
158 );
159 }
160
161 // 表单验证
162 const validateForm = () => {
163 const errors = {};
164 let isValid = true;
165
166 if (!title.trim()) {
167 errors.title = '请输入帖子标题';
168 isValid = false;
169 }
170
171 if (!content.trim()) {
172 errors.content = '请输入帖子内容';
173 isValid = false;
174 }
175
176 setFormError(errors);
177 return isValid;
178 };
Krishyab5ef96d2025-06-05 13:57:05 +0800179
180 const handleSubmit = async (e) => {
181 e.preventDefault();
Krishyaaffe8102025-06-08 00:44:46 +0800182
183 if (!validateForm()) {
184 return;
185 }
186
187 console.log('点击发布,准备发送请求');
Krishyab5ef96d2025-06-05 13:57:05 +0800188 setLoading(true);
189 setError('');
190
191 try {
Krishyaaffe8102025-06-08 00:44:46 +0800192 if (!groupId) {
193 throw new Error('小组ID缺失');
194 }
195
196 console.log('准备发布帖子:', {
197 groupId,
198 userId: user.userId,
199 title,
200 content,
201 imagesCount: images.length
202 });
203
204 // 调用创建帖子方法,不再传递 userId 参数
205 const success = await handleCreatePost(groupId, content, title, images);
Krishyab5ef96d2025-06-05 13:57:05 +0800206
207 if (success) {
208 alert('帖子发布成功');
Krishyaaffe8102025-06-08 00:44:46 +0800209 onPostCreated();
Krishyab5ef96d2025-06-05 13:57:05 +0800210 onClose();
211 } else {
Krishyaaffe8102025-06-08 00:44:46 +0800212 setError('帖子发布失败,请重试');
Krishyab5ef96d2025-06-05 13:57:05 +0800213 }
214 } catch (error) {
Krishyaaffe8102025-06-08 00:44:46 +0800215 console.error('发布帖子错误:', error);
Krishyab5ef96d2025-06-05 13:57:05 +0800216 setError(error.message || '帖子发布失败');
217 } finally {
218 setLoading(false);
219 }
220 };
221
222 return (
223 <div className="create-post-form">
224 <h4>发布新帖子</h4>
Krishyab5ef96d2025-06-05 13:57:05 +0800225
Krishyaaffe8102025-06-08 00:44:46 +0800226 <div className="form-group">
227 <input
228 type="text"
229 placeholder="帖子标题"
230 value={title}
231 onChange={(e) => setTitle(e.target.value)}
232 required
233 />
234 {formError.title && <div className="error-message">{formError.title}</div>}
235 </div>
236
237 <div className="form-group">
238 <textarea
239 placeholder="帖子内容"
240 value={content}
241 onChange={(e) => setContent(e.target.value)}
242 required
243 />
244 {formError.content && <div className="error-message">{formError.content}</div>}
245 </div>
246
247 <div className="form-group">
248 <label>上传图片:</label>
249 <input
250 type="file"
251 multiple
252 onChange={(e) => setImages(e.target.files)}
253 />
254 </div>
255
256 {error && <div className="error-message">{error}</div>}
Krishyab5ef96d2025-06-05 13:57:05 +0800257
258 <div className="button-group">
Krishyaaffe8102025-06-08 00:44:46 +0800259 <button className="submit-btn" onClick={handleSubmit} disabled={loading}>
Krishyab5ef96d2025-06-05 13:57:05 +0800260 {loading ? '发布中...' : '发布'}
261 </button>
Krishyaaffe8102025-06-08 00:44:46 +0800262 <button className="cancel-btn" onClick={onClose} disabled={loading}>
Krishyab5ef96d2025-06-05 13:57:05 +0800263 取消
264 </button>
265 </div>
266 </div>
267 );
268};
269
270export default CreatePostForm;