blob: 45f170e2c06e5956a350ddcfbcc6fff6b8351b9b [file] [log] [blame]
Krishyae71688a2025-04-10 21:25:17 +08001import React, { useState, useEffect } from'react';
2import axios from 'axios';
3
4const EditPost = ({ post_id }) => {
5 const [title, setTitle] = useState('');
6 const [postContent, setPostContent] = useState('');
7 const [imgUrl, setImgUrl] = useState('');
8
9 useEffect(() => {
10 const fetchPost = async () => {
11 try {
12 const response = await axios.get(`/echo/forum/posts/${post_id}`);
13 setTitle(response.data.title);
14 setPostContent(response.data.Postcontent);
15 setImgUrl(response.data.imgUrl);
16 } catch (error) {
17 console.error('Error fetching post for edit:', error);
18 }
19 };
20 fetchPost();
21 }, [post_id]);
22
23 const handleSubmit = async (e) => {
24 e.preventDefault();
25 try {
26 await axios.put(`/echo/forum/posts/${post_id}/editPost`, {
27 title,
28 Postcontent: postContent,
29 imgUrl
30 });
31 console.log('帖子更新成功');
32 // 可添加其他逻辑,如跳转等
33 } catch (error) {
34 console.error('Error editing post:', error);
35 }
36 };
37
38 return (
39 <form onSubmit={handleSubmit}>
40 <input
41 type="text"
42 placeholder="标题"
43 value={title}
44 onChange={(e) => setTitle(e.target.value)}
45 />
46 <textarea
47 placeholder="内容"
48 value={postContent}
49 onChange={(e) => setPostContent(e.target.value)}
50 />
51 <input
52 type="text"
53 placeholder="图片URL"
54 value={imgUrl}
55 onChange={(e) => setImgUrl(e.target.value)}
56 />
57 <button type="submit">更新帖子</button>
58 </form>
59 );
60};
61
62export default EditPost;