|
|
@@ -1,79 +1,174 @@
|
|
|
-import { Image, type PaginationProps } from 'antd';
|
|
|
+import { Empty } from 'antd';
|
|
|
import './indes.css';
|
|
|
-import { LikeOutlined } from '@ant-design/icons';
|
|
|
import { useEffect, useState } from 'react';
|
|
|
-import { useLocation, useParams } from 'react-router-dom';
|
|
|
+import CommentItem from '../CommentItem';
|
|
|
+import { getCommentListByIdType, addLikeCount, cancelLikeCount } from '@/apis/comment';
|
|
|
|
|
|
interface CommentListProps {
|
|
|
- mvId?: string;
|
|
|
+ contentId?: number;
|
|
|
+ type?: number
|
|
|
}
|
|
|
|
|
|
-interface Comment {
|
|
|
+// API 响应的数据结构
|
|
|
+interface ApiComment {
|
|
|
id: number;
|
|
|
+ userId: number;
|
|
|
+ contentType: number;
|
|
|
src: string;
|
|
|
username: string;
|
|
|
+ contentId: number;
|
|
|
+ parentId: number | null;
|
|
|
content: string;
|
|
|
- time: string;
|
|
|
- like: number;
|
|
|
+ likeCount: number;
|
|
|
+ replyCount: number;
|
|
|
+ status: number;
|
|
|
+ createTime: string;
|
|
|
+ updateTime: string;
|
|
|
}
|
|
|
|
|
|
-const Rank_Recommend_body_list_Comment_list = ({ mvId }: CommentListProps) => {
|
|
|
- const location = useLocation();
|
|
|
- const params = useParams<{ id: string }>();
|
|
|
- const actualMvId = mvId || params.id;
|
|
|
- const [comments, setComments] = useState<Comment[]>([]);
|
|
|
- const [loading, setLoading] = useState(true);
|
|
|
+interface ApiResponse {
|
|
|
+ code: number;
|
|
|
+ message: string;
|
|
|
+ data: ApiComment[];
|
|
|
+}
|
|
|
|
|
|
- // 获取MV信息
|
|
|
- const mvData = location.state?.mv || null;
|
|
|
+const Rank_Recommend_body_list_Comment_list = ({ contentId, type }: CommentListProps) => {
|
|
|
+ const actualMvId = contentId;
|
|
|
+ const [comments, setComments] = useState<ApiComment[]>([]);
|
|
|
+ const [loading, setLoading] = useState(true);
|
|
|
|
|
|
useEffect(() => {
|
|
|
const fetchComments = async () => {
|
|
|
if (actualMvId) {
|
|
|
try {
|
|
|
- const mockComments: Comment[] = [
|
|
|
- {
|
|
|
- id: 0,
|
|
|
- src: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
- username: '爱音乐的用户',
|
|
|
- content: `对 "${mvData?.mvName || '这个MV'}" 的精彩评论`,
|
|
|
- time: '2024-01-04',
|
|
|
- like: 15,
|
|
|
- },
|
|
|
- {
|
|
|
- id: 1,
|
|
|
- src: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
|
- username: '音乐爱好者',
|
|
|
- content: '这个MV真的很好看,制作精良!',
|
|
|
- time: '2024-01-03',
|
|
|
- like: 8,
|
|
|
- },
|
|
|
- ];
|
|
|
-
|
|
|
- setComments(mockComments);
|
|
|
+ const res: ApiResponse = await getCommentListByIdType({
|
|
|
+ contentId: actualMvId,
|
|
|
+ type
|
|
|
+ });
|
|
|
+ setComments(res.data);
|
|
|
} catch (error) {
|
|
|
console.error('获取评论失败:', error);
|
|
|
} finally {
|
|
|
setLoading(false);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ setLoading(false); // 如果没有 contentId,也设置为非加载状态
|
|
|
}
|
|
|
};
|
|
|
|
|
|
fetchComments();
|
|
|
- }, [actualMvId, mvData?.mvName]);
|
|
|
-
|
|
|
- const itemRender: PaginationProps['itemRender'] = (
|
|
|
- _,
|
|
|
- type,
|
|
|
- originalElement
|
|
|
- ) => {
|
|
|
- if (type === 'prev') {
|
|
|
- return <a>上一页</a>;
|
|
|
+ }, [actualMvId, type]);
|
|
|
+
|
|
|
+ const [activeCommentBox, setActiveCommentBox] = useState<{ type: 'reply' | 'main', id?: number } | null>(null);
|
|
|
+
|
|
|
+ const toggleReply = (id: number) => {
|
|
|
+ if (activeCommentBox && activeCommentBox.type === 'reply' && activeCommentBox.id === id) {
|
|
|
+ setActiveCommentBox(null);
|
|
|
+ } else {
|
|
|
+ // 否则显示这个回复框
|
|
|
+ setActiveCommentBox({ type: 'reply', id });
|
|
|
}
|
|
|
- if (type === 'next') {
|
|
|
- return <a>下一页</a>;
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleReplySubmit = () => {
|
|
|
+ setActiveCommentBox(null);
|
|
|
+ // 提交回复后重新获取评论列表
|
|
|
+ const fetchComments = async () => {
|
|
|
+ if (actualMvId) {
|
|
|
+ try {
|
|
|
+ setLoading(true);
|
|
|
+ const res: ApiResponse = await getCommentListByIdType({
|
|
|
+ contentId: actualMvId,
|
|
|
+ type
|
|
|
+ });
|
|
|
+ setComments(res.data);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取评论失败:', error);
|
|
|
+ } finally {
|
|
|
+ setLoading(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ fetchComments();
|
|
|
+ };
|
|
|
+
|
|
|
+ // 删除评论后的处理
|
|
|
+ const handleCommentDeleted = (commentId: number) => {
|
|
|
+ // 重新获取评论列表以更新状态
|
|
|
+ const fetchComments = async () => {
|
|
|
+ if (actualMvId) {
|
|
|
+ try {
|
|
|
+ setLoading(true);
|
|
|
+ const res: ApiResponse = await getCommentListByIdType({
|
|
|
+ contentId: actualMvId,
|
|
|
+ type
|
|
|
+ });
|
|
|
+ setComments(res.data);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取评论失败:', error);
|
|
|
+ } finally {
|
|
|
+ setLoading(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ fetchComments();
|
|
|
+ };
|
|
|
+
|
|
|
+ // 处理点赞功能
|
|
|
+ const handleLike = async (commentId: number) => {
|
|
|
+ try {
|
|
|
+ // 调用API进行点赞
|
|
|
+ const res = await addLikeCount(commentId);
|
|
|
+
|
|
|
+ // 更新本地状态
|
|
|
+ setComments(prevComments =>
|
|
|
+ prevComments.map(comment =>
|
|
|
+ comment.id === commentId
|
|
|
+ ? { ...comment, likeCount: comment.likeCount + 1 }
|
|
|
+ : comment
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ console.log('点赞成功:', res);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('点赞失败:', error);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 处理取消点赞功能
|
|
|
+ const handleCancelLike = async (commentId: number) => {
|
|
|
+ try {
|
|
|
+ // 调用API取消点赞
|
|
|
+ const res = await cancelLikeCount(commentId);
|
|
|
+
|
|
|
+ // 更新本地状态
|
|
|
+ setComments(prevComments =>
|
|
|
+ prevComments.map(comment =>
|
|
|
+ comment.id === commentId
|
|
|
+ ? { ...comment, likeCount: Math.max(0, comment.likeCount - 1) }
|
|
|
+ : comment
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ console.log('取消点赞成功:', res);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('取消点赞失败:', error);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 点赞按钮状态管理
|
|
|
+ const [likedComments, setLikedComments] = useState<number[]>([]);
|
|
|
+
|
|
|
+ const toggleLike = async (commentId: number) => {
|
|
|
+ if (likedComments.includes(commentId)) {
|
|
|
+ // 如果已经点赞,取消点赞
|
|
|
+ await handleCancelLike(commentId);
|
|
|
+ setLikedComments(prev => prev.filter(id => id !== commentId));
|
|
|
+ } else {
|
|
|
+ // 如果没有点赞,进行点赞
|
|
|
+ await handleLike(commentId);
|
|
|
+ setLikedComments(prev => [...prev, commentId]);
|
|
|
}
|
|
|
- return originalElement;
|
|
|
};
|
|
|
|
|
|
if (loading) {
|
|
|
@@ -85,42 +180,32 @@ const Rank_Recommend_body_list_Comment_list = ({ mvId }: CommentListProps) => {
|
|
|
<div className="Rank_Recommend_body_list_Comment_list_ttitle">
|
|
|
精彩评论 ({comments.length})
|
|
|
</div>
|
|
|
- {comments.map((item, index) => (
|
|
|
- <div
|
|
|
- className="Rank_Recommend_body_list_Comment_list_content"
|
|
|
- key={`${item.id}-${index}`}
|
|
|
- >
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_left">
|
|
|
- <Image width={50} src={item.src} preview={false} />
|
|
|
- </div>
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right">
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_top">
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_top_username">
|
|
|
- {item.username}
|
|
|
- </div>
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_top_content">
|
|
|
- {item.content}
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_bottom">
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_bottom_time">
|
|
|
- {item.time}
|
|
|
- </div>
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_bottom_right">
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_bottom_right_like">
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_bottom_right_like_icon">
|
|
|
- <LikeOutlined />
|
|
|
- {item.like === 0 ? '' : item.like}
|
|
|
- </div>
|
|
|
- <div className="Rank_Recommend_body_list_Comment_list_content_right_bottom_right_like_button">
|
|
|
- 回复
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
+
|
|
|
+ {comments.length > 0 ? (
|
|
|
+ comments.map((item, index) => (
|
|
|
+ <div
|
|
|
+ className="Rank_Recommend_body_list_Comment_list_content"
|
|
|
+ key={`${item.id}-${index}`}
|
|
|
+ >
|
|
|
+ <CommentItem
|
|
|
+ item={item}
|
|
|
+ contentId={actualMvId}
|
|
|
+ type={type}
|
|
|
+ level={0}
|
|
|
+ likedComments={likedComments}
|
|
|
+ activeCommentBox={activeCommentBox}
|
|
|
+ onToggleLike={toggleLike}
|
|
|
+ onToggleReply={toggleReply}
|
|
|
+ onReplySubmit={handleReplySubmit}
|
|
|
+ onCommentDeleted={handleCommentDeleted}
|
|
|
+ />
|
|
|
</div>
|
|
|
+ ))
|
|
|
+ ) : (
|
|
|
+ <div style={{ padding: '20px 0' }}>
|
|
|
+ <Empty description="暂无评论" />
|
|
|
</div>
|
|
|
- ))}
|
|
|
+ )}
|
|
|
</div>
|
|
|
);
|
|
|
};
|