diff --git a/frontend/src/hooks/queries/studylog.ts b/frontend/src/hooks/queries/studylog.ts index a3f100544..31c01f40e 100644 --- a/frontend/src/hooks/queries/studylog.ts +++ b/frontend/src/hooks/queries/studylog.ts @@ -29,6 +29,7 @@ const QUERY_KEY = { tempSavedStudylog: 'tempSavedStudylog', }; +/* @deprecated index에서 useState로 처리 */ export const useGetRecentStudylogsQuery = () => { const { user } = useContext(UserContext); const { accessToken } = user; @@ -36,10 +37,10 @@ export const useGetRecentStudylogsQuery = () => { return useQuery([QUERY_KEY.recentStudylogs], async () => { try { const response = await requestGetStudylogs({ - query: {type: 'searchParams', data: 'size=3'}, + query: { type: 'searchParams', data: 'size=3' }, accessToken, }); - const {data} = await response.data; + const { data } = await response.data; return data; } catch (error) { alert(ERROR_MESSAGE.DEFAULT); @@ -47,6 +48,7 @@ export const useGetRecentStudylogsQuery = () => { }); }; +/* @deprecated index에서 useState로 처리 */ export const useGetPopularStudylogsQuery = () => { const { user } = useContext(UserContext); const { accessToken } = user; diff --git a/frontend/src/pages/MainPage/index.tsx b/frontend/src/pages/MainPage/index.tsx index 762c9093a..204422357 100644 --- a/frontend/src/pages/MainPage/index.tsx +++ b/frontend/src/pages/MainPage/index.tsx @@ -1,6 +1,6 @@ /** @jsxImportSource @emotion/react */ -import { useContext, useEffect } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import { UserContext } from '../../contexts/UserProvider'; import bannerList from '../../configs/bannerList'; @@ -11,26 +11,37 @@ import PopularStudyLogList from './PopularStudyLogList'; import { MainContentStyle } from '../../PageRouter'; import { getRowGapStyle } from '../../styles/layout.styles'; -import { - useGetPopularStudylogsQuery, - useGetRecentStudylogsQuery, -} from '../../hooks/queries/studylog'; +import { requestGetStudylogs } from '../../service/requests'; +import { ERROR_MESSAGE } from '../../constants'; const MainPage = () => { const { user } = useContext(UserContext); const { accessToken } = user; - const { data: recentStudylogs, refetch: refetchRecentStudylogs } = useGetRecentStudylogsQuery(); - - const { - data: popularStudyLogs, - refetch: refetchPopularStudyLogs, - } = useGetPopularStudylogsQuery(); + const [recentStudylogs, setRecentStudylogs] = useState(null); + const [popularStudyLogs, setPopularStudyLogs] = useState(null); useEffect(() => { - refetchRecentStudylogs(); - refetchPopularStudyLogs(); - }, [accessToken]); + const fetchRecentStudylogs = async () => { + try { + const response = await requestGetStudylogs({ + query: { type: 'searchParams', data: 'size=3' }, + accessToken, + }); + + if (!response) { + return []; + } + + const { data } = response.data; + setRecentStudylogs(data); + } catch (error) { + alert(ERROR_MESSAGE.DEFAULT); + } + }; + + fetchRecentStudylogs(); + }, []); return ( <> diff --git a/frontend/src/utils/axiosInstance.ts b/frontend/src/utils/axiosInstance.ts index cfb1e4484..b67a1d9f3 100644 --- a/frontend/src/utils/axiosInstance.ts +++ b/frontend/src/utils/axiosInstance.ts @@ -14,8 +14,29 @@ export const createAxiosInstance = ({ accessToken }: AxiosInstanceProps = {}) => headers['Authorization'] = `Bearer ${accessToken}`; } - return axios.create({ + const axiosInstance = axios.create({ baseURL: BASE_URL, headers: accessToken ? headers : {}, }); + + axiosInstance.interceptors.response.use( + response => { + // 성공적인 응답 처리 + return response; + }, + error => { + if (error.response && error.response.status === 400) { + const { code } = error.response.data; + if (code === 1002) { + localStorage.removeItem('accessToken'); + window.location.href = '/'; + return Promise.resolve(); + } + } + + return Promise.reject(error); + } + ); + + return axiosInstance; };