Skip to content

Commit

Permalink
feat: add in next weeks games to upcoming if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvanc committed Jan 25, 2025
1 parent 9eed592 commit 0c7f672
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
8 changes: 4 additions & 4 deletions app/mocks/upcomingGames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const upcomingGames: Array<HttpHandler> = [
leagueId: "00",
gameDates: [
{
gameDate: "01/25/2025 17:00:00",
gameDate: "01/27/2025 00:00:00",
games: [
{
gameId: "0012400001",
Expand Down Expand Up @@ -388,7 +388,7 @@ export const upcomingGames: Array<HttpHandler> = [
],
},
{
gameDate: "01/26/2025 17:00:00",
gameDate: "01/26/2025 00:00:00",
games: [
{
gameId: "0012400001",
Expand Down Expand Up @@ -760,7 +760,7 @@ export const upcomingGames: Array<HttpHandler> = [
],
},
{
gameDate: "01/25/2025 17:00:00",
gameDate: "02/01/2025 00:00:00",
games: [
{
gameId: "0012400001",
Expand Down Expand Up @@ -1132,7 +1132,7 @@ export const upcomingGames: Array<HttpHandler> = [
],
},
{
gameDate: "01/19/2025 17:00:00",
gameDate: "01/21/2025 00:00:00",
games: [
{
gameId: "0012400001",
Expand Down
44 changes: 26 additions & 18 deletions app/utils/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,46 @@ export async function getUpcomingGames() {
weeks: GameWeek[];
};

const currentDate = new Date().toISOString();
const currentDate = new Date().toISOString().split("T")[0];

const isDateWithinRange = (compare: string, start: string, end: string) =>
compare >= start && compare <= end;
compare >= start.split("T")[0] && compare <= end.split("T")[0];

const currentWeek = weeks.filter((week) =>
isDateWithinRange(currentDate, week.startDate, week.endDate)
)[0];
// const nextWeek = weeks.filter(
// (week) => week.weekNumber === currentWeek.weekNumber + 1
// )[0];

// let skipTodaysGames = false;

const upcomingSchedule = gameDates
.filter((gameDay) => {
const getGameLineups = (startDate: string, endDate: string) => {
console.log("startDate", startDate);
console.log("endDate", endDate);
return gameDates.filter((gameDay) => {
const gameDate = gameDay.gameDate.split(" ")[0];
const [month, day, year] = gameDate.split("/");
const gamesDate = `${year}-${month}-${day}`;
const formedDate = currentDate.split("T")[0];

return (
isDateWithinRange(
gamesDate,
currentWeek.startDate,
currentWeek.endDate
) && gamesDate > formedDate
isDateWithinRange(gamesDate, startDate, endDate) &&
gamesDate > formedDate
);
})
});
};

let scheduleDates = getGameLineups(
currentWeek.startDate,
currentWeek.endDate
);

if (!scheduleDates.length || scheduleDates[0].games.length < 5) {
const nextWeek = weeks.filter(
(week) => week.weekNumber === currentWeek.weekNumber + 1
)[0];
const newDates = getGameLineups(nextWeek.startDate, nextWeek.endDate);

scheduleDates = [...scheduleDates, ...newDates.slice(0, 2)];
}

const upcomingSchedule = scheduleDates
.map((gameDay) =>
gameDay.games.map(
({
Expand All @@ -166,9 +177,6 @@ export async function getUpcomingGames() {
)
.flat();

// TODO: See below
// 7. If there's either, no more games for the week or only 5 more games, return the next week's games. If there's no more games next week, return an empty array

return upcomingSchedule;
} catch {
console.error("Unable to retrieve upcoming games");
Expand Down

0 comments on commit 0c7f672

Please sign in to comment.