-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser-mutations.ts
More file actions
95 lines (88 loc) · 3.33 KB
/
user-mutations.ts
File metadata and controls
95 lines (88 loc) · 3.33 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
import { get, patch, post, put } from '@apis/base/http';
import { END_POINT } from '@constants/api';
import { USER_KEY } from '@constants/query-key';
import { HTTP_STATUS } from '@constants/response';
import queryClient from '@libs/query-client';
import { NICKNAME_DUPLICATE_FAILURE_COUNT } from '@pages/sign-up/constants/validation';
import { router } from '@routes/router';
import { ROUTES } from '@routes/routes-config';
import { mutationOptions } from '@tanstack/react-query';
import axios from 'axios';
import type { responseTypes } from '@/shared/types/base-types';
import type {
postAgreementInfoRequest,
postEditProfileRequest,
postMatchConditionRequest,
postUserInfoRequest,
UserInfoResponse,
} from '@/shared/types/user-types';
export const userMutations = {
USER_INFO: () =>
mutationOptions<UserInfoResponse, Error, postUserInfoRequest>({
mutationKey: USER_KEY.INFO(),
mutationFn: ({ nickname, introduction, birthYear, gender }) =>
post(END_POINT.USER_INFO, { nickname, introduction, birthYear, gender }),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: USER_KEY.ALL });
router.navigate(ROUTES.HOME, { replace: true });
},
onError: (err) => {
console.error(err);
},
}),
LOGOUT: () =>
mutationOptions<responseTypes, Error, void>({
mutationKey: USER_KEY.LOGOUT(),
mutationFn: () => post(END_POINT.POST_AUTH_LOGOUT),
onSuccess: async () => {
await queryClient.cancelQueries({ queryKey: USER_KEY.ALL });
queryClient.removeQueries({ queryKey: USER_KEY.ALL });
},
onError: (err) => {
console.error('로그아웃 실패', err);
},
}),
EDIT_PROFILE: () =>
mutationOptions<responseTypes, Error, postEditProfileRequest>({
mutationKey: USER_KEY.EDIT_PROFILE(),
mutationFn: ({ field, value }) => put(END_POINT.POST_EDIT_PROFILE, { field, value }),
onSuccess: async () => {
queryClient.invalidateQueries({ queryKey: USER_KEY.ALL });
window.location.reload();
},
onError: (err) => {
console.error('수정에 실패했어요', err);
},
}),
EDIT_MATCH_CONDITION: () =>
mutationOptions<postMatchConditionRequest, Error, postMatchConditionRequest>({
mutationKey: USER_KEY.MATCH_CONDITION(),
mutationFn: ({ team, teamAllowed, style, avgSeason }) =>
patch(END_POINT.MATCH_CONDITION, { team, teamAllowed, style, avgSeason }),
}),
AGREEMENT_INFO: () =>
mutationOptions<responseTypes, Error, postAgreementInfoRequest>({
mutationKey: USER_KEY.AGREEMENT(),
mutationFn: ({ hasAccepted }) => post(END_POINT.AGREEMENT_INFO, { hasAccepted }),
}),
NICKNAME_CHECK: () =>
mutationOptions<boolean, Error, { nickname: string }>({
mutationFn: async ({ nickname }) => {
try {
await get<void>(END_POINT.GET_NICKNAME_CHECK(nickname));
return true;
} catch (e) {
if (axios.isAxiosError(e) && e.response?.status === HTTP_STATUS.CONFLICT) {
return false;
}
throw e;
}
},
retry: (failureCount, error) => {
if (axios.isAxiosError(error) && error.response?.status === HTTP_STATUS.CONFLICT) {
return false;
}
return failureCount < NICKNAME_DUPLICATE_FAILURE_COUNT;
},
}),
};