ym923 | 4558e9f | 2025-06-09 20:16:16 +0800 | [diff] [blame^] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { |
| 3 | likePost, |
| 4 | unlikePost, |
| 5 | togglePinPost, |
| 6 | deletePost, |
| 7 | } from '../api/post'; |
| 8 | |
| 9 | const PostCard = ({ post, onDeleted, onUpdated }) => { |
| 10 | const [likes, setLikes] = useState(post.likes); |
| 11 | const [isLiked, setIsLiked] = useState(false); // 默认未点赞 |
| 12 | const [isPinned, setIsPinned] = useState(post.is_pinned || false); |
| 13 | const [loading, setLoading] = useState(false); |
| 14 | |
| 15 | useEffect(() => { |
| 16 | setIsPinned(post.is_pinned || false); |
| 17 | }, [post.is_pinned]); |
| 18 | |
| 19 | const handleLike = async () => { |
| 20 | setLoading(true); |
| 21 | try { |
| 22 | if (!isLiked) { |
| 23 | const res = await likePost(post.postid); |
| 24 | if (res) { |
| 25 | setLikes(likes + 1); |
| 26 | setIsLiked(true); |
| 27 | } |
| 28 | } else { |
| 29 | const res = await unlikePost(post.postid); |
| 30 | if (res) { |
| 31 | setLikes(likes - 1); |
| 32 | setIsLiked(false); |
| 33 | } |
| 34 | } |
| 35 | } catch (error) { |
| 36 | console.error('点赞操作失败', error); |
| 37 | } |
| 38 | setLoading(false); |
| 39 | }; |
| 40 | |
| 41 | const handlePin = async () => { |
| 42 | setLoading(true); |
| 43 | try { |
| 44 | const res = await togglePinPost(post.postid); |
| 45 | if (res) { |
| 46 | onUpdated && onUpdated(); // 通知父组件刷新数据 |
| 47 | } |
| 48 | } catch (error) { |
| 49 | console.error('置顶切换失败', error); |
| 50 | } |
| 51 | setLoading(false); |
| 52 | }; |
| 53 | |
| 54 | const handleDelete = async () => { |
| 55 | if (!window.confirm('确认删除该帖子吗?')) return; |
| 56 | setLoading(true); |
| 57 | try { |
| 58 | const res = await deletePost(post.postid); |
| 59 | if (res) { |
| 60 | onDeleted && onDeleted(post.postid); |
| 61 | } |
| 62 | } catch (error) { |
| 63 | console.error('删除失败', error); |
| 64 | } |
| 65 | setLoading(false); |
| 66 | }; |
| 67 | |
| 68 | return ( |
| 69 | <div className="post-card" style={{ border: '1px solid #ddd', padding: 16, marginBottom: 12 }}> |
| 70 | <h3>{post.postTitle}</h3> |
| 71 | <p>{post.postContent}</p> |
| 72 | {post.photo && ( |
| 73 | <img |
| 74 | src={`http://localhost:8080${post.photo}`} |
| 75 | alt="帖子图片" |
| 76 | style={{ maxWidth: '100%', maxHeight: 200 }} |
| 77 | /> |
| 78 | )} |
| 79 | <p> |
| 80 | <strong>标签:</strong> {post.tags || '无'} |
| 81 | </p> |
| 82 | <p> |
| 83 | <strong>作者ID:</strong> {post.userid} | <strong>点赞数:</strong> {likes} |
| 84 | </p> |
| 85 | <p> |
| 86 | <strong>发布时间:</strong> {new Date(post.postCreatedTime).toLocaleString()} |
| 87 | </p> |
| 88 | <p> |
| 89 | <button onClick={handleLike} disabled={loading}> |
| 90 | {isLiked ? '取消点赞' : '点赞'} |
| 91 | </button>{' '} |
| 92 | <button onClick={handlePin} disabled={loading}> |
| 93 | {isPinned ? '取消置顶' : '置顶'} |
| 94 | </button>{' '} |
| 95 | <button onClick={handleDelete} disabled={loading}> |
| 96 | 删除 |
| 97 | </button> |
| 98 | </p> |
| 99 | </div> |
| 100 | ); |
| 101 | }; |
| 102 | |
| 103 | export default PostCard; |