Skip to content

Commit

Permalink
Fix: added lockout to onWeeklyChange function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexappleget committed Sep 30, 2024
1 parent c25250a commit cf4af9e
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions app/(main)/league/[leagueId]/entry/[entryId]/week/Week.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import Image from 'next/image';
import LinkCustom from '@/components/LinkCustom/LinkCustom';
import { ChevronLeft } from 'lucide-react';
import toast from 'react-hot-toast';
import { onWeeklyPickChange } from './WeekHelper';
import { useRouter } from 'next/navigation';

/**
* Renders the weekly picks page.
Expand All @@ -52,6 +54,36 @@ const Week = ({ entry, league, NFLTeams, week }: IWeekProps): JSX.Element => {
const { user, updateCurrentWeek, updateWeeklyPicks, weeklyPicks } =
useDataStore((state) => state);
const { isSignedIn } = useAuthContext();
const [lockedOut, setLockedOut] = useState<boolean>(false);
const router = useRouter();

useEffect(() => {
/**
* Checks if the user is locked out from making a pick.
*/
const checkLockout = (): void => {
const currentDateAndTime = new Date();
const day = currentDateAndTime.getUTCDay();
const hours = currentDateAndTime.getUTCHours();
if (
(day === 5 && hours >= 0) || // Friday at 12am UTC (Thurs 8pm CT)
day > 5 || // Friday and Saturday
day === 0 || // Sunday
day === 1 || // Monday
(day === 2 && hours < 12) // Tuesday at 12pm UTC (8am CT)
) {
setLockedOut(true);
} else {
setLockedOut(false);
}
};

checkLockout();

const intervalId = setInterval(checkLockout, 60 * 60 * 1000);

return (): void => clearInterval(intervalId);
}, []);

/**
* Fetches the current game week.
Expand Down Expand Up @@ -195,6 +227,7 @@ const Week = ({ entry, league, NFLTeams, week }: IWeekProps): JSX.Element => {

/**
* Handles the weekly pick team change
* @param lockedOut - the lockout logic.
* @param teamSelect - the selected team name.
* @returns {void}
*/
Expand All @@ -214,16 +247,26 @@ const Week = ({ entry, league, NFLTeams, week }: IWeekProps): JSX.Element => {
week,
};

try {
toast.custom(
<Alert
variant={AlertVariants.Error}
message={`Team selection has been locked for the week!`}
/>,
);
console.error(params);
} catch (error) {
console.error('Submission error:', error);
if (lockedOut) {
try {
toast.custom(
<Alert
variant={AlertVariants.Error}
message={`Team selection has been locked for the week!`}
/>,
);
console.error(params);
} catch (error) {
console.error('Submission error:', error);
}
} else {
try {
await onWeeklyPickChange(params);
setUserPick(teamSelect);
router.push(`/league/${league}/entry/all`);
} catch (error) {
console.error('Submission error:', error);
}
}
};

Expand Down

0 comments on commit cf4af9e

Please sign in to comment.