Skip to content

Commit e5314bb

Browse files
authored
[공통] querykey options factory 적용 (#1210)
1 parent fd763d1 commit e5314bb

205 files changed

Lines changed: 2427 additions & 1971 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pnp.cjs

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"@bcsdlab/koin": "^0.0.15",
77
"@bcsdlab/utils": "^0.0.15",
88
"@next/third-parties": "latest",
9+
"@tanstack/react-query": "^5.90.21",
910
"@sentry/nextjs": "^10",
10-
"@tanstack/react-query": "^5.28.6",
1111
"axios": "^0.27.2",
1212
"dayjs": "^1.11.12",
1313
"embla-carousel-autoplay": "^8.0.4",

src/api/abTest/queries.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { queryOptions } from '@tanstack/react-query';
2+
import { ABTestAssignResponse } from './entity';
3+
import { abTestAssign } from './index';
4+
5+
type DefaultABTestAssignResponse = ABTestAssignResponse | { access_history_id: null; variable_name: string };
6+
7+
const getDefaultABTestResponse = (): DefaultABTestAssignResponse => ({
8+
access_history_id: null,
9+
variable_name: 'default',
10+
});
11+
12+
export const abTestQueryKeys = {
13+
all: ['ab-test'] as const,
14+
assign: (title: string, authorization?: string, accessHistoryId?: string | number | null) =>
15+
[...abTestQueryKeys.all, 'assign', title, authorization ?? '', accessHistoryId ?? ''] as const,
16+
};
17+
18+
export const abTestQueries = {
19+
assign: (title: string, authorization?: string, accessHistoryId?: string | number | null) =>
20+
queryOptions<DefaultABTestAssignResponse>({
21+
queryKey: abTestQueryKeys.assign(title, authorization, accessHistoryId),
22+
queryFn: async () => {
23+
try {
24+
return await abTestAssign(title, authorization || undefined, accessHistoryId);
25+
} catch {
26+
return getDefaultABTestResponse();
27+
}
28+
},
29+
}),
30+
};

src/api/articles/mutations.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { mutationOptions, QueryClient } from '@tanstack/react-query';
2+
import {
3+
LostItemArticlesRequestDTO,
4+
ReportItemArticleRequestDTO,
5+
UpdateLostItemArticleRequestDTO,
6+
} from './entity';
7+
import { articleQueryKeys } from './queries';
8+
import {
9+
deleteLostItemArticle,
10+
postBlockLostItemChatroom,
11+
postFoundLostItem,
12+
postLostItemArticle,
13+
postLostItemChatroom,
14+
postReportLostItemArticle,
15+
putLostItemArticle,
16+
} from './index';
17+
18+
const invalidateLostItemAll = (queryClient: QueryClient) =>
19+
queryClient.invalidateQueries({ queryKey: articleQueryKeys.lostItemAll });
20+
21+
const invalidateLostItemChatroomAll = (queryClient: QueryClient) =>
22+
queryClient.invalidateQueries({ queryKey: articleQueryKeys.lostItemChatroomAll });
23+
24+
export const articleMutations = {
25+
createLostItem: (queryClient: QueryClient, token: string) =>
26+
mutationOptions({
27+
mutationFn: async (data: LostItemArticlesRequestDTO) => {
28+
const response = await postLostItemArticle(token, data);
29+
return response.id;
30+
},
31+
onSuccess: () => invalidateLostItemAll(queryClient),
32+
}),
33+
34+
updateLostItem: (queryClient: QueryClient, token: string, articleId: number) =>
35+
mutationOptions({
36+
mutationFn: async (data: UpdateLostItemArticleRequestDTO) => {
37+
const response = await putLostItemArticle(token, articleId, data);
38+
return response.id;
39+
},
40+
onSuccess: () => invalidateLostItemAll(queryClient),
41+
}),
42+
43+
deleteLostItem: (queryClient: QueryClient, token: string) =>
44+
mutationOptions({
45+
mutationFn: (articleId: number) => deleteLostItemArticle(token, articleId),
46+
onSuccess: () => invalidateLostItemAll(queryClient),
47+
}),
48+
49+
reportLostItem: (queryClient: QueryClient, token: string) =>
50+
mutationOptions({
51+
mutationFn: ({ articleId, reports }: { articleId: number; reports: ReportItemArticleRequestDTO['reports'] }) =>
52+
postReportLostItemArticle(token, articleId, { reports }),
53+
onSuccess: async () => {
54+
await queryClient.invalidateQueries({ queryKey: articleQueryKeys.all });
55+
await invalidateLostItemAll(queryClient);
56+
},
57+
}),
58+
59+
toggleLostItemFound: (queryClient: QueryClient, token: string, articleId: number) =>
60+
mutationOptions({
61+
mutationFn: () => postFoundLostItem(token, articleId),
62+
onSuccess: () => queryClient.invalidateQueries({ queryKey: articleQueryKeys.lostItemDetail(articleId) }),
63+
}),
64+
65+
createLostItemChatroom: (queryClient: QueryClient, token: string) =>
66+
mutationOptions({
67+
mutationFn: (articleId: number) => postLostItemChatroom(token, articleId),
68+
onSuccess: () => invalidateLostItemChatroomAll(queryClient),
69+
}),
70+
71+
blockLostItemChatroom: (queryClient: QueryClient, token: string) =>
72+
mutationOptions({
73+
mutationFn: ({ articleId, chatroomId }: { articleId: number; chatroomId: number }) =>
74+
postBlockLostItemChatroom(token, articleId, chatroomId),
75+
onSuccess: () => invalidateLostItemChatroomAll(queryClient),
76+
}),
77+
};

src/api/articles/queries.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { infiniteQueryOptions, queryOptions } from '@tanstack/react-query';
2+
import { LostItemArticlesRequest, SearchLostItemArticleRequest } from './entity';
3+
import {
4+
getArticle,
5+
getArticles,
6+
getLostItemChatroomDetail,
7+
getLostItemChatroomList,
8+
getLostItemChatroomMessagesV2,
9+
getHotArticles,
10+
getLostItemArticles,
11+
getLostItemSearch,
12+
getLostItemStat,
13+
getSingleLostItemArticle,
14+
} from './index';
15+
16+
type LostItemInfiniteListParams = Omit<LostItemArticlesRequest, 'page'>;
17+
18+
type LostItemSearchParams = Required<Pick<SearchLostItemArticleRequest, 'query'>> & {
19+
page: number;
20+
limit: number;
21+
};
22+
23+
export const articleQueryKeys = {
24+
all: ['articles'] as const,
25+
listRoot: ['articles', 'list'] as const,
26+
list: (page: string) => [...articleQueryKeys.listRoot, page] as const,
27+
hot: ['articles', 'hot'] as const,
28+
detail: (id: string) => ['articles', 'detail', id] as const,
29+
lostItemAll: ['lostItem'] as const,
30+
lostItemListRoot: ['lostItem', 'list'] as const,
31+
lostItemList: (params: LostItemArticlesRequest) => [...articleQueryKeys.lostItemListRoot, params] as const,
32+
lostItemInfiniteListRoot: ['lostItem', 'infinite-list'] as const,
33+
lostItemInfiniteList: (params: LostItemInfiniteListParams) =>
34+
[...articleQueryKeys.lostItemInfiniteListRoot, params] as const,
35+
lostItemDetail: (articleId: number) => ['lostItem', 'detail', articleId] as const,
36+
lostItemSearch: (params: LostItemSearchParams) => ['lostItem', 'search', params] as const,
37+
lostItemStat: ['lostItem', 'stat'] as const,
38+
lostItemChatroomAll: ['chatroom', 'lost-item'] as const,
39+
lostItemChatroomList: ['chatroom', 'lost-item', 'list'] as const,
40+
lostItemChatroomDetail: (articleId: number | string | null, chatroomId: number | string | null) =>
41+
['chatroom', 'lost-item', 'detail', articleId, chatroomId] as const,
42+
lostItemChatroomMessages: (articleId: number | string | null, chatroomId: number | string | null) =>
43+
['chatroom', 'lost-item', 'messages', articleId, chatroomId] as const,
44+
};
45+
46+
export const articleQueries = {
47+
list: (token: string, page: string) =>
48+
queryOptions({
49+
queryKey: articleQueryKeys.list(page),
50+
queryFn: () => getArticles(token, page),
51+
}),
52+
53+
hot: () =>
54+
queryOptions({
55+
queryKey: articleQueryKeys.hot,
56+
queryFn: getHotArticles,
57+
}),
58+
59+
detail: (id: string) =>
60+
queryOptions({
61+
queryKey: articleQueryKeys.detail(id),
62+
queryFn: () => getArticle(id),
63+
}),
64+
65+
lostItemList: (token: string, params: LostItemArticlesRequest) =>
66+
queryOptions({
67+
queryKey: articleQueryKeys.lostItemList(params),
68+
queryFn: () => getLostItemArticles(token, params),
69+
}),
70+
71+
lostItemInfiniteList: (token: string, params: LostItemInfiniteListParams) =>
72+
infiniteQueryOptions({
73+
queryKey: articleQueryKeys.lostItemInfiniteList(params),
74+
initialPageParam: 1,
75+
queryFn: ({ pageParam }) => getLostItemArticles(token, { ...params, page: pageParam }),
76+
getNextPageParam: (lastPage) => {
77+
if (lastPage.total_page > lastPage.current_page) {
78+
return lastPage.current_page + 1;
79+
}
80+
81+
return undefined;
82+
},
83+
}),
84+
85+
lostItemDetail: (token: string, articleId: number) =>
86+
queryOptions({
87+
queryKey: articleQueryKeys.lostItemDetail(articleId),
88+
queryFn: () => getSingleLostItemArticle(token, articleId),
89+
}),
90+
91+
lostItemSearch: (params: LostItemSearchParams) =>
92+
queryOptions({
93+
queryKey: articleQueryKeys.lostItemSearch(params),
94+
queryFn: () => getLostItemSearch(params),
95+
}),
96+
97+
lostItemStat: () =>
98+
queryOptions({
99+
queryKey: articleQueryKeys.lostItemStat,
100+
queryFn: getLostItemStat,
101+
}),
102+
103+
lostItemChatroomList: (token: string) =>
104+
queryOptions({
105+
queryKey: articleQueryKeys.lostItemChatroomList,
106+
queryFn: () => getLostItemChatroomList(token),
107+
}),
108+
109+
lostItemChatroomDetail: (token: string, articleId: number, chatroomId: number) =>
110+
queryOptions({
111+
queryKey: articleQueryKeys.lostItemChatroomDetail(articleId, chatroomId),
112+
queryFn: () => getLostItemChatroomDetail(token, articleId, chatroomId),
113+
}),
114+
115+
lostItemChatroomMessages: (token: string, articleId: number, chatroomId: number) =>
116+
queryOptions({
117+
queryKey: articleQueryKeys.lostItemChatroomMessages(articleId, chatroomId),
118+
queryFn: () => getLostItemChatroomMessagesV2(token, articleId, chatroomId),
119+
}),
120+
};

src/api/auth/queries.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { queryOptions } from '@tanstack/react-query';
2+
import { GeneralUserResponse, UserAcademicInfoResponse, UserResponse } from './entity';
3+
import { getGeneralUser, getUser, getUserAcademicInfo } from './index';
4+
5+
type AuthUserType = 'STUDENT' | 'GENERAL';
6+
type AuthUserInfoResponse = UserResponse | GeneralUserResponse;
7+
8+
const getUserInfo = (token: string, userType: AuthUserType): Promise<AuthUserInfoResponse> => {
9+
if (userType === 'STUDENT') {
10+
return getUser(token);
11+
}
12+
13+
return getGeneralUser(token);
14+
};
15+
16+
export const authQueryKeys = {
17+
all: ['auth'] as const,
18+
userInfo: (token: string, userType: AuthUserType) => [...authQueryKeys.all, 'user-info', token, userType] as const,
19+
userAcademicInfo: (token: string) => [...authQueryKeys.all, 'user-academic-info', token] as const,
20+
};
21+
22+
export const authQueries = {
23+
userInfo: (token: string, userType: AuthUserType) =>
24+
queryOptions<AuthUserInfoResponse | null>({
25+
queryKey: authQueryKeys.userInfo(token, userType),
26+
queryFn: () => (token ? getUserInfo(token, userType) : null),
27+
}),
28+
29+
userAcademicInfo: (token: string) =>
30+
queryOptions<UserAcademicInfoResponse | null>({
31+
queryKey: authQueryKeys.userAcademicInfo(token),
32+
queryFn: () => (token ? getUserAcademicInfo(token) : null),
33+
}),
34+
};

0 commit comments

Comments
 (0)