diff --git a/app/public/na.webp b/app/public/na.webp new file mode 100644 index 0000000..8aff666 Binary files /dev/null and b/app/public/na.webp differ diff --git a/app/src/app/page.tsx b/app/src/app/page.tsx index 8520d14..176fbd4 100644 --- a/app/src/app/page.tsx +++ b/app/src/app/page.tsx @@ -1,30 +1,30 @@ import { - fetchLeagueDetails, fetchCurrentGames, fetchTestGames, + fetchCurrentLeagues, } from '@/lib/fetch-data' import { ScrollArea } from '@/components/ui/scroll-area' import LeagueSection from '@/components/league-section' import GameCard from '@/components/game-card' -import { leaguesData, leagueIds, currentSeason } from '@/config/api' +import { leaguesData, currentSeason } from '@/config/api' import { Sport, Game } from '@/types' +import { setLeaguesIds } from '@/lib/server-context' export default async function Home({ searchParams, }: { searchParams: { [key: string]: string | string[] | undefined } }) { + const currentLeagues = await fetchCurrentLeagues(Sport.Rugby) + setLeaguesIds(currentLeagues.map((l) => l.id)) const data = await Promise.all( - leagueIds[Sport.Rugby].map(async (leagueId) => { - const league = leaguesData[leagueId] - ? leaguesData[leagueId] - : await fetchLeagueDetails(Sport.Rugby, leagueId) - let allGames: Game[] = await fetchCurrentGames(Sport.Rugby, leagueId) + currentLeagues.map(async (league) => { + let allGames: Game[] = await fetchCurrentGames(Sport.Rugby, league.id) // todo: remove after implementing dummy games if (searchParams.mode === 'test') { const testGames = await fetchTestGames( Sport.Rugby, - leagueId, + league.id, currentSeason, ) allGames = [...testGames, ...allGames] @@ -40,7 +40,7 @@ export default async function Home({ .includes((searchParams.search as string).toLowerCase()), ) : allGames - return { league, games } + return { league: leaguesData[league.id] || league, games } }), ) @@ -54,9 +54,7 @@ export default async function Home({ ))} - ) : ( -
No games found
- )} + ) : null} ))} diff --git a/app/src/components/bet-card.tsx b/app/src/components/bet-card.tsx index 948316a..e36a04c 100644 --- a/app/src/components/bet-card.tsx +++ b/app/src/components/bet-card.tsx @@ -71,7 +71,7 @@ export default function BetCard({
{ + const leagueIds = getLeaguesIds() const games = ( await Promise.all( - leagueIds[Sport.Rugby].map(async (leagueId) => { + leagueIds.map(async (leagueId) => { const games = await fetchGames(Sport.Rugby, leagueId, currentSeason) return games }), diff --git a/app/src/components/game-card.tsx b/app/src/components/game-card.tsx index cef473d..a21d132 100644 --- a/app/src/components/game-card.tsx +++ b/app/src/components/game-card.tsx @@ -10,7 +10,7 @@ export default function GameCard({ game }: { game: Game }) {
{league.name
diff --git a/app/src/config/api.ts b/app/src/config/api.ts index 464604a..f72ac64 100644 --- a/app/src/config/api.ts +++ b/app/src/config/api.ts @@ -1,4 +1,4 @@ -import { Sport, League, Winner } from '@/types' +import { League, Winner } from '@/types' export const liveGameStatuses = ['1H', '2H', 'HT', 'ET', 'BT', 'PT'] @@ -6,15 +6,13 @@ export const currentGameStatuses = ['NS', ...liveGameStatuses] export const currentSeason = new Date().getFullYear() +export const leaguesCountLimit = 5 + export const winnerToResult = { [Winner.Home]: 1, [Winner.Away]: 2, } -export const leagueIds = { - [Sport.Rugby]: [69], -} - export const leaguesData: { [key: number]: League } = { diff --git a/app/src/lib/fetch-data.ts b/app/src/lib/fetch-data.ts index 037c00d..f2cf36c 100644 --- a/app/src/lib/fetch-data.ts +++ b/app/src/lib/fetch-data.ts @@ -2,8 +2,12 @@ import 'server-only' import { cache } from 'react' import { add, getUnixTime } from 'date-fns' -import { currentGameStatuses, currentSeason } from '@/config/api' -import { Sport, Game, League } from '@/types' +import { + currentGameStatuses, + currentSeason, + leaguesCountLimit, +} from '@/config/api' +import { Sport, Game, League, LeagueResponse } from '@/types' export const fetchCurrentGames = async (sport: Sport, leagueId: number) => { const games = await fetchGames(sport, leagueId, currentSeason) @@ -107,11 +111,25 @@ const transformScore = (scores: any, sport: Sport) => { } } +export const fetchCurrentLeagues = async (sport: Sport) => { + const res = await fetchSportData( + sport, + '/leagues', + new URLSearchParams(), + 3600 * 24, + ) + const allLeagues: LeagueResponse[] = res.response + const currentLeagues = allLeagues.filter((league: LeagueResponse) => + league.seasons.some((season) => season.current === true), + ) + return currentLeagues.reverse().slice(0, leaguesCountLimit) +} + export const fetchLeagueDetails = async (sport: Sport, leagueId: number) => { const params = new URLSearchParams({ id: leagueId.toString(), }) - const res = await fetchSportData(sport, '/leagues', params) + const res = await fetchSportData(sport, '/leagues', params, 3600 * 24) const league = res.response[0] if (sport === Sport.Soccer) { @@ -157,7 +175,12 @@ const baseUrls = { const apiKey = process.env.API_KEY || '' const fetchSportData = cache( - async (sport: Sport, path: string, params: URLSearchParams) => { + async ( + sport: Sport, + path: string, + params: URLSearchParams, + revalidate = 3600, + ) => { const response = await fetch( `${baseUrls[sport]}${path}?${params.toString()}`, { @@ -165,7 +188,7 @@ const fetchSportData = cache( 'x-apisports-key': apiKey, }, next: { - revalidate: 3600, + revalidate, }, }, ) diff --git a/app/src/lib/server-context.ts b/app/src/lib/server-context.ts new file mode 100644 index 0000000..34d893b --- /dev/null +++ b/app/src/lib/server-context.ts @@ -0,0 +1,17 @@ +import 'server-only' +// @ts-ignore +import { cache } from 'react' + +const serverContext = (defaultValue: T): [() => T, (v: T) => void] => { + const getRef = cache(() => ({ current: defaultValue })) + + const getValue = (): T => getRef().current + + const setValue = (value: T) => { + getRef().current = value + } + + return [getValue, setValue] +} + +export const [getLeaguesIds, setLeaguesIds] = serverContext([]) diff --git a/app/src/types.ts b/app/src/types.ts index 65356d7..274940f 100644 --- a/app/src/types.ts +++ b/app/src/types.ts @@ -14,6 +14,10 @@ export type League = { season?: string } +export type LeagueResponse = League & { + seasons: { current: boolean }[] +} + export type Game = { id: number date: Date