Skip to content

Commit 92af93f

Browse files
fix: 코드리뷰 반영
1 parent 8b0aab2 commit 92af93f

File tree

9 files changed

+47
-12
lines changed

9 files changed

+47
-12
lines changed

src/api/domain/edit/hook.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const useUpperGoalIds = (mandalartId: number) => {
2222
return useQuery({
2323
queryKey: EDIT_QUERY_KEY.upperGoalIds(mandalartId),
2424
queryFn: () => getUpperGoalIds(mandalartId),
25+
enabled: !!mandalartId,
2526
});
2627
};
2728

@@ -31,10 +32,13 @@ export const useSubGoals = (
3132
cycle?: CycleType,
3233
options?: Omit<UseQueryOptions<BaseResponse<SubGoalsResponse>>, 'queryKey' | 'queryFn'>,
3334
) => {
35+
const enabled = !!mandalartId && (options?.enabled ?? true);
36+
3437
return useQuery({
3538
queryKey: EDIT_QUERY_KEY.subGoals(mandalartId, coreGoalId, cycle),
3639
queryFn: () => getSubGoals(mandalartId, coreGoalId, cycle),
3740
...options,
41+
enabled,
3842
});
3943
};
4044

@@ -61,5 +65,6 @@ export const useCoreGoals = (mandalartId: number) => {
6165
return useQuery({
6266
queryKey: EDIT_QUERY_KEY.coreGoals(mandalartId),
6367
queryFn: () => getCoreGoals(mandalartId),
68+
enabled: !!mandalartId,
6469
});
6570
};

src/api/domain/history/hook/useGetHistory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export const useGetHistory = (mandalartId: number) => {
88
return useQuery<historyResponse>({
99
queryKey: QUERY_KEY.HISTORY(mandalartId),
1010
queryFn: () => getHistory(mandalartId),
11+
enabled: !!mandalartId,
1112
});
1213
};

src/api/domain/history/hook/useGetStreak.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export const useGetStreak = (mandalartId: number) => {
88
return useQuery<StreakResponse>({
99
queryKey: QUERY_KEY.STREAK(mandalartId),
1010
queryFn: () => getStreak(mandalartId),
11+
enabled: !!mandalartId,
1112
});
1213
};

src/api/domain/lowerTodo/hook/useCoreGoals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export const useCoreGoals = (mandalartId: number) => {
88
return useQuery({
99
queryKey: QUERY_KEY.MANDALART_CORE_GOALS(mandalartId),
1010
queryFn: () => getCoreGoals(mandalartId),
11+
enabled: !!mandalartId,
1112
});
1213
};

src/api/domain/mandalAll/hook.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export const useMandalAll = (mandalartId: number) => {
1111
return useQuery({
1212
queryKey: MANDAL_ALL_KEY.detail(mandalartId),
1313
queryFn: () => getMandalAll(mandalartId),
14+
enabled: !!mandalartId,
1415
});
1516
};

src/api/domain/myTodo/hook/useGetRecommendation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export const useGetRecommendation = (mandalartId: number, date: string) => {
77
return useQuery({
88
queryKey: [QUERY_KEY.RECOMMENDED_TODO(mandalartId), date],
99
queryFn: () => getRecommendation(mandalartId, { date }),
10+
enabled: !!mandalartId,
1011
});
1112
};

src/api/domain/myTodo/hook/useMyMandal.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ export const useGetMandalAll = (mandalartId: number) => {
1919
return useQuery({
2020
queryKey: [QUERY_KEY.ENTIRE_GOAL, mandalartId],
2121
queryFn: () => getMandalAll(mandalartId),
22+
enabled: !!mandalartId,
2223
});
2324
};
2425

2526
export const useGetMandalCoreGoals = (mandalartId: number) => {
2627
return useQuery<MandalCoreGoalsResponse>({
2728
queryKey: [QUERY_KEY.CORE_GOALS, mandalartId],
2829
queryFn: () => getMandalCoreGoals(mandalartId),
30+
enabled: !!mandalartId,
2931
});
3032
};
3133

src/page/todo/entireTodo/Todo.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@ const Todo = () => {
1818
const isValid = trimmed.length > 0;
1919
const displayedText = useTypingEffect(FULL_TEXT, TYPING_DURATION);
2020

21-
const { mutate, isPending } = useCreateEntireTodo();
21+
const { mutateAsync, isPending } = useCreateEntireTodo();
2222
const navigate = useNavigate();
2323

24-
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
24+
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
2525
e.preventDefault();
2626
if (!isValid || isPending) {
2727
return;
2828
}
2929

3030
const title = trimmed;
31-
mutate(
32-
{ title },
33-
{
34-
onSuccess: () => navigate(PATH.TODO_UPPER),
35-
},
36-
);
31+
try {
32+
const data = await mutateAsync({ title });
33+
if (typeof window !== 'undefined') {
34+
localStorage.setItem('mandalartId', String(data.id));
35+
}
36+
navigate(PATH.TODO_UPPER);
37+
} catch {
38+
alert('목표 생성에 실패했습니다.');
39+
}
3740
};
3841

3942
if (isPending) {

src/page/todo/upperTodo/hook/useUpperTodoAI.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useState } from 'react';
2+
import { isAxiosError } from 'axios';
23

34
import { extractTitles, updateSubGoalsWithAiResponse } from '../utils/goal';
45
import { ALERT, GOAL_COUNT } from '../constants';
@@ -26,6 +27,22 @@ interface CoreGoalResponse {
2627
title: string;
2728
}
2829

30+
interface ApiErrorResponse {
31+
message?: string;
32+
}
33+
34+
const getServerMessage = (error: unknown, fallback: string) => {
35+
if (isAxiosError<ApiErrorResponse>(error)) {
36+
return error.response?.data?.message ?? fallback;
37+
}
38+
39+
if (error instanceof Error && error.message) {
40+
return error.message;
41+
}
42+
43+
return fallback;
44+
};
45+
2946
export const useUpperTodoAI = ({
3047
mandalartId,
3148
mainGoal,
@@ -51,8 +68,9 @@ export const useUpperTodoAI = ({
5168
refetchCoreGoalIds();
5269
refetch();
5370
},
54-
onError: () => {
55-
alert(ALERT.aiSaveFail);
71+
onError: (error) => {
72+
const message = getServerMessage(error, ALERT.aiSaveFail);
73+
alert(message);
5674
},
5775
},
5876
);
@@ -91,9 +109,11 @@ export const useUpperTodoAI = ({
91109
);
92110

93111
openModal(aiModalContent);
94-
} catch {
112+
} catch (error) {
113+
const message = getServerMessage(error, ALERT.aiFetchFail);
114+
alert(message);
115+
} finally {
95116
setIsAiUsed(false);
96-
alert(ALERT.aiFetchFail);
97117
}
98118
};
99119

0 commit comments

Comments
 (0)