Skip to content

Commit

Permalink
fix next school day calculation + getUserSchedule
Browse files Browse the repository at this point in the history
  • Loading branch information
33tm committed Nov 16, 2024
1 parent 2ae74ef commit 329890b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
13 changes: 4 additions & 9 deletions client/src/hooks/useSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -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() {
Expand Down
17 changes: 6 additions & 11 deletions client/src/util/sgyPeriodFunctions.ts
Original file line number Diff line number Diff line change
@@ -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';


Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions shared/util/schedule.ts
Original file line number Diff line number Diff line change
@@ -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';


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 329890b

Please sign in to comment.