Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"@bcsdlab/koin": "^0.0.15",
"@bcsdlab/utils": "^0.0.15",
"@next/third-parties": "latest",
"@tanstack/react-query": "^5.90.21",
"@sentry/nextjs": "^10",
"@tanstack/react-query": "^5.28.6",
"axios": "^0.27.2",
"dayjs": "^1.11.12",
"embla-carousel-autoplay": "^8.0.4",
Expand Down
30 changes: 30 additions & 0 deletions src/api/abTest/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { queryOptions } from '@tanstack/react-query';
import { ABTestAssignResponse } from './entity';
import { abTestAssign } from './index';

type DefaultABTestAssignResponse = ABTestAssignResponse | { access_history_id: null; variable_name: string };

const getDefaultABTestResponse = (): DefaultABTestAssignResponse => ({
access_history_id: null,
variable_name: 'default',
});

export const abTestQueryKeys = {
all: ['ab-test'] as const,
assign: (title: string, authorization?: string, accessHistoryId?: string | number | null) =>
[...abTestQueryKeys.all, 'assign', title, authorization ?? '', accessHistoryId ?? ''] as const,
};

export const abTestQueries = {
assign: (title: string, authorization?: string, accessHistoryId?: string | number | null) =>
queryOptions<DefaultABTestAssignResponse>({
queryKey: abTestQueryKeys.assign(title, authorization, accessHistoryId),
queryFn: async () => {
try {
return await abTestAssign(title, authorization || undefined, accessHistoryId);
} catch {
return getDefaultABTestResponse();
}
},
}),
};
77 changes: 77 additions & 0 deletions src/api/articles/mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { mutationOptions, QueryClient } from '@tanstack/react-query';
import {
LostItemArticlesRequestDTO,
ReportItemArticleRequestDTO,
UpdateLostItemArticleRequestDTO,
} from './entity';
import { articleQueryKeys } from './queries';
import {
deleteLostItemArticle,
postBlockLostItemChatroom,
postFoundLostItem,
postLostItemArticle,
postLostItemChatroom,
postReportLostItemArticle,
putLostItemArticle,
} from './index';

const invalidateLostItemAll = (queryClient: QueryClient) =>
queryClient.invalidateQueries({ queryKey: articleQueryKeys.lostItemAll });

const invalidateLostItemChatroomAll = (queryClient: QueryClient) =>
queryClient.invalidateQueries({ queryKey: articleQueryKeys.lostItemChatroomAll });

export const articleMutations = {
createLostItem: (queryClient: QueryClient, token: string) =>
mutationOptions({
mutationFn: async (data: LostItemArticlesRequestDTO) => {
const response = await postLostItemArticle(token, data);
return response.id;
},
onSuccess: () => invalidateLostItemAll(queryClient),
}),

updateLostItem: (queryClient: QueryClient, token: string, articleId: number) =>
mutationOptions({
mutationFn: async (data: UpdateLostItemArticleRequestDTO) => {
const response = await putLostItemArticle(token, articleId, data);
return response.id;
},
onSuccess: () => invalidateLostItemAll(queryClient),
}),

deleteLostItem: (queryClient: QueryClient, token: string) =>
mutationOptions({
mutationFn: (articleId: number) => deleteLostItemArticle(token, articleId),
onSuccess: () => invalidateLostItemAll(queryClient),
}),

reportLostItem: (queryClient: QueryClient, token: string) =>
mutationOptions({
mutationFn: ({ articleId, reports }: { articleId: number; reports: ReportItemArticleRequestDTO['reports'] }) =>
postReportLostItemArticle(token, articleId, { reports }),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: articleQueryKeys.all });
await invalidateLostItemAll(queryClient);
},
}),

toggleLostItemFound: (queryClient: QueryClient, token: string, articleId: number) =>
mutationOptions({
mutationFn: () => postFoundLostItem(token, articleId),
onSuccess: () => queryClient.invalidateQueries({ queryKey: articleQueryKeys.lostItemDetail(articleId) }),
}),

createLostItemChatroom: (queryClient: QueryClient, token: string) =>
mutationOptions({
mutationFn: (articleId: number) => postLostItemChatroom(token, articleId),
onSuccess: () => invalidateLostItemChatroomAll(queryClient),
}),

blockLostItemChatroom: (queryClient: QueryClient, token: string) =>
mutationOptions({
mutationFn: ({ articleId, chatroomId }: { articleId: number; chatroomId: number }) =>
postBlockLostItemChatroom(token, articleId, chatroomId),
onSuccess: () => invalidateLostItemChatroomAll(queryClient),
}),
};
120 changes: 120 additions & 0 deletions src/api/articles/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { infiniteQueryOptions, queryOptions } from '@tanstack/react-query';
import { LostItemArticlesRequest, SearchLostItemArticleRequest } from './entity';
import {
getArticle,
getArticles,
getLostItemChatroomDetail,
getLostItemChatroomList,
getLostItemChatroomMessagesV2,
getHotArticles,
getLostItemArticles,
getLostItemSearch,
getLostItemStat,
getSingleLostItemArticle,
} from './index';

type LostItemInfiniteListParams = Omit<LostItemArticlesRequest, 'page'>;

type LostItemSearchParams = Required<Pick<SearchLostItemArticleRequest, 'query'>> & {
page: number;
limit: number;
};

export const articleQueryKeys = {
all: ['articles'] as const,
listRoot: ['articles', 'list'] as const,
list: (page: string) => [...articleQueryKeys.listRoot, page] as const,
hot: ['articles', 'hot'] as const,
detail: (id: string) => ['articles', 'detail', id] as const,
lostItemAll: ['lostItem'] as const,
lostItemListRoot: ['lostItem', 'list'] as const,
lostItemList: (params: LostItemArticlesRequest) => [...articleQueryKeys.lostItemListRoot, params] as const,
lostItemInfiniteListRoot: ['lostItem', 'infinite-list'] as const,
lostItemInfiniteList: (params: LostItemInfiniteListParams) =>
[...articleQueryKeys.lostItemInfiniteListRoot, params] as const,
lostItemDetail: (articleId: number) => ['lostItem', 'detail', articleId] as const,
lostItemSearch: (params: LostItemSearchParams) => ['lostItem', 'search', params] as const,
lostItemStat: ['lostItem', 'stat'] as const,
lostItemChatroomAll: ['chatroom', 'lost-item'] as const,
lostItemChatroomList: ['chatroom', 'lost-item', 'list'] as const,
lostItemChatroomDetail: (articleId: number | string | null, chatroomId: number | string | null) =>
['chatroom', 'lost-item', 'detail', articleId, chatroomId] as const,
lostItemChatroomMessages: (articleId: number | string | null, chatroomId: number | string | null) =>
['chatroom', 'lost-item', 'messages', articleId, chatroomId] as const,
};

export const articleQueries = {
list: (token: string, page: string) =>
queryOptions({
queryKey: articleQueryKeys.list(page),
queryFn: () => getArticles(token, page),
}),

hot: () =>
queryOptions({
queryKey: articleQueryKeys.hot,
queryFn: getHotArticles,
}),

detail: (id: string) =>
queryOptions({
queryKey: articleQueryKeys.detail(id),
queryFn: () => getArticle(id),
}),

lostItemList: (token: string, params: LostItemArticlesRequest) =>
queryOptions({
queryKey: articleQueryKeys.lostItemList(params),
queryFn: () => getLostItemArticles(token, params),
}),

lostItemInfiniteList: (token: string, params: LostItemInfiniteListParams) =>
infiniteQueryOptions({
queryKey: articleQueryKeys.lostItemInfiniteList(params),
initialPageParam: 1,
queryFn: ({ pageParam }) => getLostItemArticles(token, { ...params, page: pageParam }),
getNextPageParam: (lastPage) => {
if (lastPage.total_page > lastPage.current_page) {
return lastPage.current_page + 1;
}

return undefined;
},
}),

lostItemDetail: (token: string, articleId: number) =>
queryOptions({
queryKey: articleQueryKeys.lostItemDetail(articleId),
queryFn: () => getSingleLostItemArticle(token, articleId),
}),

lostItemSearch: (params: LostItemSearchParams) =>
queryOptions({
queryKey: articleQueryKeys.lostItemSearch(params),
queryFn: () => getLostItemSearch(params),
}),

lostItemStat: () =>
queryOptions({
queryKey: articleQueryKeys.lostItemStat,
queryFn: getLostItemStat,
}),

lostItemChatroomList: (token: string) =>
queryOptions({
queryKey: articleQueryKeys.lostItemChatroomList,
queryFn: () => getLostItemChatroomList(token),
}),

lostItemChatroomDetail: (token: string, articleId: number, chatroomId: number) =>
queryOptions({
queryKey: articleQueryKeys.lostItemChatroomDetail(articleId, chatroomId),
queryFn: () => getLostItemChatroomDetail(token, articleId, chatroomId),
}),

lostItemChatroomMessages: (token: string, articleId: number, chatroomId: number) =>
queryOptions({
queryKey: articleQueryKeys.lostItemChatroomMessages(articleId, chatroomId),
queryFn: () => getLostItemChatroomMessagesV2(token, articleId, chatroomId),
}),
};
34 changes: 34 additions & 0 deletions src/api/auth/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { queryOptions } from '@tanstack/react-query';
import { GeneralUserResponse, UserAcademicInfoResponse, UserResponse } from './entity';
import { getGeneralUser, getUser, getUserAcademicInfo } from './index';

type AuthUserType = 'STUDENT' | 'GENERAL';
type AuthUserInfoResponse = UserResponse | GeneralUserResponse;

const getUserInfo = (token: string, userType: AuthUserType): Promise<AuthUserInfoResponse> => {
if (userType === 'STUDENT') {
return getUser(token);
}

return getGeneralUser(token);
};

export const authQueryKeys = {
all: ['auth'] as const,
userInfo: (token: string, userType: AuthUserType) => [...authQueryKeys.all, 'user-info', token, userType] as const,
userAcademicInfo: (token: string) => [...authQueryKeys.all, 'user-academic-info', token] as const,
};

export const authQueries = {
userInfo: (token: string, userType: AuthUserType) =>
queryOptions<AuthUserInfoResponse | null>({
queryKey: authQueryKeys.userInfo(token, userType),
queryFn: () => (token ? getUserInfo(token, userType) : null),
}),

userAcademicInfo: (token: string) =>
queryOptions<UserAcademicInfoResponse | null>({
queryKey: authQueryKeys.userAcademicInfo(token),
queryFn: () => (token ? getUserAcademicInfo(token) : null),
}),
};
22 changes: 22 additions & 0 deletions src/api/banner/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { queryOptions } from '@tanstack/react-query';
import { getBannerCategoryList, getBanners } from './index';

export const bannerQueryKeys = {
all: ['banner'] as const,
categories: () => [...bannerQueryKeys.all, 'categories'] as const,
list: (categoryId: number) => [...bannerQueryKeys.all, 'list', categoryId] as const,
};

export const bannerQueries = {
categories: () =>
queryOptions({
queryKey: bannerQueryKeys.categories(),
queryFn: getBannerCategoryList,
}),

list: (categoryId: number) =>
queryOptions({
queryKey: bannerQueryKeys.list(categoryId),
queryFn: () => getBanners(categoryId),
}),
};
Loading
Loading