|
1 | | -import { useEffect, useState } from "react"; |
| 1 | +import { useEffect } from "react"; |
2 | 2 | import { proxy } from "@/lib/api/axiosInstanceApi"; |
3 | 3 | import { LinkData } from "@/types/linkTypes"; |
4 | | -import { useRouter } from "next/router"; |
| 4 | +import { ParsedUrlQuery } from "querystring"; |
| 5 | +import useViewport from "./useViewport"; |
5 | 6 |
|
6 | | -// 링크페이지의 query가 바뀌면 새로운 리스트로 업데이트 해주는 훅 |
| 7 | +// 링크페이지의 query가 바뀌면 그에 맞는 링크들을 보여주는 훅 |
7 | 8 | const useFetchLinks = ( |
8 | | - setLinkCardList: (list: LinkData[], totalCount: number) => void |
| 9 | + setLinkCardList: React.Dispatch<React.SetStateAction<LinkData[]>>, |
| 10 | + setTotalCount?: React.Dispatch<React.SetStateAction<number>>, |
| 11 | + query?: ParsedUrlQuery, |
| 12 | + pathname?: string |
9 | 13 | ) => { |
10 | | - const router = useRouter(); |
| 14 | + const { isTablet } = useViewport(); |
11 | 15 |
|
12 | 16 | useEffect(() => { |
13 | 17 | const fetchLinks = async () => { |
14 | 18 | // 경로에 따라 API 엔드포인트 분기 |
15 | | - let endpoint = "/api/links"; |
16 | | - const params: any = { |
17 | | - page: router.query.page, |
18 | | - pageSize: 6, |
19 | | - search: router.query.search, |
20 | | - }; |
| 19 | + let endpoint = |
| 20 | + pathname === "/favorite" |
| 21 | + ? "/api/favorites" |
| 22 | + : query?.folder |
| 23 | + ? `/api/folders/${query.folder}/links` |
| 24 | + : "/api/links"; |
21 | 25 |
|
22 | | - if (router.pathname === "/favorite") { |
23 | | - endpoint = "/api/favorites"; |
24 | | - } |
25 | | - |
26 | | - try { |
27 | | - const res = await proxy.get(endpoint, { params }); |
28 | | - setLinkCardList(res.data.list, res.data.totalCount); |
29 | | - } catch (error) { |
30 | | - console.error("Error fetching links:", error); |
| 26 | + const res = await proxy.get(endpoint, { |
| 27 | + params: { |
| 28 | + page: query?.page, |
| 29 | + pageSize: isTablet ? 6 : 10, |
| 30 | + search: query?.search, |
| 31 | + }, |
| 32 | + }); |
| 33 | + console.log("query가 바뀌었을 때 다시 받아온 리스트:", res.data.list); |
| 34 | + setLinkCardList(res.data.list); |
| 35 | + { |
| 36 | + setTotalCount && setTotalCount(res.data.totalCount); |
31 | 37 | } |
32 | 38 | }; |
33 | | - |
34 | | - if (router.query) fetchLinks(); |
35 | | - }, [router.query, setLinkCardList, router.pathname]); |
| 39 | + if (query) fetchLinks(); |
| 40 | + }, [setLinkCardList, query, isTablet]); |
36 | 41 | }; |
37 | 42 |
|
38 | 43 | export default useFetchLinks; |
0 commit comments