|
1 | | -import { useEffect, useState } from "react"; |
| 1 | +import { AxiosResponse } from "axios"; |
2 | 2 |
|
3 | | -import useFetch from "@/utils/apiUtils"; |
| 3 | +import { axiosInstance } from "@/utils/axiosInstance"; |
4 | 4 |
|
5 | 5 | import { ArticleResponse } from "../types/response"; |
| 6 | +import { QueryKeys } from "./queryKey"; |
| 7 | + |
| 8 | +import { useQuery } from "@tanstack/react-query"; |
6 | 9 |
|
7 | 10 | /* ---------- 타입 ---------- */ |
8 | 11 |
|
9 | 12 | interface ArticleListResponse { |
10 | 13 | news: ArticleResponse[]; // 최대 5개 |
11 | 14 | } |
12 | 15 |
|
| 16 | +const getArticleList = async (userId: number): Promise<ArticleListResponse> => { |
| 17 | + const response: AxiosResponse<ArticleListResponse> = await axiosInstance.get(`/news?site-user-id=${userId}`); |
| 18 | + return response.data; |
| 19 | +}; |
| 20 | + |
13 | 21 | const useGetArticleList = (userId: number | null) => { |
14 | | - const { result, loading, error, fetchData } = useFetch<ArticleListResponse>(); |
15 | | - |
16 | | - const [articleList, setArticleList] = useState<ArticleResponse[]>([]); |
17 | | - |
18 | | - /* 페이지 변경 시 데이터 요청 */ |
19 | | - useEffect(() => { |
20 | | - if (userId === null) return; |
21 | | - |
22 | | - fetchData({ |
23 | | - method: "get", |
24 | | - url: `/news?site-user-id=${userId}`, |
25 | | - body: undefined, |
26 | | - isToken: true, |
27 | | - }); |
28 | | - }, [userId, fetchData]); |
29 | | - |
30 | | - /* 응답 처리 */ |
31 | | - useEffect(() => { |
32 | | - if (result) { |
33 | | - setArticleList(result.data.news); |
34 | | - } |
35 | | - }, [result]); |
36 | | - |
37 | | - return { articleList, loading, error }; |
| 22 | + return useQuery({ |
| 23 | + queryKey: [QueryKeys.articleList, userId], |
| 24 | + queryFn: () => getArticleList(userId!), |
| 25 | + enabled: userId !== null, |
| 26 | + select: (data: ArticleListResponse) => data.news, |
| 27 | + }); |
38 | 28 | }; |
39 | 29 |
|
40 | 30 | export default useGetArticleList; |
0 commit comments