Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 1 | // src/pages/Forum/CreatePost.jsx |
| 2 | import React, { useState } from 'react'; |
| 3 | import axios from 'axios'; |
| 4 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 5 | const API_BASE = process.env.REACT_APP_API_BASE; |
| 6 | |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 7 | const CreatePost = ({ userId }) => { |
| 8 | const [title, setTitle] = useState(''); |
| 9 | const [content, setContent] = useState(''); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 10 | const [imgUrl, setImageUrl] = useState(''); |
| 11 | const [isAnonymous, setIsAnonymous] = useState(false); |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 12 | |
| 13 | const handleSubmit = async (e) => { |
| 14 | e.preventDefault(); |
| 15 | |
| 16 | try { |
| 17 | const postData = { |
| 18 | title, |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 19 | postContent: content, |
| 20 | postType: isAnonymous, |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 21 | }; |
| 22 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 23 | if (imgUrl.trim()) { |
| 24 | postData.imgUrl = imgUrl; |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | const response = await axios.post( |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 28 | `${API_BASE}/echo/forum/posts/${userId}/createPost`, |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 29 | postData |
| 30 | ); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 31 | |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 32 | |
| 33 | if (response.status === 201) { |
| 34 | alert('帖子创建成功!'); |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 35 | setTitle(''); |
| 36 | setContent(''); |
| 37 | setImageUrl(''); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 38 | setIsAnonymous(false); |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 39 | } |
| 40 | } catch (error) { |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 41 | console.error('帖子创建失败:', error.response?.data || error.message); |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 42 | alert('创建失败,请重试'); |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 43 | } |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | return ( |
| 47 | <div className="create-post"> |
| 48 | <h2>创建新帖子</h2> |
| 49 | <form onSubmit={handleSubmit}> |
| 50 | <div> |
| 51 | <label>标题:</label> |
| 52 | <input |
| 53 | type="text" |
| 54 | value={title} |
| 55 | onChange={(e) => setTitle(e.target.value)} |
| 56 | required |
| 57 | /> |
| 58 | </div> |
| 59 | <div> |
| 60 | <label>内容:</label> |
| 61 | <textarea |
| 62 | value={content} |
| 63 | onChange={(e) => setContent(e.target.value)} |
| 64 | required |
| 65 | /> |
| 66 | </div> |
| 67 | <div> |
| 68 | <label>图片 URL(可选):</label> |
| 69 | <input |
| 70 | type="text" |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 71 | value={imgUrl} |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 72 | onChange={(e) => setImageUrl(e.target.value)} |
| 73 | /> |
| 74 | </div> |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 75 | <div> |
| 76 | <label> |
| 77 | <input |
| 78 | type="checkbox" |
| 79 | checked={isAnonymous} |
| 80 | onChange={(e) => setIsAnonymous(e.target.checked)} |
| 81 | /> |
| 82 | 匿名发布 |
| 83 | </label> |
| 84 | </div> |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 85 | <button type="submit">发布</button> |
| 86 | </form> |
| 87 | </div> |
| 88 | ); |
| 89 | }; |
| 90 | |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame^] | 91 | export default CreatePost; |