blob: 6848b548968941870a980006d44f4e68d3915d58 [file] [log] [blame]
Krishyab5ef96d2025-06-05 13:57:05 +08001import React, { useState } from 'react';
2import { useGroupStore } from '../../context/useGroupStore';
3
4const CreatePostForm = ({ groupId, onClose }) => {
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
12 const handleSubmit = async (e) => {
13 e.preventDefault();
14 setLoading(true);
15 setError('');
16
17 try {
18 const success = await handleCreatePost(groupId, userId, content, title, images);
19
20 if (success) {
21 alert('帖子发布成功');
22 onClose();
23 } else {
24 setError('帖子发布失败');
25 }
26 } catch (error) {
27 setError(error.message || '帖子发布失败');
28 } finally {
29 setLoading(false);
30 }
31 };
32
33 return (
34 <div className="create-post-form">
35 <h4>发布新帖子</h4>
36 <input
37 type="text"
38 placeholder="帖子标题"
39 value={title}
40 onChange={(e) => setTitle(e.target.value)}
41 required
42 />
43 <textarea
44 placeholder="帖子内容"
45 value={content}
46 onChange={(e) => setContent(e.target.value)}
47 required
48 />
49 <input
50 type="file"
51 multiple
52 onChange={(e) => setImages(e.target.files)}
53 />
54
55 {error && <p className="error">{error}</p>}
56
57 <div className="button-group">
58 <button onClick={handleSubmit} disabled={loading}>
59 {loading ? '发布中...' : '发布'}
60 </button>
61 <button onClick={onClose} disabled={loading}>
62 取消
63 </button>
64 </div>
65 </div>
66 );
67};
68
69export default CreatePostForm;