From b02ac6479214ddf536455873351b1ab7fa882e77 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 2 Mar 2024 19:35:08 +0200 Subject: [PATCH] fix schedule notes --- pages/api/schedule.ts | 46 +++++++++++++++++++++++++++++++++----- src/templates/schedule.hbs | 4 ++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/pages/api/schedule.ts b/pages/api/schedule.ts index 2e78c30..d94d567 100644 --- a/pages/api/schedule.ts +++ b/pages/api/schedule.ts @@ -27,6 +27,36 @@ const generateTemplateFile = async (data, templateSrc) => { return html; } +function splitNotes(notes) { + if (!notes) { + return { notes: '', notes_bold: '' }; + } + + // Combine both dash types into a single pattern for the regex + const dashPattern = /[-–]/g; + + // Find all occurrences of either dash + let allDashes = [...notes.matchAll(dashPattern)]; + + if (allDashes.length === 0) { + // No dash found, return the original notes in both parts + return { notes: notes, notes_bold: '' }; + } + + // Get the index of the first and last dash + const firstDashIndex = allDashes[0].index; + const lastDashIndex = allDashes[allDashes.length - 1].index; + + // Extract parts before the first dash and after the last dash + const notesBeforeFirstDash = notes.substring(0, firstDashIndex + 1); + const notesAfterLastDash = notes.substring(lastDashIndex + 1); + + return { + notes: notesBeforeFirstDash, + notes_bold: notesAfterLastDash + }; +} + export default async function handler(req: NextApiRequest, res: NextApiResponse) { console.log(req.url); console.log(req.query); @@ -53,7 +83,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) fromDate.setDate(fromDate.getDate() - 1); fromDate.setHours(0, 0, 0, 0); let toDate = new Date(fromDate); - toDate.setDate(toDate.getDate() + 30); + toDate.setDate(toDate.getDate() + 40); try { @@ -83,7 +113,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) let json = JSON.stringify(shifts); const groupedShifts = {}; const startDate = new Date(shifts[0].startTime); - const monthName = common.getMonthName(shifts[0].startTime.getMonth()); let i = 0; try { for (const shift of shifts) { @@ -97,13 +126,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) if (!groupedShifts[day][time]) { groupedShifts[day][time] = []; } + + let { notes, notes_bold } = splitNotes(shift.notes);//.substring(Math.max(shift.notes.lastIndexOf("-"), shift.notes.lastIndexOf("–")) + 1).trim() || ""; + let shiftSchedule = { date: date, placeOfEvent: shift.cartEvent.location.name, time: time, //bold the text after - in the notes - notes: shift.notes?.substring(0, shift.notes.indexOf("-") + 1), - notes_bold: shift.notes?.substring(shift.notes.indexOf("-") + 1), + notes: notes, + notes_bold: notes_bold, names: shift.assignments .map((assignment) => { return ( @@ -123,7 +155,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Create the output object in the format of the second JSON file const monthlySchedule = { - month: monthName, + month: common.getMonthName(shifts[0].startTime.getMonth()), year: startDate.getFullYear(), events: [], }; @@ -139,6 +171,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) continue; } let weekday = common.getDayOfWeekName(shift.date); + let monthName = common.getMonthName(shift.date.getMonth()); weekday = weekday.charAt(0).toUpperCase() + weekday.slice(1); let weekNr = common.getWeekNumber(shift.date); console.log("weekday = " + weekday, " weekNr = " + weekNr); @@ -148,7 +181,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) dayOfMonth: shift.date.getDate(), placeOfEvent: shift.placeOfEvent, shifts: [], - //transport: shift.notes, + transport: shift.notes, + monthName: monthName, }; } diff --git a/src/templates/schedule.hbs b/src/templates/schedule.hbs index f85badd..5f95f05 100644 --- a/src/templates/schedule.hbs +++ b/src/templates/schedule.hbs @@ -150,7 +150,7 @@ {{dayOfWeek}} {{dayOfMonth}}
+ class="">{{dayOfWeek}} {{dayOfMonth}} {{monthName}}
{{placeOfEvent}} @@ -160,7 +160,7 @@ {{time}} {{names}} - {{notes_bold}} + {{notes}}{{notes_bold}} {{/each}}