Skip to content

Commit

Permalink
feat: Signature Comment 무한스크롤 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
dydals3440 committed Feb 17, 2024
1 parent 0325b87 commit 65bab87
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/apis/request/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ const getLikeList = signatureId => {

// 시그니처 댓글 / 답글 불러오기 (무한 스크롤)
const getSignatureComments = (signatureId, take, { pageParam }) => {
const url = `api/v1/signature/${signatureId}/comment?take${take}&cursorId=${pageParam}`;
console.log(url);
const url = `api/v1/signature/${signatureId}/comment?take=${take}&cursorId=${pageParam}`;
return axiosWithToken.get(url);
};

Expand Down
25 changes: 21 additions & 4 deletions src/components/comment/signature/SignatureCommentList.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useEffect } from 'react';
import { useInView } from 'react-intersection-observer';
import { useParams } from 'react-router-dom';
import { ClipLoader } from 'react-spinners';

import * as S from './SignatureCommentList.style';
import SignatureComment from './commentView/SignatureComment';
Expand All @@ -7,20 +10,34 @@ import { useGetSignatureComments } from '@/hooks/signature/queries/useGetSignatu
const SignatureCommentList = () => {
const { signatureId } = useParams();

const { data, isLoading, isPending } = useGetSignatureComments(
signatureId,
2,
);
const { data, isFetching, hasNextPage, fetchNextPage } =
useGetSignatureComments(signatureId, 3);

const signatureComments = data?.pages;

const { ref, inView } = useInView({
threshold: 0,
delay: 0,
});

useEffect(() => {
if (inView) {
console.log('fire');
!isFetching && hasNextPage && fetchNextPage();
}
}, [inView, isFetching, hasNextPage, fetchNextPage]);

return (
<S.Container>
{signatureComments?.map(comments =>
comments?.data?.data?.data?.map(comment => (
<SignatureComment key={comment._id} data={comment} />
)),
)}
<S.LoadingWrapper>
{isFetching && <ClipLoader size={50} color={'#1B9C85'} />}
</S.LoadingWrapper>
<div ref={ref} style={{ height: 5 }} />
</S.Container>
);
};
Expand Down
23 changes: 19 additions & 4 deletions src/components/comment/signature/SignatureCommentList.style.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import styled from 'styled-components';

import theme from '@/theme';

const Container = styled.div`
display: flex;
flex-direction: column;
align-items: center;
${theme.ALIGN.COLUMN_CENTER};
max-height: 500px;
width: 100%;
overflow: scroll;
&::-webkit-scrollbar {
width: 4px;
}
&::-webkit-scrollbar-thumb {
border-radius: 20px;
background: #ccc;
}
`;

const LoadingWrapper = styled.div`
${theme.ALIGN.ROW_CENTER}
height: 20px;
`;

export { Container };
export { Container, LoadingWrapper };
6 changes: 4 additions & 2 deletions src/hooks/signature/queries/useGetSignatureComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const useGetSignatureComments = (signatureId, take) => {
initialPageParam: 0,
getNextPageParam: (lastPage, allPages, lastPageParam) => {
const meta = lastPage?.data?.data?.meta;
if (meta && meta.hasNextData) {
return meta.cursor;
console.log(lastPage);
if (meta?.hasNextData) {
return meta?.cursor;
} else {
return null;
}
},
staleTime: 60 * 1000,
});
};

Expand Down

0 comments on commit 65bab87

Please sign in to comment.