Skip to content

Commit

Permalink
fix: timetable default index
Browse files Browse the repository at this point in the history
  • Loading branch information
antoKeinanen committed Jul 18, 2024
1 parent f8c1de5 commit 0d3a406
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
20 changes: 20 additions & 0 deletions api/eventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ const determineEvent = (): string => {
}
};

export const determineStartDayIndex = (events: AssemblyEvent[]): number => {
const doorsOpen = events.find((event) => event.title.toLowerCase() === 'doors open');

if (!doorsOpen) {
console.error("Could not find event with name 'doors open'");
return 0;
}

const days = [...new Set(events.map((event) => event.start))];
const start_day_index = days.findIndex((day) => doorsOpen.start === day);

if (start_day_index === -1) {
console.error("Failed to find matching day for 'Doors Open' event");
return 0;
}

// the index returned by findIndex if off by one
return start_day_index - 1;
};

const API_BASE_PATH = `https://wp.assembly.org/${determineEvent()}/index.php?rest_route=/api/v1`;

/**
Expand Down
19 changes: 13 additions & 6 deletions elements/timetable/Timetable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssemblyEvent, getEvents } from '@/api/eventService';
import { AssemblyEvent, determineStartDayIndex, getEvents } from '@/api/eventService';
import DateSelector from '@/components/timetable/DateSelector';
import EventsBox from '@/elements/timetable/EventsBox';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -42,11 +42,18 @@ const Timetable = () => {

setEvents(eventsGroupedByDay);

// Set index so that the current day is shown
const index = eventsGroupedByDay.findIndex(
(events) => events[events.length - 1].end.getTime() > new Date().getTime()
); // If last event of the day has ended, it's not the current day
setEventDayIndex(index === -1 ? eventsGroupedByDay.length - 1 : index);
const startDayIndex = determineStartDayIndex(eventRes);
// If the event has not started yet
if (new Date().getTime() < eventsGroupedByDay[startDayIndex][0].start.getTime()) {
// Set the index to be the "official" event start index
setEventDayIndex(startDayIndex);
} else {
// Set index so that the current day is shown
const index = eventsGroupedByDay.findIndex(
(events) => events[events.length - 1].end.getTime() > new Date().getTime()
); // If last event of the day has ended, it's not the current day
setEventDayIndex(index === -1 ? eventsGroupedByDay.length - 1 : index);
}
});
}, []);

Expand Down

0 comments on commit 0d3a406

Please sign in to comment.