Add shop resources based on events that don't match the tool pattern

This commit is contained in:
Adam Goldsmith 2023-09-21 23:10:44 -04:00
parent 77ef62503f
commit 48a55519f1

View File

@ -54,15 +54,7 @@ const shops: { [key: string]: Shop } = {
}, },
}; };
function extract_calendars(shops: { [key: string]: Shop }): string[] { const colors: string[] = unique_colors(Object.keys(shops).length);
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);
export const common_calendarOptions: CalendarOptions = { export const common_calendarOptions: CalendarOptions = {
schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives', schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives',
@ -113,30 +105,38 @@ export function main(
) { ) {
const calendarEl = document.getElementById('calendar'); const calendarEl = document.getElementById('calendar');
const calendar = new Calendar(calendarEl!, calendarOptions); const calendar = new Calendar(calendarEl!, calendarOptions);
calendars.forEach((id, idx) => Object.entries(shops)
calendar.addEventSource({ .filter(([shop_name, shop]) => shop.calendar !== undefined)
url: '/calendar/ical/' + id + '/public/basic.ics', .forEach(([shop_name, shop], idx) => {
format: 'ics', calendar.addEventSource({
color: colors[idx], url: '/calendar/ical/' + shop.calendar + '/public/basic.ics',
eventDataTransform: (eventData) => { format: 'ics',
// clear the url to prevent clicking on the event color: colors[idx],
delete eventData.url; eventDataTransform: (eventData) => {
// clear the url to prevent clicking on the event
delete eventData.url;
const match = eventData?.title?.match(/([^\/]*) \| ([^-]*) - (.*)/); const match = eventData?.title?.match(/([^\/]*) \| ([^-]*) - (.*)/);
if (match) { if (match) {
const [, member, shop, tool] = match; const [, member, _event_shop, tool] = match;
eventData.title = `${member}`; eventData.title = `${member}`;
eventData.resourceId = tool; eventData.resourceId = tool;
} else {
// assume any event not for a specific tool is for the shop as a whole
eventData.resourceId = shop_name;
}
if (allTools) { if (allTools) {
if (!calendar.getResourceById(tool)) { if (!calendar.getResourceById(eventData.resourceId)) {
calendar.addResource({ id: tool, title: tool }, false); calendar.addResource(
{ id: eventData.resourceId, title: eventData.resourceId },
false
);
} }
} }
} return eventData;
return eventData; },
}, });
}) });
);
calendar.render(); calendar.render();
//calendar.gotoDate('2022-06-27'); //calendar.gotoDate('2022-06-27');