fix(comment): Fix switch commentType have no request.

mack-mac
mackt 7 months ago
parent affdb25789
commit 0636552867

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

@ -2,12 +2,12 @@ export default function CommentHeader({
type, type,
count, count,
clssName, clssName,
onChange, onChangeType,
}: { }: {
type: 'hot' | 'new'; type: 'hot' | 'new';
count: string; count: string;
clssName: string; clssName: string;
onChange: (value: 'hot' | 'new') => void; onChangeType: (value: 'hot' | 'new') => void;
}) { }) {
return ( return (
<div className={`flex flex-row items-center justify-between mb-[10px] ${clssName}`}> <div className={`flex flex-row items-center justify-between mb-[10px] ${clssName}`}>
@ -19,14 +19,14 @@ export default function CommentHeader({
<div className="flex flex-row items-center text-[14px] leading-[19.6px] gap-[12px]"> <div className="flex flex-row items-center text-[14px] leading-[19.6px] gap-[12px]">
<div <div
className={`${type === 'hot' ? 'text-base' : 'text-[rgba(0,0,0,0.4)] hover:text-brand cursor-pointer'}`} className={`${type === 'hot' ? 'text-base' : 'text-[rgba(0,0,0,0.4)] hover:text-brand cursor-pointer'}`}
onClick={() => onChange('hot')} onClick={() => onChangeType('hot')}
> >
</div> </div>
<div className="text-[rgba(0,0,0,0.4)] cursor-pointer hover:text-brand">|</div> <div className="text-[rgba(0,0,0,0.4)] cursor-pointer hover:text-brand">|</div>
<div <div
className={`${type === 'new' ? 'text-base' : 'text-[rgba(0,0,0,0.4)] hover:text-brand cursor-pointer'}`} className={`${type === 'new' ? 'text-base' : 'text-[rgba(0,0,0,0.4)] hover:text-brand cursor-pointer'}`}
onClick={() => onChange('new')} onClick={() => onChangeType('new')}
> >
</div> </div>

Loading…
Cancel
Save