-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseUserHook.ts
More file actions
68 lines (62 loc) · 1.95 KB
/
useUserHook.ts
File metadata and controls
68 lines (62 loc) · 1.95 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
import { useMutation, useQuery } from '@tanstack/react-query';
import { agreeTerms, readUser, sendCertificationCode, updateUser, verifyCertificationCode } from '../api/user';
import { UserInfoRequest, UserInfoResponse } from '../model/userInformation';
import { AxiosError } from 'axios';
export const useUserInfo = (enabled: boolean = true) => {
return useQuery<UserInfoResponse>({
queryKey: ['userInfo'],
queryFn: readUser,
enabled,
});
};
export const useUserUpdate = () => {
return useMutation<UserInfoResponse, Error, UserInfoRequest>({
mutationFn: updateUser,
onError: (error) => {
alert(error.message);
}
});
};
export const useAgreeTerms = () => {
return useMutation({
mutationFn: agreeTerms,
onError: () => {
alert('약관 동의 처리에 실패했습니다.');
}
});
};
// 인증번호 발급
export const useSendCertificationCode = () => {
return useMutation({
mutationFn: (data: { phoneNumber: string }) => sendCertificationCode(data),
onSuccess: () => {
alert('인증번호를 발송했습니다.');
},
onError: (error: AxiosError<any>) => {
if (error.response?.data) {
const allMessages = Object.values(error.response.data.result).join('\n');
alert(allMessages);
} else {
alert(error.message || '인증번호 발송에 실패하였습니다.');
}
},
});
};
// 인증번호 확인
export const useVerifyCertificationCode = () => {
return useMutation({
mutationFn: (params: { phoneNumber: string; certificationCode: string }) =>
verifyCertificationCode(params),
onSuccess: () => {
alert('인증에 성공했습니다.');
},
onError: (error: AxiosError<any>) => {
if (error.response?.data) {
const allMessages = Object.values(error.response.data.result).join('\n');
alert(allMessages);
} else {
alert(error.message || '인증에 실패하였습니다.');
}
},
});
}