|
| 1 | +import { useQuery, useMutation } from '@tanstack/react-query'; |
| 2 | + |
| 3 | +import { |
| 4 | + getMandalAll, |
| 5 | + getCoreGoals, |
| 6 | + getSubGoalIds, |
| 7 | + postOnboardingSubGoal, |
| 8 | + patchOnboardingSubGoal, |
| 9 | + deleteOnboardingSubGoal, |
| 10 | + postAiRecommendNewSubGoal, |
| 11 | + type SubGoalIdPosition, |
| 12 | +} from './index'; |
| 13 | + |
| 14 | +import { QUERY_KEY } from '@/api/constant/queryKey'; |
| 15 | + |
| 16 | +export const useGetMandalAll = (mandalartId: number) => { |
| 17 | + return useQuery({ |
| 18 | + queryKey: [QUERY_KEY.ENTIRE_GOAL, mandalartId], |
| 19 | + queryFn: () => getMandalAll(mandalartId), |
| 20 | + enabled: !!mandalartId, |
| 21 | + }); |
| 22 | +}; |
| 23 | + |
| 24 | +export const useGetCoreGoals = (mandalartId: number) => { |
| 25 | + return useQuery({ |
| 26 | + queryKey: [QUERY_KEY.CORE_GOALS, mandalartId], |
| 27 | + queryFn: () => getCoreGoals(mandalartId), |
| 28 | + enabled: !!mandalartId, |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +export const useGetSubGoalIds = (coreGoalId: number) => { |
| 33 | + return useQuery<{ subGoalIds: SubGoalIdPosition[] }>({ |
| 34 | + queryKey: QUERY_KEY.SUB_GOAL_IDS(coreGoalId), |
| 35 | + queryFn: () => getSubGoalIds(coreGoalId), |
| 36 | + enabled: !!coreGoalId && coreGoalId > 0, |
| 37 | + retry: false, |
| 38 | + }); |
| 39 | +}; |
| 40 | + |
| 41 | +export const usePostOnboardingSubGoal = () => { |
| 42 | + return useMutation({ |
| 43 | + mutationKey: QUERY_KEY.POST_ONBOARDING_SUB_GOAL, |
| 44 | + mutationFn: ({ |
| 45 | + coreGoalId, |
| 46 | + title, |
| 47 | + position, |
| 48 | + cycle, |
| 49 | + }: { |
| 50 | + coreGoalId: number; |
| 51 | + title: string; |
| 52 | + position: number; |
| 53 | + cycle: string; |
| 54 | + }) => postOnboardingSubGoal(coreGoalId, { title, position, cycle }), |
| 55 | + }); |
| 56 | +}; |
| 57 | + |
| 58 | +export const usePatchOnboardingSubGoal = () => { |
| 59 | + return useMutation({ |
| 60 | + mutationKey: QUERY_KEY.PATCH_ONBOARDING_SUB_GOAL, |
| 61 | + mutationFn: (params: { subGoalId: number; title: string; cycle: string }) => |
| 62 | + patchOnboardingSubGoal(params), |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | +export const useDeleteOnboardingSubGoal = () => { |
| 67 | + return useMutation({ |
| 68 | + mutationKey: QUERY_KEY.DELETE_ONBOARDING_SUB_GOAL, |
| 69 | + mutationFn: (subGoalId: number) => deleteOnboardingSubGoal(subGoalId), |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | +export const usePostAiRecommendNewSubGoal = () => { |
| 74 | + return useMutation({ |
| 75 | + mutationKey: QUERY_KEY.POST_AI_RECOMMEND_NEW_SUB_GOAL, |
| 76 | + mutationFn: async ({ |
| 77 | + coreGoalId, |
| 78 | + coreGoal, |
| 79 | + subGoal, |
| 80 | + }: { |
| 81 | + coreGoalId: number; |
| 82 | + coreGoal: string; |
| 83 | + subGoal: { title: string }[]; |
| 84 | + }) => { |
| 85 | + const response = await postAiRecommendNewSubGoal(coreGoalId, { coreGoal, subGoal }); |
| 86 | + return response; |
| 87 | + }, |
| 88 | + }); |
| 89 | +}; |
0 commit comments