diff --git a/client/src/hooks/useSchedule.ts b/client/src/hooks/useSchedule.ts index 52a0d1c1..1f4c8cde 100644 --- a/client/src/hooks/useSchedule.ts +++ b/client/src/hooks/useSchedule.ts @@ -6,7 +6,7 @@ import UserDataContext from '../contexts/UserDataContext'; import AlternatesContext, {Alternates} from '../contexts/AlternatesContext'; // Utils -import {getSchedule} from '@watt/shared/util/schedule'; +import {getUserSchedule} from '@watt/shared/util/schedule'; import {SCHOOL_END_EXCLUSIVE, PeriodObj} from '@watt/shared/data/schedule'; @@ -24,14 +24,9 @@ export function useSchedule(date: DateTime) { const altFormat = localizedDate.toFormat('MM-dd'); useEffect(() => { - const {periods, alternate} = getSchedule(date, alternates); - - setPeriods(periods && periods.filter(({n, grades}) => { - if (n === '0' && !userData.options.period0) return false; - if (n === '8' && !userData.options.period8) return false; - if (grades && userData.gradYear) return grades.includes(12 - (userData.gradYear - SCHOOL_END_EXCLUSIVE.year)); - return true; - })); + const {periods, alternate} = getUserSchedule(userData, date, alternates); + + setPeriods(periods || null); setAlternate(alternate); return function cleanup() { diff --git a/client/src/util/sgyPeriodFunctions.ts b/client/src/util/sgyPeriodFunctions.ts index c1670a01..ecbfa256 100644 --- a/client/src/util/sgyPeriodFunctions.ts +++ b/client/src/util/sgyPeriodFunctions.ts @@ -1,7 +1,7 @@ import {DateTime} from 'luxon'; import { UserData } from '../contexts/UserDataContext'; import {Alternates} from '../contexts/AlternatesContext'; -import { getSchedule } from '@watt/shared/util/schedule'; +import { getSchedule, getUserSchedule } from '@watt/shared/util/schedule'; import { SCHOOL_END_EXCLUSIVE, SCHOOL_START } from '@watt/shared/data/schedule'; @@ -86,27 +86,22 @@ export function nextSchoolDay(userData: UserData, alternates: Alternates['altern let now = DateTime.now(); while (true) { + now = now.plus({day: 1}); // increment day + if (now > SCHOOL_END_EXCLUSIVE) break; - const sched = getSchedule(now, alternates).periods; - if (sched) { - const periods = sched.filter(({n}) => { - if (n === '0' && !userData.options.period0) return false; - if (n === '8' && !userData.options.period8) return false; - return true; - }); + const {periods} = getUserSchedule(userData, now, alternates); + if (periods) { const end = periods[periods.length - 1].e; // console.log(now.clone().startOf('day').add(end, 'minutes').format('MMMM Do hh:mm:ss')); // console.log(moment().isAfter(now.clone().startOf('day').add(end, 'minutes'))) if (DateTime.now() <= now.startOf('day').plus({minute: end})) break; } - - now = now.plus({day: 1}); // increment day } if (now > SCHOOL_END_EXCLUSIVE) return null; - const p = getSchedule(now, alternates).periods![0]; + const p = getUserSchedule(userData, now, alternates).periods![0]; if (p) { const t = p.s; const m = t % 60; diff --git a/shared/util/schedule.ts b/shared/util/schedule.ts index 0fafd9e2..76d4dca0 100644 --- a/shared/util/schedule.ts +++ b/shared/util/schedule.ts @@ -1,5 +1,6 @@ import {DateTime} from 'luxon'; import {Alternates} from '@watt/client/src/contexts/AlternatesContext'; +import {UserData} from '@watt/client/src/contexts/UserDataContext'; import schedule, {SCHOOL_START, SCHOOL_END, SCHOOL_END_EXCLUSIVE, PeriodObj} from '../data/schedule'; @@ -33,6 +34,21 @@ export function getSchedule(date: DateTime, alternates: Alternates['alternates'] return {periods, alternate}; } +// Filters `periods` from `getSchedule()` to apply to the given `UserData` +export function getUserSchedule(userData: UserData, date: DateTime, alternates: Alternates['alternates']) { + const schedule = getSchedule(date, alternates); + const {alternate} = schedule + + const periods = schedule.periods?.filter(({n, grades}) => { + if (n === '0' && !userData.options.period0) return false; + if (n === '8' && !userData.options.period8) return false; + if (grades && userData.gradYear) return grades.includes(12 - (userData.gradYear - SCHOOL_END_EXCLUSIVE.year)); + return true; + }); + + return {periods, alternate}; +} + // Returns the next and previous period and information relevant for displaying them. Returns the next period or null // if there is none, the previous period or null if there is none, the minutes the next period starts in (or 0 if none), // the minutes the next period ends in (or 0 if none), the seconds left in the current minute, and the current minutes