|
|
|
@ -28,6 +28,8 @@ export default function Comment({ journalId, className, totalCommentReply, total
|
|
|
|
|
const handleChangeType = async (type: 'hot' | 'new') => {
|
|
|
|
|
setCommentType(type);
|
|
|
|
|
setPage(1);
|
|
|
|
|
setCommentList([]);
|
|
|
|
|
handleLoadMore(); // 添加这一行
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 发表评论
|
|
|
|
@ -38,25 +40,24 @@ export default function Comment({ journalId, className, totalCommentReply, total
|
|
|
|
|
|
|
|
|
|
// 加载评论
|
|
|
|
|
const handleLoadMore = useCallback(async () => {
|
|
|
|
|
if (commentList.length >= totalCommentReplyInt) return;
|
|
|
|
|
if (loading) return;
|
|
|
|
|
console.log('handleLoadMore');
|
|
|
|
|
if (loading || commentList.length >= totalCommentReplyInt) return;
|
|
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
const res = await apiGetComment({ type: commentType, journalId, page, size: pageSize });
|
|
|
|
|
// 请求失败
|
|
|
|
|
if (res.code !== 200) {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 请求第一页时,先清空列表
|
|
|
|
|
if (page === 1) setCommentList([]);
|
|
|
|
|
if (res.code !== 200) return setLoading(false);
|
|
|
|
|
|
|
|
|
|
// 请求到完整一页后翻页
|
|
|
|
|
if (res.data.rows.length === 5) setPage((prePage) => prePage + 1);
|
|
|
|
|
// 最后一页完整?
|
|
|
|
|
|
|
|
|
|
// 最后一页完整
|
|
|
|
|
const lastPageLength = commentList.length % pageSize;
|
|
|
|
|
if (lastPageLength) setCommentList([...commentList, ...res.data.rows.slice(lastPageLength)]);
|
|
|
|
|
|
|
|
|
|
setCommentList([...commentList, ...res.data.rows]);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}, [commentList, page]);
|
|
|
|
|
}, [commentList, page, commentType]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const observer = new IntersectionObserver((entries) => {
|
|
|
|
@ -81,11 +82,11 @@ export default function Comment({ journalId, className, totalCommentReply, total
|
|
|
|
|
<CommentHeader
|
|
|
|
|
type={commentType}
|
|
|
|
|
count={totalCommentReply}
|
|
|
|
|
onChange={(type: 'hot' | 'new') => handleChangeType(type)}
|
|
|
|
|
onChangeType={(type) => handleChangeType(type)}
|
|
|
|
|
clssName="mt-[25px]"
|
|
|
|
|
/>
|
|
|
|
|
<CommentList commentList={commentList} />
|
|
|
|
|
<div ref={loaderRef} className="h-[200px">
|
|
|
|
|
<div ref={loaderRef} className="h-[200px]">
|
|
|
|
|
{loading && 'loading...'}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|