From 1592d3af2ff3a493d2a2746d44598f65d79114d8 Mon Sep 17 00:00:00 2001 From: Jeong Daseul <98886223+goodaseul@users.noreply.github.com> Date: Fri, 2 Jan 2026 10:23:53 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9A=A8=20Fix=20:=20=EB=AA=A9=ED=91=9C?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EC=8B=9C=20proxy=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/(protected)/goals/[goalId]/page.tsx | 13 ++++ .../queries/goals/useDeleteGoalMutation.ts | 74 ++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/app/(protected)/goals/[goalId]/page.tsx b/src/app/(protected)/goals/[goalId]/page.tsx index 5d50b0f..7f968a4 100644 --- a/src/app/(protected)/goals/[goalId]/page.tsx +++ b/src/app/(protected)/goals/[goalId]/page.tsx @@ -29,6 +29,19 @@ export default async function GoalsPage({ params }: GoalsPageProps) { auth: "access", }), }), + queryClient.prefetchQuery({ + queryKey: goalsQueryKeys.detail(Number(goalId)), + queryFn: async () => { + try { + return await backendFetch(`/goals/${goalId}`, { + auth: "access", + }); + } catch (error) { + console.warn(`Goal ${goalId} not found, skipping prefetch`); + return null; + } + }, + }), ]); return ( diff --git a/src/hooks/queries/goals/useDeleteGoalMutation.ts b/src/hooks/queries/goals/useDeleteGoalMutation.ts index 7c03884..0b802ee 100644 --- a/src/hooks/queries/goals/useDeleteGoalMutation.ts +++ b/src/hooks/queries/goals/useDeleteGoalMutation.ts @@ -1,14 +1,84 @@ -import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { + useMutation, + useQueryClient, + InfiniteData, +} from "@tanstack/react-query"; import { deleteGoal } from "@/api/goal"; import goalsQueryKeys from "./queryKeys"; +import { useRouter } from "next/navigation"; +import { Goal, GoalResponse } from "@/api/types/goal"; + +type GoalsCache = + | { nextCursor: number | null; totalCount: number; goals: Goal[] } + | undefined; + export function useDeleteGoalMutation() { const queryClient = useQueryClient(); + const router = useRouter(); return useMutation({ mutationFn: (goalId: number) => deleteGoal(goalId), + + onMutate: async (goalId) => { + await queryClient.cancelQueries({ + queryKey: goalsQueryKeys.detail(goalId), + }); + await queryClient.cancelQueries({ + queryKey: goalsQueryKeys.list(), + }); + await queryClient.cancelQueries({ + queryKey: goalsQueryKeys.infinite(), + }); + + queryClient.setQueriesData( + { queryKey: goalsQueryKeys.list() }, + (old) => { + if (!old?.goals) return old; + return { + ...old, + totalCount: old.totalCount - 1, + goals: old.goals.filter((goal) => goal.id !== goalId), + }; + }, + ); + + queryClient.setQueriesData>( + { queryKey: goalsQueryKeys.infinite() }, + (old) => { + if (!old?.pages) return old; + return { + ...old, + pages: old.pages.map((page) => ({ + ...page, + totalCount: page.totalCount - 1, + goals: page.goals.filter((goal) => goal.id !== goalId), + })), + }; + }, + ); + + queryClient.removeQueries({ + queryKey: goalsQueryKeys.detail(goalId), + }); + }, + onSuccess: () => { queryClient.invalidateQueries({ - queryKey: goalsQueryKeys.all, + queryKey: goalsQueryKeys.list(), + }); + queryClient.invalidateQueries({ + queryKey: goalsQueryKeys.infinite(), + }); + + router.replace("/dashboard"); + }, + + onError: () => { + queryClient.invalidateQueries({ + queryKey: goalsQueryKeys.list(), + }); + queryClient.invalidateQueries({ + queryKey: goalsQueryKeys.infinite(), }); }, }); From e363db3f4923ce4f46daad6b3e91d74b2c41fb0a Mon Sep 17 00:00:00 2001 From: Jeong Daseul <98886223+goodaseul@users.noreply.github.com> Date: Fri, 2 Jan 2026 10:31:17 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=9A=A8=20Fix=20:=20try/catch=20?= =?UTF-8?q?=EB=AC=B8=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/(protected)/goals/[goalId]/page.tsx | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/app/(protected)/goals/[goalId]/page.tsx b/src/app/(protected)/goals/[goalId]/page.tsx index 7f968a4..d926d41 100644 --- a/src/app/(protected)/goals/[goalId]/page.tsx +++ b/src/app/(protected)/goals/[goalId]/page.tsx @@ -31,16 +31,10 @@ export default async function GoalsPage({ params }: GoalsPageProps) { }), queryClient.prefetchQuery({ queryKey: goalsQueryKeys.detail(Number(goalId)), - queryFn: async () => { - try { - return await backendFetch(`/goals/${goalId}`, { - auth: "access", - }); - } catch (error) { - console.warn(`Goal ${goalId} not found, skipping prefetch`); - return null; - } - }, + queryFn: () => + backendFetch(`/goals/${goalId}`, { + auth: "access", + }), }), ]);