feat: 支持用户删除自己的评论
- CommentItem: 添加删除按钮(仅显示在自己的评论上) - CommentItem: 添加删除确认对话框,防止误删 - CommentList: 传递 currentUserId 和 onDelete 到 CommentItem - EventCommentSection: 添加 handleDeleteComment 处理函数 - mock handler: 使用真实登录用户信息创建评论 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -144,8 +144,9 @@ const EventCommentSection: React.FC<EventCommentSectionProps> = ({ eventId }) =>
|
||||
content_type: 'text',
|
||||
author: {
|
||||
id: user?.id || 'current_user',
|
||||
username: user?.username || '当前用户',
|
||||
avatar: user?.avatar || null,
|
||||
// 与导航区保持一致:优先显示昵称
|
||||
username: user?.nickname || user?.username || user?.email || '当前用户',
|
||||
avatar: user?.avatar_url || null,
|
||||
},
|
||||
created_at: new Date().toISOString(),
|
||||
likes_count: 0,
|
||||
@@ -187,6 +188,51 @@ const EventCommentSection: React.FC<EventCommentSectionProps> = ({ eventId }) =>
|
||||
}
|
||||
}, [eventId, commentText, toast, user, setComments, setTotalCount]);
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*/
|
||||
const handleDeleteComment = useCallback(async (commentId: string | number) => {
|
||||
try {
|
||||
const result = await eventService.deletePost(commentId);
|
||||
|
||||
if (result.success) {
|
||||
// 从本地 state 中移除该评论
|
||||
setComments((prevComments) =>
|
||||
prevComments.filter((comment) => comment.id !== commentId)
|
||||
);
|
||||
// 总评论数 -1
|
||||
setTotalCount((prevTotal) => Math.max(0, prevTotal - 1));
|
||||
|
||||
toast({
|
||||
title: '评论已删除',
|
||||
status: 'success',
|
||||
duration: 2000,
|
||||
isClosable: true,
|
||||
});
|
||||
|
||||
logger.info('EventCommentSection', '评论删除成功', {
|
||||
eventId,
|
||||
commentId,
|
||||
});
|
||||
} else {
|
||||
throw new Error(result.message || '删除评论失败');
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error('EventCommentSection', 'handleDeleteComment', error, {
|
||||
eventId,
|
||||
commentId,
|
||||
});
|
||||
toast({
|
||||
title: '删除评论失败',
|
||||
description: error.message || '请稍后重试',
|
||||
status: 'error',
|
||||
duration: 3000,
|
||||
isClosable: true,
|
||||
});
|
||||
throw error; // 重新抛出让 CommentItem 知道删除失败
|
||||
}
|
||||
}, [eventId, toast, setComments, setTotalCount]);
|
||||
|
||||
return (
|
||||
<Box>
|
||||
{/* 标题栏 */}
|
||||
@@ -203,7 +249,12 @@ const EventCommentSection: React.FC<EventCommentSectionProps> = ({ eventId }) =>
|
||||
|
||||
{/* 评论列表 */}
|
||||
<Box mb={4}>
|
||||
<CommentList comments={comments} loading={loading} />
|
||||
<CommentList
|
||||
comments={comments}
|
||||
loading={loading}
|
||||
currentUserId={user?.id}
|
||||
onDelete={handleDeleteComment}
|
||||
/>
|
||||
</Box>
|
||||
|
||||
{/* 加载更多按钮(仅当有更多评论时显示) */}
|
||||
|
||||
Reference in New Issue
Block a user