From f80563beb3efb7edbddf23d3b6d1981a8e71cef3 Mon Sep 17 00:00:00 2001 From: ejinn1 Date: Wed, 5 Mar 2025 00:44:42 +0900 Subject: [PATCH 1/6] Refactor: add function to inject token from server --- src/apis/services/httpMethod.ts | 7 +++++-- src/lib/axiosInstance.ts | 28 ++++++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/apis/services/httpMethod.ts b/src/apis/services/httpMethod.ts index b9421d1e..26de6888 100644 --- a/src/apis/services/httpMethod.ts +++ b/src/apis/services/httpMethod.ts @@ -1,7 +1,10 @@ -import axiosInstance from '@/lib/axiosInstance'; +import axiosInstance, { setAuthToken } from '@/lib/axiosInstance'; -export async function GET(url: string): Promise { +export async function GET(url: string, token?: string): Promise { try { + if (token) { + setAuthToken(token); + } const response = await axiosInstance.get(url); return response.data; } catch (error) { diff --git a/src/lib/axiosInstance.ts b/src/lib/axiosInstance.ts index 0a357d6a..8cc9ee66 100644 --- a/src/lib/axiosInstance.ts +++ b/src/lib/axiosInstance.ts @@ -9,19 +9,26 @@ const axiosInstance = axios.create({ }, }); +export const setAuthToken = (token: string) => { + if (token) { + axiosInstance.defaults.headers['token'] = token; + } +}; + axiosInstance.interceptors.request.use( (config) => { - const getCookie = (name: string) => { - const matches = document.cookie.match( - new RegExp(`(^|; )${name}=([^;]*)`), - ); - return matches ? decodeURIComponent(matches[2]) : null; - }; - - const token = getCookie('token'); // 'token' 쿠키 값을 가져옴 + if (typeof window !== 'undefined') { + const getCookie = (name: string) => { + const matches = document.cookie.match( + new RegExp(`(^|; )${name}=([^;]*)`), + ); + return matches ? decodeURIComponent(matches[2]) : null; + }; - if (token) { - config.headers['token'] = token; // 헤더에 추가 + const token = getCookie('token'); + if (token) { + config.headers['token'] = token; + } } return config; @@ -30,4 +37,5 @@ axiosInstance.interceptors.request.use( return Promise.reject(error); }, ); + export default axiosInstance; From 61f4426cbc2fe82830fadb46b467f0a707653fbc Mon Sep 17 00:00:00 2001 From: ejinn1 Date: Wed, 5 Mar 2025 00:47:18 +0900 Subject: [PATCH 2/6] Feat: add suspense for improved loading state handling --- src/app/(route)/dashboard/page.tsx | 25 ++++++++++++++++--- .../Dashboard/RecentTodos/index.tsx | 11 +++----- .../apis/Dashboard/useRecentTodosQuery.ts | 18 ++++++++----- .../apis/Dashboard/useTodayProgressQuery.ts | 19 ++++++++------ 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/app/(route)/dashboard/page.tsx b/src/app/(route)/dashboard/page.tsx index 774e0849..30d28d89 100644 --- a/src/app/(route)/dashboard/page.tsx +++ b/src/app/(route)/dashboard/page.tsx @@ -1,18 +1,37 @@ +import { Suspense } from 'react'; + +import { cookies } from 'next/headers'; + import { Header } from '@/components/common/Header'; import { PageContainer } from '@/components/common/PageContainer'; import { Follower } from '@/components/Dashboard/Follower'; import { GoalList } from '@/components/Dashboard/GoalList'; import { MyProgress } from '@/components/Dashboard/MyProgress'; import { RecentTodos } from '@/components/Dashboard/RecentTodos'; +import { TodoListSkeleton } from '@/components/Skeletons/TodoListSkeleton'; +import { recentTodosOptions } from '@/hooks/apis/Dashboard/useRecentTodosQuery'; +import { todayProgressOptions } from '@/hooks/apis/Dashboard/useTodayProgressQuery'; +import { ServerFetchBoundary } from '@/lib/query/ServerFetchBoundary'; + +export default async function DashBoardPage() { + const cookieStore = await cookies(); + const token = cookieStore.get('token')?.value || ''; + const serverFetchOptions = [ + todayProgressOptions(token), + recentTodosOptions(token), + ]; -export default function DashBoardPage() { return ( <>
- - + }> + + + + + diff --git a/src/components/Dashboard/RecentTodos/index.tsx b/src/components/Dashboard/RecentTodos/index.tsx index 4b92ba18..97e99a3e 100644 --- a/src/components/Dashboard/RecentTodos/index.tsx +++ b/src/components/Dashboard/RecentTodos/index.tsx @@ -5,7 +5,6 @@ import { FaAngleRight } from 'react-icons/fa6'; import Link from 'next/link'; import { DashboardItemContainer } from '@/components/Dashboard/DashboardItemContainer'; -import { TodoListSkeleton } from '@/components/Skeletons/TodoListSkeleton'; import { Button } from '@/components/common/Button/Button'; import { Card } from '@/components/common/Card'; import { NoDataText } from '@/components/common/NoDataText'; @@ -18,7 +17,7 @@ import { useSidebarStore } from '@/store/useSidebarStore'; import { useTodoModalStore } from '@/store/useTodoModalStore'; export const RecentTodos = () => { - const { todos, isLoading } = useRecentTodosQuery(); + const { todos } = useRecentTodosQuery(); const { goals } = useGoalsQuery(); const { open: openModal } = useTodoModalStore(); @@ -36,9 +35,7 @@ export const RecentTodos = () => { 모두 보기 - {isLoading && } - - {!isLoading && !hasGoals && ( + {!hasGoals && (