Skip to content

Commit 6118a83

Browse files
committed
Feat: 즐겨찾기에 페이지네이션 구현
1 parent 25aacbc commit 6118a83

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

components/Pagination.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@ interface PaginationProps {
88
}
99

1010
const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
11-
const router = useRouter();
1211
const LiStyle = "relative w-12 h-12 rounded-lg bg-gray900";
1312
const buttonStyle =
1413
"flex justify-center items-center w-full h-full text-black400";
15-
const { page, pageSize } = router.query;
1614

15+
const router = useRouter();
16+
17+
const { page, pageSize } = router.query;
1718
const currentPage = Number(page) || 1;
1819
const currentPageSize = Number(pageSize) || 6;
1920
const totalPages = Math.ceil(totalCount / currentPageSize);
21+
2022
const [maxPagesToShow, setMaxPagesToShow] = useState(2);
2123
const { isPC } = useViewport();
2224

2325
const handlePageChange = (newPage: number) => {
2426
if (newPage !== currentPage) {
27+
const path = router.pathname;
2528
router.push(
26-
`/link?page=${newPage}&pageSize=${currentPageSize}`,
29+
{
30+
pathname: path,
31+
query: { page: newPage, pageSize: currentPageSize },
32+
},
2733
undefined,
2834
{ shallow: true }
2935
);

hooks/useFetchLinks.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useState } from "react";
22
import { proxy } from "@/lib/api/axiosInstanceApi";
33
import { LinkData } from "@/types/linkTypes";
4+
import { useRouter } from "next/router";
45

56
// 링크페이지의 query가 바뀌면 새로운 리스트로 업데이트 해주는 훅
67
const useFetchLinks = (
@@ -10,20 +11,32 @@ const useFetchLinks = (
1011
},
1112
setLinkCardList: (list: LinkData[]) => void
1213
) => {
14+
const router = useRouter();
15+
1316
useEffect(() => {
1417
const fetchLinks = async () => {
15-
const res = await proxy.get("/api/links", {
16-
params: {
17-
page: query.page,
18-
pageSize: 6,
19-
search: query.search,
20-
},
21-
});
22-
setLinkCardList(res.data.list);
18+
// 경로에 따라 API 엔드포인트 분기
19+
let endpoint = "/api/links";
20+
const params: any = {
21+
page: query.page,
22+
pageSize: 6,
23+
search: query.search,
24+
};
25+
26+
if (router.pathname === "/favorite") {
27+
endpoint = "/api/favorites";
28+
}
29+
30+
try {
31+
const res = await proxy.get(endpoint, { params });
32+
setLinkCardList(res.data.list);
33+
} catch (error) {
34+
console.error("Error fetching links:", error);
35+
}
2336
};
2437

2538
if (query) fetchLinks();
26-
}, [query, setLinkCardList]);
39+
}, [query, setLinkCardList, router.pathname]);
2740
};
2841

2942
export default useFetchLinks;

pages/api/favorites/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import axiosInstance from "@/lib/api/axiosInstanceApi";
55
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
66
const cookies = parse(req.headers.cookie || "");
77
const accessToken = cookies.accessToken;
8+
const { page, pageSize = 6 } = req.query;
89

910
if (!accessToken) {
1011
return res.status(401).json({ message: "인증 오류: 토큰이 없습니다." });
@@ -14,12 +15,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
1415
case "GET":
1516
// 즐겨찾기 목록 조회
1617
try {
17-
const response = await axiosInstance.get(
18-
"/favorites?page=1&pageSize=10",
19-
{
20-
headers: { Authorization: `Bearer ${accessToken}` },
21-
}
22-
);
18+
const response = await axiosInstance.get(`/favorites`, {
19+
params: { page, pageSize },
20+
headers: { Authorization: `Bearer ${accessToken}` },
21+
});
2322
return res.status(200).json(response.data);
2423
} catch (err: any) {
2524
// 즐겨찾기 폴더가 없는 경우 (404 처리)

pages/favorite/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { proxy } from "@/lib/api/axiosInstanceApi";
33
import CardsLayout from "@/components/Layout/CardsLayout";
44
import Container from "@/components/Layout/Container";
55
import LinkCard from "@/components/Link/LinkCard";
6+
import Pagination from "@/components/Pagination";
7+
import useFetchLinks from "@/hooks/useFetchLinks";
8+
import { useRouter } from "next/router";
9+
import { useState } from "react";
610

711
interface FavoriteDataType {
812
id: number;
@@ -42,6 +46,12 @@ export const getServerSideProps: GetServerSideProps = async (
4246
};
4347

4448
const FavoritePage = ({ favoriteList, totalCount }: FavoriteProps) => {
49+
const router = useRouter();
50+
51+
const [linkCardList, setLinkCardList] =
52+
useState<FavoriteDataType[]>(favoriteList);
53+
54+
useFetchLinks(router.query, setLinkCardList);
4555
return (
4656
<>
4757
<div className="page-title pt-[10px] md:pt-5 pb-10 md:pb-[60px] bg-gray100 text-center">
@@ -51,8 +61,8 @@ const FavoritePage = ({ favoriteList, totalCount }: FavoriteProps) => {
5161
</div>
5262
<Container>
5363
<CardsLayout>
54-
{favoriteList.length > 0
55-
? favoriteList.map((favorite) => (
64+
{linkCardList.length > 0
65+
? linkCardList.map((favorite) => (
5666
<LinkCard key={favorite.id} info={favorite} />
5767
))
5868
: null}
@@ -70,6 +80,7 @@ const FavoritePage = ({ favoriteList, totalCount }: FavoriteProps) => {
7080
</div>
7181
</div>
7282
)}
83+
<Pagination totalCount={totalCount} />
7384
</Container>
7485
</>
7586
);

0 commit comments

Comments
 (0)