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; 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) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log(req.url); console.log(req.url);
console.log(req.query); console.log(req.query);
@ -53,7 +83,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
fromDate.setDate(fromDate.getDate() - 1); fromDate.setDate(fromDate.getDate() - 1);
fromDate.setHours(0, 0, 0, 0); fromDate.setHours(0, 0, 0, 0);
let toDate = new Date(fromDate); let toDate = new Date(fromDate);
toDate.setDate(toDate.getDate() + 30); toDate.setDate(toDate.getDate() + 40);
try { try {
@ -83,7 +113,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
let json = JSON.stringify(shifts); let json = JSON.stringify(shifts);
const groupedShifts = {}; const groupedShifts = {};
const startDate = new Date(shifts[0].startTime); const startDate = new Date(shifts[0].startTime);
const monthName = common.getMonthName(shifts[0].startTime.getMonth());
let i = 0; let i = 0;
try { try {
for (const shift of shifts) { for (const shift of shifts) {
@ -97,13 +126,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (!groupedShifts[day][time]) { if (!groupedShifts[day][time]) {
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 = { let shiftSchedule = {
date: date, date: date,
placeOfEvent: shift.cartEvent.location.name, placeOfEvent: shift.cartEvent.location.name,
time: time, time: time,
//bold the text after - in the notes //bold the text after - in the notes
notes: shift.notes?.substring(0, shift.notes.indexOf("-") + 1), notes: notes,
notes_bold: shift.notes?.substring(shift.notes.indexOf("-") + 1), notes_bold: notes_bold,
names: shift.assignments names: shift.assignments
.map((assignment) => { .map((assignment) => {
return ( 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 // Create the output object in the format of the second JSON file
const monthlySchedule = { const monthlySchedule = {
month: monthName, month: common.getMonthName(shifts[0].startTime.getMonth()),
year: startDate.getFullYear(), year: startDate.getFullYear(),
events: [], events: [],
}; };
@ -139,6 +171,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
continue; continue;
} }
let weekday = common.getDayOfWeekName(shift.date); let weekday = common.getDayOfWeekName(shift.date);
let monthName = common.getMonthName(shift.date.getMonth());
weekday = weekday.charAt(0).toUpperCase() + weekday.slice(1); weekday = weekday.charAt(0).toUpperCase() + weekday.slice(1);
let weekNr = common.getWeekNumber(shift.date); let weekNr = common.getWeekNumber(shift.date);
console.log("weekday = " + weekday, " weekNr = " + weekNr); console.log("weekday = " + weekday, " weekNr = " + weekNr);
@ -148,7 +181,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
dayOfMonth: shift.date.getDate(), dayOfMonth: shift.date.getDate(),
placeOfEvent: shift.placeOfEvent, placeOfEvent: shift.placeOfEvent,
shifts: [], shifts: [],
//transport: shift.notes, transport: shift.notes,
monthName: monthName,
}; };
} }

View File

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