Skip to content

Commit

Permalink
Adjust chesscom API
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyef committed Sep 17, 2024
1 parent ce90429 commit 9f1ab50
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/ChessComStats/hooks/useMatchesSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Params = {
};

export const useMatchesSummary = ({ matches, username }: Params) => {
if (!matches) {
if (!matches || matches.length === 0) {
return null;
}

Expand Down
10 changes: 7 additions & 3 deletions src/components/ChessComStats/hooks/useStats.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useQueries } from 'react-query';

import { RawAllTimeStats, RawMatchData, RawProfileStats } from 'types/chesscom';
import {
RawAllTimeStats,
RawProfileStats,
RawRecentMatchesResponse,
} from 'types/chesscom';

// @ts-expect-error
// eslint-disable-next-line
Expand Down Expand Up @@ -42,7 +46,7 @@ const fetchMatchData = async () => {

const data = await response.json();

return data as Array<RawMatchData>;
return data as RawRecentMatchesResponse;
};

type Params = {
Expand Down Expand Up @@ -70,6 +74,6 @@ export const useStats = ({ userId }: Params) => {

return {
stats: stats.data?.stats,
matches: matches.data,
matches: matches.data?.games,
};
};
4 changes: 2 additions & 2 deletions src/pages/api/chesscom/recent-matches.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';

import { RawMatchData } from 'types/chesscom';
import { RawRecentMatchesResponse } from 'types/chesscom';

import { chessComUserId } from '@/utils/constants';

Expand All @@ -17,7 +17,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
throw new Error(response.statusText);
}

const data = (await response.json()) as RawMatchData;
const data = (await response.json()) as RawRecentMatchesResponse;

// set cache-control header for 10 minutes
if (process.env.NODE_ENV === 'production') {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const publicUrl = process.env.NEXT_PUBLIC_URL;

// This is my chess.com information
// If you are copying this, remember to change this.
export const chessComUserId = '344047395'; // '288942993'; old banned account
export const chessComUserId = '344047395';
export const chessComUsername = 'PixelParser';
5 changes: 5 additions & 0 deletions types/chesscom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ export type RawMatchData = {
user2Accuracy: number | null;
};

export type RawRecentMatchesResponse = {
games: RawMatchData[];
totalGames: number;
};

export type GameType = {
name: Name;
code: Code;
Expand Down

0 comments on commit 9f1ab50

Please sign in to comment.