fix schedule notes

This commit is contained in:
Dobromir Popov
2024-03-02 19:35:08 +02:00
parent c984490980
commit b02ac64792
2 changed files with 42 additions and 8 deletions

View File

@ -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,
};
}

View File

@ -150,7 +150,7 @@
<thead class="week{{week}}">
<tr>
<th colspan="3"
class="">{{dayOfWeek}} {{dayOfMonth}}<br />
class="">{{dayOfWeek}} {{dayOfMonth}} {{monthName}}<br />
{{placeOfEvent}}
</th>
</tr>
@ -160,7 +160,7 @@
<tr>
<td class="col1 block sm:table-cell">{{time}}</td>
<td class="col2 block sm:table-cell">{{names}}</td>
<td class="col3 hidden sm:table-cell"><strong>{{notes_bold}}</strong></td>
<td class="col3 hidden sm:table-cell">{{notes}}<strong>{{notes_bold}}</strong></td>
</tr>
{{/each}}
</tbody>