Skip to content

Commit

Permalink
fix: leaderboard fetch 'undefined' issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jongwooha98 committed Dec 20, 2021
1 parent abf3371 commit 2460b75
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
73 changes: 62 additions & 11 deletions src/pages/Game2048/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
useLeaderboard,
useMini,
useUser,
useMyGameData,
useAccessToken,
UserLeaderboardType,
} from 'hooks';
Expand Down Expand Up @@ -43,14 +42,14 @@ export const Home: React.FC = () => {
const { accessToken } = useAccessToken();
const { isInWebEnvironment, handleThirdPartyAgreement } = useMini();
const { user, town } = useUser();
const { rank, gameType } = useMyGame2048Data();
const { rank, gameType, updateMyScore, updateMyComment } =
useMyGame2048Data();
const {
// townLeaderboard,
// userLeaderboard,
getUserLeaderboard,
getTownLeaderboard,
} = useLeaderboard();
const { updateMyGameData } = useMyGameData();
const [townLeaderboard, setTownLeaderboard] = useState<TownLeaderboardType[]>(
[]
);
Expand Down Expand Up @@ -143,6 +142,64 @@ export const Home: React.FC = () => {
}
};

// const getMyGameData = useCallback(
// async ({ gameType }: { gameType: 'GAME_KARROT' | 'GAME_2048' }) => {
// const {
// data: { data },
// } = await minigameApi.gameUserApi.getMyRankInfoUsingGET(gameType);
// console.log('2', data);
// if (data) {
// console.log('2-1', data);
// if (data.score && data.rank) {
// console.log('2-1', data.score, data.rank);
// updateMyScore({
// score: data.score,
// rank: data.rank,
// });
// if (data.comment) {
// updateMyComment(data.comment);
// }

// return data;
// }
// }
// return undefined;
// },
// [minigameApi.gameUserApi, updateMyComment, updateMyScore]
// );

const getMyGameData = useCallback(
async ({ gameType }: { gameType: 'GAME_KARROT' | 'GAME_2048' }) => {
const {
data: { data },
} = await minigameApi.gameUserApi.getMyRankInfoUsingGET(gameType);

return data;
},
[minigameApi.gameUserApi]
);
const updateMyGameData = useCallback(
async ({ gameType }: { gameType: 'GAME_KARROT' | 'GAME_2048' }) => {
getMyGameData({ gameType }).then((response) => {
if (response) {
updateMyScore({
score: response.score,
rank: response.rank,
});
if (response.comment) {
updateMyComment(response.comment);
}

setMyData({
rank: response.rank,
score: response.score,
});
}
});
},
[getMyGameData, updateMyComment, updateMyScore]
);

const updateMyTownData = useCallback(
async ({
townLeaderboard,
Expand Down Expand Up @@ -204,14 +261,8 @@ export const Home: React.FC = () => {
size: number;
}) => {
updateMyGameData({ gameType })
.then((response) => {
if (response) {
setMyData({
rank: response.rank,
score: response.score,
});
updateLeaderboard({ gameType, size });
}
.then(() => {
updateLeaderboard({ gameType, size });
})
.catch((error) => {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React from 'react';
import { css } from '@emotion/css';
import styled from '@emotion/styled';
import { Tabs } from '@karrotframe/tabs';
Expand All @@ -19,9 +19,6 @@ type Props = {
export const LeaderboardTabs: React.FC<Props> = (props) => {
const [activeTabKey, setActiveTabKey] = useState<string>('town');

useEffect(() => {
console.log('asdf', props.userLeaderboard, props.townLeaderboard);
});
return (
<LeaderboardContainer className="game-2048__tabs">
<Tabs
Expand Down

0 comments on commit 2460b75

Please sign in to comment.