Skip to content

Commit

Permalink
feat: #8 expand shifts timeblocks to render each hour
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianrbp committed Aug 11, 2024
1 parent 1a22746 commit 1c8f472
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions frontend/src/mixins/useShiftManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1c8f472

Please sign in to comment.