-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseEventHook.ts
More file actions
41 lines (35 loc) · 1.33 KB
/
useEventHook.ts
File metadata and controls
41 lines (35 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useMutation, useQuery } from '@tanstack/react-query';
import { eventDeletion, eventDetail } from '../api/event';
import { useParams } from 'react-router-dom';
import { useUserInfo } from '../../../features/join/hooks/useUserHook';
import { ApiResponse } from '../../../shared/types/api/apiResponse';
import useAuthStore from '../../../app/provider/authStore';
import { AxiosError } from 'axios';
import { EventDeletionResponse } from '../model/event';
export const useEventDetail = () => {
const { id } = useParams();
const isLoggedIn = useAuthStore(state => state.isLoggedIn);
const { data: user } = useUserInfo(isLoggedIn);
const eventId = Number(id);
const { data } = useQuery({
queryKey: ['eventDetail', eventId],
queryFn: () => eventDetail({ eventId, userId: user?.id }),
enabled: !!eventId,
retry: false,
});
return { data };
};
export const useEventDeletion = () => {
return useMutation<ApiResponse<null>, AxiosError<EventDeletionResponse>, number>({
mutationFn: async (eventId: number) => {
return await eventDeletion(eventId);
},
onError: error => {
const code = error.response?.data?.code;
if (code === 'EVENT4002') {
const message = error.response?.data?.message || '알 수 없는 오류가 발생했습니다.';
alert(message);
}
},
});
};