From 18176196cf8cb2767390f6ed9cf821f2baf8f4ce Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 21 Sep 2023 23:10:44 -0400 Subject: [PATCH] Add shop resources based on events that don't match the tool pattern --- src/common.ts | 59 ++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/common.ts b/src/common.ts index a148409..64792f5 100644 --- a/src/common.ts +++ b/src/common.ts @@ -54,15 +54,7 @@ const shops: { [key: string]: Shop } = { }, }; -function extract_calendars(shops: { [key: string]: Shop }): string[] { - return Object.values(shops) - .map((shop) => shop.calendar) - .filter((calendar) => calendar !== undefined) as string[]; -} - -const calendars: string[] = extract_calendars(shops); - -const colors: string[] = unique_colors(calendars.length); +const colors: string[] = unique_colors(Object.keys(shops).length); export const common_calendarOptions: CalendarOptions = { schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives', @@ -113,30 +105,39 @@ export function main( ) { const calendarEl = document.getElementById('calendar'); const calendar = new Calendar(calendarEl!, calendarOptions); - calendars.forEach((id, idx) => - calendar.addEventSource({ - url: '/calendar/ical/' + id + '/public/basic.ics', - format: 'ics', - color: colors[idx], - eventDataTransform: (eventData) => { - // clear the url to prevent clicking on the event - delete eventData.url; + Object.entries(shops) + .filter(([shop_name, shop]) => shop.calendar !== undefined) + .forEach(([shop_name, shop], idx) => { + calendar.addEventSource({ + url: '/calendar/ical/' + shop.calendar + '/public/basic.ics', + format: 'ics', + color: colors[idx], + eventDataTransform: (eventData) => { + // clear the url to prevent clicking on the event + delete eventData.url; - const match = eventData?.title?.match(/([^\/]*) \| ([^-]*) - (.*)/); - if (match) { - const [, member, shop, tool] = match; - eventData.title = `${member}`; - eventData.resourceId = tool; + const match = eventData?.title?.match(/([^\/]*) \| ([^-]*) - (.*)/); + if (match) { + const [, member, _event_shop, tool] = match; + eventData.title = `${member}`; + eventData.resourceId = tool; + } else { + // assume any event not for a specific tool is for the shop as a whole + eventData.resourceId = shop_name; + } + console.log(eventData.resourceId); if (allTools) { - if (!calendar.getResourceById(tool)) { - calendar.addResource({ id: tool, title: tool }, false); + if (!calendar.getResourceById(eventData.resourceId)) { + calendar.addResource( + { id: eventData.resourceId, title: eventData.resourceId }, + false + ); } } - } - return eventData; - }, - }) - ); + return eventData; + }, + }); + }); calendar.render(); //calendar.gotoDate('2022-06-27');