From 1c8f4720fd89f793897da776279080b54ed19b2f Mon Sep 17 00:00:00 2001 From: adrian peralta Date: Sun, 11 Aug 2024 18:07:04 +0000 Subject: [PATCH] feat: #8 expand shifts timeblocks to render each hour --- frontend/src/mixins/useShiftManagement.ts | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/frontend/src/mixins/useShiftManagement.ts b/frontend/src/mixins/useShiftManagement.ts index bd7f613..951e90d 100644 --- a/frontend/src/mixins/useShiftManagement.ts +++ b/frontend/src/mixins/useShiftManagement.ts @@ -62,6 +62,7 @@ export function useShiftManagement() { selectedWeek.value ); shifts.value = data; + expandAllShiftsTimeBlocks(); errorMessage.value = null; // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { @@ -126,6 +127,41 @@ export function useShiftManagement() { return engineer ? engineer.color : ""; }; + // Utility function to generate time blocks + function generateTimeBlocks(start: string, end: string) { + const startTime = new Date(`1970-01-01T${start}:00`); + const endTime = new Date(`1970-01-01T${end}:00`); + const blocks = []; + + while (startTime < endTime) { + const nextHour = new Date(startTime.getTime() + 60 * 60 * 1000); + blocks.push({ + start_time: startTime.toTimeString().substring(0, 5), + end_time: nextHour.toTimeString().substring(0, 5), + }); + startTime.setHours(startTime.getHours() + 1); + } + + return blocks; + } + + const expandAllShiftsTimeBlocks = () => { + if (!shifts.value || !Array.isArray(shifts.value)) { + return []; + } + return shifts.value.map((shift) => ({ + ...shift, + time_blocks: shift.time_blocks.flatMap((timeBlock) => + generateTimeBlocks(timeBlock.start_time, timeBlock.end_time).map( + (block) => ({ + ...block, + engineer: timeBlock.engineer, + }) + ) + ), + })); + }; + return { services, selectedService,