Skip to content

Commit ef22e1c

Browse files
committed
refact: 멤버 초대 로직을 훅으로 분리하여 역할 분리
1 parent 9f2efe0 commit ef22e1c

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useQueryClient } from '@tanstack/react-query';
2+
import { useHostInvitation } from './useHostInvitation';
3+
4+
export const useInviteMembers = (hostChannelId: number) => {
5+
const queryClient = useQueryClient();
6+
const { mutate: inviteMember } = useHostInvitation(hostChannelId);
7+
8+
const inviteMembers = (emails: string[], onSuccess?: () => void, onError?: () => void) => {
9+
if (emails.length === 0) {
10+
alert('초대할 이메일을 입력해주세요.');
11+
return;
12+
}
13+
14+
const invitationPromises = emails.map(
15+
email =>
16+
new Promise((resolve, reject) => {
17+
inviteMember(
18+
{ email },
19+
{
20+
onSuccess: resolve,
21+
onError: reject,
22+
}
23+
);
24+
})
25+
);
26+
27+
Promise.all(invitationPromises)
28+
.then(() => {
29+
alert('초대가 전송되었습니다.');
30+
queryClient.invalidateQueries({ queryKey: ['hostInfo', hostChannelId] });
31+
onSuccess?.();
32+
})
33+
.catch(() => {
34+
alert('초대 중 일부 실패했습니다.');
35+
onError?.();
36+
});
37+
};
38+
39+
return { inviteMembers };
40+
};

src/pages/menu/ui/myHost/HostEditPage.tsx

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import TertiaryButton from '../../../../../design-system/ui/buttons/TertiaryButt
77
import { useParams } from 'react-router-dom';
88
import MemberEmailInput from '../../../../features/menu/ui/MemberEmailInput';
99
import useHostChannelInfo from '../../../../entities/host/hook/useHostChannelInfoHook';
10-
import { useQueryClient } from '@tanstack/react-query';
11-
import { useHostInvitation } from '../../../../features/host/hook/useHostInvitation';
1210
import { useHostInfoSave } from '../../../../features/host/hook/useHostInfoHook';
11+
import { useInviteMembers } from '../../../../features/host/hook/useInviteHostHook';
1312

1413
const HostEditPage = () => {
1514
const { id } = useParams<{ id: string }>();
@@ -20,8 +19,7 @@ const HostEditPage = () => {
2019

2120
const hostChannelId = Number(id);
2221
const { data: hostInfo } = useHostChannelInfo(hostChannelId);
23-
const { mutate: inviteMember } = useHostInvitation(hostChannelId);
24-
const queryClient = useQueryClient();
22+
const { inviteMembers } = useInviteMembers(hostChannelId);
2523

2624
const { handleSave } = useHostInfoSave(hostChannelId, hostInfo!, channelDescription);
2725

@@ -37,33 +35,7 @@ const HostEditPage = () => {
3735
const handleInviteMembers = () => {
3836
if (!hostInfo?.result.id) return;
3937

40-
if (emails.length === 0) {
41-
alert('초대할 이메일을 입력해주세요.');
42-
return;
43-
}
44-
45-
const invitationPromises = emails.map(
46-
email =>
47-
new Promise((resolve, reject) => {
48-
inviteMember(
49-
{ email },
50-
{
51-
onSuccess: resolve,
52-
onError: reject,
53-
}
54-
);
55-
})
56-
);
57-
58-
Promise.all(invitationPromises)
59-
.then(() => {
60-
alert('초대가 전송되었습니다.');
61-
setEmails([]);
62-
queryClient.invalidateQueries({ queryKey: ['hostInfo', hostChannelId] }); // ✅ 멤버 목록 리패치
63-
})
64-
.catch(() => {
65-
alert('초대 중 일부 실패했습니다.');
66-
});
38+
inviteMembers(emails, () => setEmails([]));
6739
};
6840

6941
useEffect(() => {

0 commit comments

Comments
 (0)