Skip to content

Commit

Permalink
Auto-select schedule day if it matches current date
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Dec 14, 2023
1 parent c458181 commit 064957c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion blocks/schedule/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ function getActiveDayFromHash() {
return undefined;
}

/**
* Checks if the current date matches with one of the schedule dates and returns
* that date as active day.
* @returns Active day or undefined.
*/
async function getActiveDateFromCurrentDate() {
// load schedule data
const siteRoot = getSiteRootPath(document.location.pathname);
const scheduleData = await getScheduleData(`${siteRoot}schedule-data.json`, false);

const currentDate = new Date().toDateString();
return scheduleData.getDays().filter((day) => day.start.toDateString() === currentDate)[0]?.day;
}

/**
* Displays schedule for given day.
* @param {Element} block
Expand Down Expand Up @@ -200,7 +214,7 @@ export default async function decorate(block) {
// detect active day
let activeDay = getActiveDayFromHash();
if (!activeDay) {
activeDay = 1;
activeDay = await getActiveDateFromCurrentDate() ?? 1;
window.history.replaceState(null, null, `#day-${activeDay}`);
}

Expand Down

0 comments on commit 064957c

Please sign in to comment.