-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTicketOptionHook.ts
More file actions
192 lines (175 loc) · 6.46 KB
/
useTicketOptionHook.ts
File metadata and controls
192 lines (175 loc) · 6.46 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { ApiResponse } from '../../../shared/types/api/apiResponse';
import { useParams, useNavigate } from 'react-router-dom';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { TicketOptionRequest, TicketOptionTypeResponse } from '../model/ticketOption';
import {
PersonalTicketOptionAnswerResponse,
TicketOptionAnswerRequest,
TicketOptionAnswerResponse,
TicketOptionResponse,
} from '../model/ticketInformation';
import {
createTicketOptionAnswers,
readPersonalTicketOptionAnswers,
readPurchaserAnswers,
readTicketOptions,
} from '../api/ticketOption';
import {
getTicketOptions,
createTicketOption,
modifyTicketOption,
deleteTicketOption,
getAttachedTicketOptions,
getTicketOptionDetail,
attachTicketOption,
detachTicketOption,
} from '../api/ticketOption';
// 티켓 옵션 조회
export const useTicketOptions = (ticketId: number) => {
return useQuery<{ isSuccess: boolean; result: TicketOptionResponse[] }>({
queryKey: ['ticketOptions', ticketId],
queryFn: () => readTicketOptions(ticketId),
enabled: !!ticketId,
});
};
// 티켓 옵션 응답 전송
export const useCreateTicketOptionAnswers = () => {
return useMutation<ApiResponse<null>, Error, TicketOptionAnswerRequest>({
mutationFn: createTicketOptionAnswers,
onError: () => {
alert('티켓 옵션 응답 전송 중 오류가 발생했습니다.');
},
});
};
// 티켓 옵션 응답 전체 조회
export const usePurchaserAnswers = (ticketId: number | null) => {
return useQuery<{ isSuccess: boolean; result: TicketOptionAnswerResponse }>({
queryKey: ['purchaserAnswers', ticketId],
queryFn: () => {
if (ticketId === null) {
throw new Error('ticketId is required');
}
return readPurchaserAnswers(ticketId);
},
enabled: !!ticketId,
});
};
// 티켓 옵션 응답 개별 조회
export const usePersonalTicketOptionAnswers = (ticketId: number | null) => {
return useQuery<{ isSuccess: boolean; result: PersonalTicketOptionAnswerResponse[] }>({
queryKey: ['personalTicketOptionAnswers', ticketId],
queryFn: () => {
if (ticketId === null) {
throw new Error('ticketId is required');
}
return readPersonalTicketOptionAnswers(ticketId);
},
enabled: !!ticketId,
});
};
// 티켓 옵션 생성 훅
export const useCreateTicketOptionMutation = () => {
const { id } = useParams();
const navigate = useNavigate();
return useMutation({
mutationFn: (data: TicketOptionRequest) => createTicketOption({ ...data, eventId: Number(id) }),
onSuccess: () => {
alert('티켓 옵션이 성공적으로 저장되었습니다.');
navigate(`/dashboard/${id}/ticket/option`);
},
onError: () => {
alert('티켓 옵션 저장에 실패했습니다. 다시 시도해주세요.');
},
});
};
// 티켓 옵션 수정 훅
export const useModifyTicketOptionMutation = () => {
const { id } = useParams();
const navigate = useNavigate();
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ ticketOptionId, data }: { ticketOptionId: number; data: TicketOptionRequest }) =>
modifyTicketOption(ticketOptionId, { ...data, eventId: Number(id) }),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({ queryKey: ['ticketOptions', id] });
queryClient.invalidateQueries({ queryKey: ['ticketOptionDetail', variables.ticketOptionId] });
alert('티켓 옵션이 성공적으로 수정되었습니다.');
navigate(`/dashboard/${id}/ticket/option`);
},
onError: () => {
alert('티켓 옵션 수정에 실패했습니다. 다시 시도해주세요.');
},
});
};
// 티켓 옵션 삭제 훅
export const useDeleteTicketOptionMutation = () => {
const { id } = useParams();
const queryClient = useQueryClient();
return useMutation({
mutationFn: (ticketOptionId: number) => deleteTicketOption(ticketOptionId),
onSuccess: (_, ticketOptionId) => {
// 티켓 옵션 목록과 상세 정보 쿼리 리패칭
queryClient.invalidateQueries({ queryKey: ['ticketOptions', id] });
queryClient.invalidateQueries({ queryKey: ['ticketOptionDetail', ticketOptionId] });
queryClient.invalidateQueries({ queryKey: ['attachedTicketOptions'] });
},
onError: error => {
const errorCode = error.message;
if (errorCode === '이미 응답된 티켓 옵션입니다.') {
alert('한 명 이상의 유저가 응답한 옵션은 삭제할 수 없습니다.');
} else {
alert('티켓 옵션 삭제에 실패했습니다. 다시 시도해주세요.');
}
},
});
};
// 티켓 옵션 목록 조회 훅
export const useGetTicketOptions = () => {
const { id } = useParams();
const eventId = Number(id);
return useQuery<TicketOptionTypeResponse>({
queryKey: ['ticketOptions', id],
queryFn: () => getTicketOptions(eventId),
enabled: !!id,
});
};
// 티켓에 부착된 옵션 목록 조회 훅
export const useGetAttachedTicketOptions = (ticketId: number) => {
return useQuery<TicketOptionTypeResponse>({
queryKey: ['attachedTicketOptions', ticketId],
queryFn: () => getAttachedTicketOptions(ticketId),
enabled: !!ticketId,
});
};
// 티켓 옵션 상세 조회 훅
export const useGetTicketOptionDetail = (ticketOptionId: number) => {
return useQuery<TicketOptionTypeResponse>({
queryKey: ['ticketOptionDetail', ticketOptionId],
queryFn: () => getTicketOptionDetail(ticketOptionId),
enabled: !!ticketOptionId,
});
};
// 티켓 옵션 부착 훅
export const useAttachTicketOptionMutation = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ ticketId, ticketOptionId }: { ticketId: number; ticketOptionId: number }) =>
attachTicketOption(ticketId, ticketOptionId),
onSuccess: (_data, variables) => {
// 티켓별 옵션 목록 쿼리 리패칭
queryClient.invalidateQueries({ queryKey: ['attachedTicketOptions', variables.ticketId] });
},
});
};
// 티켓에 부착된 티켓 옵션 부착 취소 훅
export const useDetachTicketOptionMutation = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ ticketId, ticketOptionId }: { ticketId: number; ticketOptionId: number }) =>
detachTicketOption(ticketId, ticketOptionId),
onSuccess: (_data, variables) => {
// 티켓별 옵션 목록 쿼리 리패칭
queryClient.invalidateQueries({ queryKey: ['attachedTicketOptions', variables.ticketId] });
},
});
};