Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…n-survivor into danielle/550-remove-past-weeks-picks-link
  • Loading branch information
Danielle254 committed Oct 5, 2024
2 parents 9a401a1 + c7acabe commit 34fc69d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const updatedWeeklyPicks = {
},
};

xdescribe('League Week Picks', () => {
describe('League Week Picks', () => {
const setUserPick = jest.fn();
const updateWeeklyPicks = jest.fn();
const mockGetNFLTeamLogo = getNFLTeamLogo as jest.Mock;
Expand Down Expand Up @@ -354,7 +354,7 @@ xdescribe('League Week Picks', () => {
);
});

it('should redirect back to entry page after successfully selecting a team', async () => {
xit('should redirect back to entry page after successfully selecting a team', async () => {
mockUseAuthContext.isSignedIn = true;
(getCurrentUserEntries as jest.Mock).mockResolvedValue([
{
Expand Down
14 changes: 6 additions & 8 deletions app/(main)/league/[leagueId]/entry/[entryId]/week/Week.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import {
import { ILeague } from '@/api/apiFunctions.interface';
import WeekTeams from './WeekTeams';
import GlobalSpinner from '@/components/GlobalSpinner/GlobalSpinner';
import { onWeeklyPickChange } from './WeekHelper';
import Alert from '@/components/AlertNotification/AlertNotification';
import { AlertVariants } from '@/components/AlertNotification/Alerts.enum';
import { NFLTeams } from '@/api/apiFunctions.enum';
import { useAuthContext } from '@/context/AuthContextProvider';
import { cn, getNFLTeamLogo } from '@/utils/utils';
import Image from 'next/image';
import { useRouter } from 'next/navigation';
import LinkCustom from '@/components/LinkCustom/LinkCustom';
import { ChevronLeft } from 'lucide-react';
import toast from 'react-hot-toast';

/**
* Renders the weekly picks page.
Expand All @@ -52,6 +53,7 @@ const Week = ({ entry, league, NFLTeams, week }: IWeekProps): JSX.Element => {
const { user, updateCurrentWeek, updateWeeklyPicks, weeklyPicks } =
useDataStore((state) => state);
const { isSignedIn } = useAuthContext();
const router = useRouter();

/**
* Fetches the current game week.
Expand Down Expand Up @@ -215,13 +217,9 @@ const Week = ({ entry, league, NFLTeams, week }: IWeekProps): JSX.Element => {
};

try {
toast.custom(
<Alert
variant={AlertVariants.Error}
message={`Team selection has been locked for the week!`}
/>,
);
console.error(params);
await onWeeklyPickChange(params);
setUserPick(teamSelect);
router.push(`/league/${league}/entry/all`);
} catch (error) {
console.error('Submission error:', error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const TestWeekTeamsComponent = ({
);
};

describe('WeekTeams', () => {
xdescribe('WeekTeams', () => {
beforeEach(() => {
jest.clearAllMocks();
});
Expand Down
91 changes: 57 additions & 34 deletions app/(main)/league/[leagueId]/entry/[entryId]/week/WeekTeams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ const formatDateTime = (dateString: string): string => {
return `${formattedDate}${formattedTime}`;
};

/**
* Checks if the current game time is within 1 hour of the current time.
* @param gameTime The game time to check.
* @returns True if the current game time is within 1 hour of the current time, false otherwise.
*/
const checkCurrentGameTime = (gameTime: string): boolean => {
const timestamp = new Date(gameTime);
const currentTime = new Date();

const currentTimeMinus1Hour = new Date(
currentTime.getTime() - 60 * 60 * 1000,
);

return currentTimeMinus1Hour > timestamp;
};

/**
* Renders the weekly picks page.
* @param props The parameters for the weekly picks page.
Expand All @@ -59,41 +75,48 @@ const WeekTeams = ({
value={userPick}
onChange={field.onChange}
>
{schedule.map((scheduledGame) => (
<div
className="grid w-full grid-cols-[1fr_auto_1fr] gap-4 pb-8"
style={{ direction: 'rtl' }}
key={scheduledGame.id}
>
<div className="week-page-game-schedule col-span-3 text-center">
<p>{formatDateTime(scheduledGame.date)}</p>
{schedule.map((scheduledGame) => {
const disableGame = checkCurrentGameTime(scheduledGame.date);

return (
<div
className="grid w-full grid-cols-[1fr_auto_1fr] gap-4 pb-8"
style={{ direction: 'rtl' }}
key={scheduledGame.id}
>
<div className="week-page-game-schedule col-span-3 text-center">
<p>{formatDateTime(scheduledGame.date)}</p>
</div>
{scheduledGame.competitions[0].competitors.map(
(competition, index) => (
<>
{index > 0 && (
<div className="h-20 flex self-end items-center">
<span>@</span>
</div>
)}
<FormItem key={competition.id} className="text-center">
<FormControl>
<WeeklyPickButton
loadingTeamName={loadingTeamName}
selectedTeam={competition.team.shortDisplayName.toLowerCase()}
homeAway={competition.homeAway}
team={competition.team.name}
src={competition.team.logo}
isDisabled={
disableGame ||
Boolean(loadingTeamName) ||
hasTeamBeenPicked(competition.team.name, selectedTeams)
}
/>
</FormControl>
</FormItem>
</>
),
)}
</div>
{scheduledGame.competitions[0].competitors.map((competition, index) => (
<>
{index > 0 && (
<div className="h-20 flex self-end items-center">
<span>@</span>
</div>
)}
<FormItem key={competition.id} className="text-center">
<FormControl>
<WeeklyPickButton
loadingTeamName={loadingTeamName}
selectedTeam={competition.team.shortDisplayName.toLowerCase()}
homeAway={competition.homeAway}
team={competition.team.name}
src={competition.team.logo}
isDisabled={
Boolean(loadingTeamName) ||
hasTeamBeenPicked(competition.team.name, selectedTeams)
}
/>
</FormControl>
</FormItem>
</>
))}
</div>
))}
);
})}
</RadioGroup>
);
export default WeekTeams;
2 changes: 1 addition & 1 deletion app/(main)/league/[leagueId]/entry/all/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('League entries page (Entry Component)', () => {
expect(addNewEntryButton).toBeInTheDocument();
});

it('should not display a button to add a new entry if there are 5 entries', async () => {
xit('should not display a button to add a new entry if there are 5 entries', async () => {
mockUseDataStore.mockReturnValue({
...mockUseDataStore(),
currentWeek: 2,
Expand Down
37 changes: 2 additions & 35 deletions app/(main)/league/[leagueId]/entry/all/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@

'use client';
import {
createEntry,
getCurrentLeague,
getCurrentUserEntries,
getGameWeek,
getNFLTeams,
} from '@/api/apiFunctions';
import { Button } from '@/components/Button/Button';
import { ChevronLeft, PlusCircle } from 'lucide-react';
import { ChevronLeft } from 'lucide-react';
import { ENTRY_URL, LEAGUE_URL, WEEK_URL } from '@/const/global';
import { IEntry, IEntryProps } from '../Entries.interface';
import { IEntry } from '../Entries.interface';
import { LeagueEntries } from '@/components/LeagueEntries/LeagueEntries';
import { LeagueSurvivors } from '@/components/LeagueSurvivors/LeagueSurvivors';
import { useDataStore } from '@/store/dataStore';
import GlobalSpinner from '@/components/GlobalSpinner/GlobalSpinner';
import Heading from '@/components/Heading/Heading';
import Link from 'next/link';
import React, { JSX, useEffect, useState } from 'react';
import LoadingSpinner from '@/components/LoadingSpinner/LoadingSpinner';
import { cn } from '@/utils/utils';
import LinkCustom from '@/components/LinkCustom/LinkCustom';
import { getNFLTeamLogo } from '@/utils/utils';

Expand All @@ -38,12 +34,10 @@ const Entry = ({
const [entries, setEntries] = useState<IEntry[]>([]);
const [leagueName, setLeagueName] = useState<string>('');
const [loadingData, setLoadingData] = useState<boolean>(true);
const [addingEntry, setAddingEntry] = useState<boolean>(false);
const [survivors, setSurvivors] = useState<number>(0);
const [totalPlayers, setTotalPlayers] = useState<number>(0);
const { currentWeek, NFLTeams, user, updateCurrentWeek, updateNFLTeams } =
useDataStore((state) => state);
const MAX_ENTRIES = 5;

useEffect(() => {
/**
Expand Down Expand Up @@ -113,33 +107,6 @@ const Entry = ({
}
};

/**
* Adds a new entry to the league.
* @param {IEntryProps} props - The entry properties.
* @param {string} props.name - The name of the entry.
* @param {string} props.user - The user id.
* @param {string} props.league - The league id.
* @returns {void}
*/
const addNewEntry = async ({
name,
user,
league,
}: IEntryProps): Promise<void> => {
if (entries.length >= MAX_ENTRIES) {
return;
}
setAddingEntry(true);
try {
const createdEntry = await createEntry({ name, user, league });
setEntries((prevEntries) => [...prevEntries, createdEntry]);
} catch (error) {
throw new Error('Error adding new entry');
} finally {
setAddingEntry(false);
}
};

useEffect(() => {
if (!user.id || user.id === '') {
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"version": "0.4.3",
"version": "0.4.4",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand Down

0 comments on commit 34fc69d

Please sign in to comment.