-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseInviteHostHook.ts
More file actions
54 lines (44 loc) · 1.58 KB
/
useInviteHostHook.ts
File metadata and controls
54 lines (44 loc) · 1.58 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
import { useQueryClient } from '@tanstack/react-query';
import { useHostInvitation } from './useHostInvitation';
export const useInviteMembers = (hostChannelId: number) => {
const queryClient = useQueryClient();
const { mutate: inviteMember } = useHostInvitation(hostChannelId);
const inviteMembers = (emails: string[], onSuccess?: () => void, onError?: () => void) => {
if (emails.length === 0) {
alert('초대할 이메일을 입력해주세요.');
return;
}
const invitationPromises = emails.map(
email =>
new Promise<void>((resolve, reject) => {
inviteMember(
{ email },
{
onSuccess: () => resolve(),
onError: error => reject(error),
}
);
})
);
Promise.allSettled(invitationPromises).then(results => {
const failed = results.filter(r => r.status === 'rejected') as PromiseRejectedResult[];
if (failed.length > 0) {
console.log('🚨 first error:', failed[0].reason);
}
if (failed.length === 0) {
alert('초대가 전송되었습니다.');
queryClient.invalidateQueries({ queryKey: ['hostInfo', hostChannelId] });
onSuccess?.();
return;
}
const firstError = failed[0].reason;
const errorMessage =
firstError?.response?.data?.message || // AxiosError일 경우
firstError?.message || // 일반 에러 객체일 경우
'알 수 없는 오류가 발생했습니다.';
alert(errorMessage);
onError?.();
});
};
return { inviteMembers };
};