Skip to content

Commit

Permalink
[ feat ] 댓글 페이지 네이션 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Happhee committed Jul 21, 2022
1 parent c5d1bec commit fba492a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
1 change: 0 additions & 1 deletion components/collectionProduct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export { default as PriceFilter } from './PriceFilter';
export { default as PageNavigation } from './PageNavigation';
export { default as ToyPreview } from './ToyPreview';
export { default as CollectionList } from './CollectionList';

61 changes: 47 additions & 14 deletions components/community/ReplyList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../types/community';
import ReplyContent from './ReplyContent';
import { useRouter } from 'next/router';
import { PageNavigation } from '../common/index';

interface ReplyListProps {
cid: string;
Expand All @@ -23,6 +24,8 @@ export default function ReplyList(props: ReplyListProps) {
boardId: '',
content: '',
});
const [currentPage, setCurrentPage] = useState<number>(1);
const [pageReplyList, setPageReplyList] = useState<ReplyData[]>([]);

const handleInputText = (e: React.ChangeEvent<HTMLInputElement>) => {
setReplyText(e.target.value);
Expand All @@ -47,7 +50,20 @@ export default function ReplyList(props: ReplyListProps) {
});
router.push(`/community/${data.id}`);
};
const handleCurrentPage = (nextPage: number) => {
setCurrentPage(nextPage);
};

useEffect(() => {
if (replyList) {
setPageReplyList(
replyList.filter(
(_, idx) => (currentPage - 1) * 10 <= idx && idx < currentPage * 10,
),
);
window.scrollTo({ top: 750 });
}
}, [replyList, currentPage]);
return (
<>
<StReplyTitle>
Expand All @@ -68,20 +84,42 @@ export default function ReplyList(props: ReplyListProps) {
</StInputBtn>
</StInputForm>
<StReplyWrapper>
{replyList.map((reply, idx) => (
<ReplyContent
key={idx}
author={reply.author}
userNickname={reply.userNickname}
content={reply.content}
createdAt={reply.createdAt}
/>
))}
{pageReplyList.map(
({ author, userNickname, content, createdAt }, idx) => (
<ReplyContent
key={idx}
author={author}
userNickname={userNickname}
content={content}
createdAt={createdAt}
/>
),
)}
</StReplyWrapper>
<StReplyListNav>
{replyList && (
<PageNavigation
currentPage={currentPage}
lastPage={Math.ceil(replyList.length / 10)}
handleCurrentPage={handleCurrentPage}
/>
)}
</StReplyListNav>
</>
);
}

const StReplyWrapper = styled.section`
display: flex;
flex-direction: column;
justify-content: flex-start;
`;
const StReplyListNav = styled.nav`
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
`;
const StReplyTitle = styled.article`
display: flex;
flex-direction: row;
Expand Down Expand Up @@ -158,8 +196,3 @@ const StInputBtn = styled.span<{ inputColor: boolean }>`
cursor: pointer;
`;
const StReplyWrapper = styled.section`
display: flex;
flex-direction: column;
justify-content: flex-start;
`;
3 changes: 0 additions & 3 deletions pages/community/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { getCommunity } from '../../core/api/community';
import { communityMockData } from '../../mocks/data/communityMockData';

import { PageNavigation } from '../../components/collectionProduct';


const limit = 20;

export default function community({
Expand Down

0 comments on commit fba492a

Please sign in to comment.