Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor : rank Api querykey factor #876

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
41 changes: 39 additions & 2 deletions src/api/attendanceApi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { useQuery } from 'react-query';
import axios from 'axios';
import { DateTime } from 'luxon';
import { CallenderChartInfo, TodayAttendInfo, TodayAttendPoint } from './dto';
import {
AttendRankInfo,
CallenderChartInfo,
PageAndSize,
TodayAttendInfo,
TodayAttendPoint,
TodayAttendRank,
} from './dto';

const attendanceKeys = {
base: ['attendances'] as const,
todayAttendancePoint: ['todayAttendancePoint'],
todayAttendanceInfo: ({ memberId }: { memberId: number }) => ['todayAttendanceInfo', memberId],
attendanceInfoList: ({ memberId, year }: { memberId: number; year: number }) => [
'attendanceInfoList',
memberId,
year,
],
publdaze marked this conversation as resolved.
Show resolved Hide resolved
todayAttendanceRank: (param: PageAndSize) => [...attendanceKeys.base, 'todayRank', param] as const,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params로 해야할 것 같습니당

continuousAttendanceRank: () => [...attendanceKeys.base, 'continuousRank'] as const,
};

const useGetTodayAttendancePointQuery = () => {
Expand All @@ -36,4 +46,31 @@ const useGetAttendanceInfoListQuery = ({ memberId, year }: { memberId: number; y
return useQuery<CallenderChartInfo[]>(attendanceKeys.attendanceInfoList({ memberId, year }), fetcher);
};

export { useGetTodayAttendancePointQuery, useGetTodayAttendanceInfoQuery, useGetAttendanceInfoListQuery };
const useGetTodayAttendanceRank = ({ page, size = 10 }: PageAndSize) => {
const params = { page, size };

const fetcher = () =>
axios
.get('/attendances/today-rank', {
params,
})
.then(({ data }) => data);

return useQuery<TodayAttendRank>(attendanceKeys.todayAttendanceRank(params), fetcher, {
keepPreviousData: true,
});
};

const useGetContinuousAttendanceRank = () => {
const fetcher = () => axios.get('/attendances/continuous-rank').then(({ data }) => data);

return useQuery<AttendRankInfo[]>(attendanceKeys.continuousAttendanceRank(), fetcher);
};

export {
useGetTodayAttendancePointQuery,
useGetTodayAttendanceInfoQuery,
useGetAttendanceInfoListQuery,
useGetTodayAttendanceRank,
useGetContinuousAttendanceRank,
};
23 changes: 18 additions & 5 deletions src/api/memberApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import axios from 'axios';
import { PASSWORD } from '@constants/apiResponseMessage';
import { useApiError } from '@hooks/useGetApiError';
import { formatGeneration } from '@utils/converter';
import { ProfileInfo, MemberDetailInfo } from './dto';
import { ProfileInfo, MemberDetailInfo, PageAndSize, PointRank } from './dto';

const memberKeys = {
memberList: ['member', 'memberList'] as const,
base: ['member'] as const,
memberList: () => [...memberKeys.base, 'memberList'] as const,
pointRank: (params: PageAndSize) => [...memberKeys.base, 'pointRank', params] as const,
};
const profileKeys = {
profileInfo: (memberId: number) => ['profile', 'profileInfo', memberId] as const,
Expand All @@ -29,7 +31,7 @@ const useGetMembersQuery = ({
};
});
});
return useQuery<MemberDetailInfo[]>(memberKeys.memberList, fetcher, {
return useQuery<MemberDetailInfo[]>(memberKeys.memberList(), fetcher, {
onSuccess,
});
};
Expand Down Expand Up @@ -175,7 +177,7 @@ const useEditMemberTypeMutation = () => {

return useMutation(fetcher, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: memberKeys.memberList });
queryClient.invalidateQueries({ queryKey: memberKeys.memberList() });
},
});
};
Expand All @@ -186,11 +188,21 @@ const useDeleteMemberMutation = () => {

return useMutation(fetcher, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: memberKeys.memberList });
queryClient.invalidateQueries({ queryKey: memberKeys.memberList() });
},
});
};

const useGetPointRank = ({ page, size = 10 }: PageAndSize) => {
const params = { page, size };

const fetcher = () => axios.get('/members/point-rank', { params }).then(({ data }) => data);

return useQuery<PointRank>(memberKeys.pointRank(params), fetcher, {
keepPreviousData: true,
});
};

export {
useGetMembersQuery,
useGetProfileQuery,
Expand All @@ -204,4 +216,5 @@ export {
useWithdrawalMutation,
useEditMemberTypeMutation,
useDeleteMemberMutation,
useGetPointRank,
};
45 changes: 0 additions & 45 deletions src/api/rankApi.ts

This file was deleted.

18 changes: 17 additions & 1 deletion src/pages/Game/Baseball/api/baseballApi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import toast from 'react-hot-toast';
import { useQuery, useMutation, useQueryClient } from 'react-query';
import axios from 'axios';
import { GameRankInfo } from '@api/dto';
import { gameKeys } from '@api/gameApi';
import { useApiError } from '@hooks/useGetApiError';
import { GameInfo, GameResultInfo, GameStatus } from './baseballDto';

export const baseballKeys = {
base: ['game'] as const,
game_info: ['game_info'] as const,
status: ['status'] as const,
result: ['result'] as const,
gameRank: () => [...baseballKeys.base, 'rank'] as const,
};

const useGetGameInfoQuery = () => {
Expand Down Expand Up @@ -69,4 +72,17 @@ const useGetResultQuery = () => {
return useQuery<GameResultInfo>(baseballKeys.result, fetcher);
};

export { useGetGameInfoQuery, useGetBaseBallStatusQuery, useGameStartMutation, useGuessMutation, useGetResultQuery };
const useGetGameRank = () => {
const fetcher = () => axios.get('/game/rank').then(({ data }) => data);

return useQuery<GameRankInfo[]>(baseballKeys.gameRank(), fetcher);
};

export {
useGetGameInfoQuery,
useGetBaseBallStatusQuery,
useGameStartMutation,
useGuessMutation,
useGetResultQuery,
useGetGameRank,
};
9 changes: 3 additions & 6 deletions src/pages/rank/Rank.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { Typography } from '@mui/material';
import { useGetContinuousAttendanceRank, useGetTodayAttendanceRank } from '@api/attendanceApi';
import { AttendRankInfo, GameRankInfo } from '@api/dto';
import {
useGetContinuousAttendanceRank,
useGetGameRank,
useGetPointRank,
useGetTodayAttendanceRank,
} from '@api/rankApi';
import { useGetPointRank } from '@api/memberApi';
import usePagination from '@hooks/usePagination';
import { formatGeneration } from '@utils/converter';
import { useGetGameRank } from '@pages/Game/Baseball/api/baseballApi';
import ServerAvatar from '@components/Avatar/ServerAvatar';
import OutlinedButton from '@components/Button/OutlinedButton';
import StandardTab from '@components/Tab/StandardTab';
Expand Down
Loading