-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathqueries.ts
More file actions
119 lines (102 loc) · 3.62 KB
/
queries.ts
File metadata and controls
119 lines (102 loc) · 3.62 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { infiniteQueryOptions, mutationOptions } from '@tanstack/react-query';
import { END_POINT } from '@shared/api/config/end-point.ts';
import { api } from '@shared/api/config/instance';
import {
AUTH_MUTATION_KEY,
USER_MUTATION_KEY,
USER_QUERY_KEY,
} from '@shared/api/keys/query-key.ts';
import {
KakaoLogoutResponse,
KakaoWithdrawResponse,
MePostResponse,
UserProfileEditRequestBody,
UserProfileEditResponse,
} from '@shared/api/types/types';
// =============================================================================
// QUERY OPTIONS
// =============================================================================
export const USER_QUERY_OPTIONS = {
ME_POSTS: () =>
infiniteQueryOptions({
queryKey: USER_QUERY_KEY.ME_POSTS(),
queryFn: ({ pageParam = 0 }) => getMePosts({ pageParam }),
getNextPageParam: (lastPage) =>
lastPage.isLast ? undefined : lastPage.nextCursor,
initialPageParam: 0,
}),
ME_COMMENTS: () =>
infiniteQueryOptions({
queryKey: USER_QUERY_KEY.ME_COMMENTS(),
queryFn: ({ pageParam = 0 }) => getMeComments({ pageParam }),
getNextPageParam: (lastPage) =>
lastPage.isLast ? undefined : lastPage.nextCursor,
initialPageParam: 0,
}),
};
// =============================================================================
// QUERY FUNCTIONS
// =============================================================================
export const getMePosts = async ({ pageParam }: { pageParam: number }) => {
const url =
pageParam === 0
? `${END_POINT.USER.GET_ME_POSTS}?size=10`
: `${END_POINT.USER.GET_ME_POSTS}?cursorId=${pageParam}&size=10`;
const response = await api.get(url).json<MePostResponse>();
return response.data;
};
export const getMeComments = async ({ pageParam }: { pageParam: number }) => {
const url =
pageParam === 0
? `${END_POINT.USER.GET_ME_COMMENTS}?size=10`
: `${END_POINT.USER.GET_ME_COMMENTS}?cursorId=${pageParam}&size=10`;
const response = await api.get(url).json<MePostResponse>();
return response.data;
};
// =============================================================================
// MUTATION OPTIONS
// =============================================================================
export const USER_MUTATION_OPTIONS = {
PATCH_USER_PROFILE: () => {
return mutationOptions({
mutationKey: USER_MUTATION_KEY.USER_PROFILE(),
mutationFn: ({ body }: { body: UserProfileEditRequestBody }) =>
patchUserProfile(body),
});
},
};
export const AUTH_MUTATION_OPTIONS = {
KAKAO_LOGOUT: () => {
return mutationOptions({
mutationKey: AUTH_MUTATION_KEY.KAKAO_LOGOUT(),
mutationFn: kakaoLogout,
});
},
KAKAO_WITHDRAW: () => {
return mutationOptions({
mutationKey: AUTH_MUTATION_KEY.KAKAO_WITHDRAW(),
mutationFn: kakaoWithdraw,
});
},
};
// =============================================================================
// MUTATION FUNCTIONS
// =============================================================================
export const patchUserProfile = async (data: UserProfileEditRequestBody) => {
const response = await api
.patch(END_POINT.USER.PATCH_USER_INFO, { json: data })
.json<UserProfileEditResponse>();
return response;
};
export const kakaoLogout = async (redirectUrl: string) => {
const response = await api
.post(`${END_POINT.AUTH.KAKAO_LOGOUT}?redirect-url=${redirectUrl}`)
.json<KakaoLogoutResponse>();
return response;
};
export const kakaoWithdraw = async () => {
const response = await api
.delete(END_POINT.AUTH.KAKAO_WITHDRAW)
.json<KakaoWithdrawResponse>();
return response;
};