刘嘉昕 | 9cdeca2 | 2025-06-03 16:54:30 +0800 | [diff] [blame] | 1 | // src/components/Comment.jsx |
| 2 | import React, { useState, useEffect } from 'react'; |
| 3 | import { |
| 4 | getCommentsByPostId, |
| 5 | createComment, |
| 6 | deleteComment, |
| 7 | likeComment, |
| 8 | unlikeComment, |
| 9 | } from '../api/comment'; |
| 10 | |
| 11 | const Comment = ({ postId, currentUser }) => { |
| 12 | const [comments, setComments] = useState([]); |
| 13 | const [newContent, setNewContent] = useState(''); |
| 14 | |
| 15 | useEffect(() => { |
| 16 | loadComments(); |
| 17 | }, [postId]); |
| 18 | |
| 19 | const loadComments = async () => { |
| 20 | const data = await getCommentsByPostId(postId); |
| 21 | setComments(data); |
| 22 | }; |
| 23 | |
| 24 | const handleCreate = async () => { |
| 25 | if (!newContent.trim()) return; |
| 26 | |
| 27 | const commentData = { |
| 28 | postid: postId, |
| 29 | userid: currentUser.id, |
| 30 | postCommentcontent: newContent, |
| 31 | commenttime: new Date().toISOString() |
| 32 | }; |
| 33 | |
| 34 | await createComment(commentData); |
| 35 | setNewContent(''); |
| 36 | loadComments(); |
| 37 | }; |
| 38 | |
| 39 | const handleDelete = async (commentid) => { |
| 40 | await deleteComment(commentid); |
| 41 | loadComments(); |
| 42 | }; |
| 43 | |
| 44 | const handleLike = async (commentid) => { |
| 45 | await likeComment(commentid); |
| 46 | loadComments(); |
| 47 | }; |
| 48 | |
| 49 | const handleUnlike = async (commentid) => { |
| 50 | await unlikeComment(commentid); |
| 51 | loadComments(); |
| 52 | }; |
| 53 | |
| 54 | return ( |
| 55 | <div> |
| 56 | <h4 className="font-semibold text-gray-700 mb-2">评论</h4> |
| 57 | <div className="mb-2"> |
| 58 | <textarea |
| 59 | value={newContent} |
| 60 | onChange={(e) => setNewContent(e.target.value)} |
| 61 | placeholder="写下你的评论..." |
| 62 | className="w-full border rounded p-2" |
| 63 | /> |
| 64 | <button |
| 65 | onClick={handleCreate} |
| 66 | className="mt-2 bg-blue-500 text-white px-3 py-1 rounded hover:bg-blue-600" |
| 67 | > |
| 68 | 发布评论 |
| 69 | </button> |
| 70 | </div> |
| 71 | |
| 72 | <div className="space-y-2 mt-4"> |
| 73 | {comments.map((comment) => ( |
| 74 | <div key={comment.commentid} className="border rounded p-2"> |
| 75 | <div className="text-sm text-gray-800 font-medium">用户ID:{comment.userid}</div> |
| 76 | <div className="text-gray-700">{comment.postCommentcontent}</div> |
| 77 | <div className="text-xs text-gray-500 mt-1"> |
| 78 | {comment.commenttime || '暂无时间'} | 👍 {comment.likes} |
| 79 | </div> |
| 80 | <div className="flex gap-2 mt-1 text-sm"> |
| 81 | <button onClick={() => handleLike(comment.commentid)} className="text-blue-500 hover:underline">点赞</button> |
| 82 | <button onClick={() => handleUnlike(comment.commentid)} className="text-yellow-500 hover:underline">取消点赞</button> |
| 83 | {comment.userid === currentUser.id && ( |
| 84 | <button onClick={() => handleDelete(comment.commentid)} className="text-red-500 hover:underline">删除</button> |
| 85 | )} |
| 86 | </div> |
| 87 | </div> |
| 88 | ))} |
| 89 | </div> |
| 90 | </div> |
| 91 | ); |
| 92 | }; |
| 93 | |
| 94 | export default Comment; |