-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseEmailHook.ts
More file actions
71 lines (66 loc) · 2.44 KB
/
useEmailHook.ts
File metadata and controls
71 lines (66 loc) · 2.44 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
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { sendEmail, editEmail, readEmail, deleteEmail, readPurchaserEmails } from '../api/mail';
import { EmailRequest, EmailResponse, ReadEmailResponse } from '../model/email';
import { useNavigate, useParams } from 'react-router-dom';
import { useEmailStore } from '../model/store/EmailStore';
export const useReadEmail = (eventId: number, status: 'PENDING' | 'SENT') => {
return useQuery<ReadEmailResponse[]>({
queryKey: ['emails', eventId, status],
queryFn: () => readEmail(eventId, status),
enabled: !!eventId && !!status,
});
};
export const usePurchaserEmails = () => {
return useMutation({
mutationFn: ({ eventId, ticketId }: { eventId: number; ticketId?: number }) =>
readPurchaserEmails(eventId, ticketId),
onError: () => {
alert('이메일을 불러오는 데 실패했습니다.');
},
});
};
export const useSendEmail = () => {
const { id } = useParams();
const navigate = useNavigate();
const { reset } = useEmailStore();
return useMutation<EmailResponse, Error, EmailRequest>({
mutationFn: sendEmail,
onSuccess: () => {
reset();
alert('예약 메일이 성공적으로 발송되었습니다!');
navigate(`/dashboard/${id}/mailBox`);
},
onError: () => {
alert(`메일 전송에 실패했습니다. 다시 시도해 주세요.`);
},
});
};
export const useEditEmail = () => {
const { id } = useParams();
const navigate = useNavigate();
const { reset } = useEmailStore();
return useMutation<EmailResponse, Error, { reservationEmailId: number; data: EmailRequest }>({
mutationFn: ({ reservationEmailId, data }) => editEmail(reservationEmailId, data),
onSuccess: () => {
reset();
alert('예약 메일이 성공적으로 수정되었습니다!');
navigate(`/dashboard/${id}/mailBox`);
},
onError: () => {
alert('메일 수정에 실패했습니다. 다시 시도해 주세요.');
},
});
};
export const useDeleteEmail = () => {
const queryClient = useQueryClient();
return useMutation<EmailResponse, Error, number>({
mutationFn: reservationEmailId => deleteEmail(reservationEmailId),
onSuccess: () => {
alert('메일이 삭제되었습니다.');
queryClient.invalidateQueries({ queryKey: ['emails'] });
},
onError: () => {
alert('메일 삭제에 실패했습니다. 다시 시도해 주세요.');
},
});
};