Skip to content

Commit

Permalink
group together time slots
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrir committed Jul 21, 2024
1 parent 0424ed0 commit 524a191
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions components/committee/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,20 @@ export default function Schedule({
return dates;
};

const convertToIso = (date: string, timeSlot: string): IsoTimeSlot => {
const [startTimeStr, endTimeStr] = timeSlot.split(" - ");
const [year, month, day] = date.split("-").map(Number);
const convertToIso = (slots: {
date: string;
times: string[];
}): IsoTimeSlot => {
const [startSlot, endSlot] = [
slots.times[0],
slots.times[slots.times.length - 1],
];
const [startDateStr, endDateStr] = [slots.date, slots.date];

const [startTimeStr] = startSlot.split(" - ");
const [, endTimeStr] = endSlot.split(" - ");

const [year, month, day] = slots.date.split("-").map(Number);

const [startHour, startMinute] = parseTime(startTimeStr);
const startTime = new Date(
Expand Down Expand Up @@ -89,6 +100,35 @@ export default function Schedule({
return [hour, minute];
};

const groupConsecutiveSlots = (
slots: { date: string; time: string }[]
): { date: string; times: string[] }[] => {
const groupedSlots: { date: string; times: string[] }[] = [];
let currentGroup: { date: string; times: string[] } | null = null;

slots.forEach((slot) => {
if (
currentGroup &&
slot.date === currentGroup.date &&
slot.time.split(" - ")[0] ===
currentGroup.times[currentGroup.times.length - 1].split(" - ")[1]
) {
currentGroup.times.push(slot.time);
} else {
if (currentGroup) {
groupedSlots.push(currentGroup);
}
currentGroup = { date: slot.date, times: [slot.time] };
}
});

if (currentGroup) {
groupedSlots.push(currentGroup);
}

return groupedSlots;
};

useEffect(() => {
const dates = getDatesWithinPeriod(periodTime);
const allAvailableTimes: { date: string; time: string }[] = [];
Expand All @@ -99,8 +139,10 @@ export default function Schedule({
});
});

const isoTimeSlotsForExport = allAvailableTimes.map((slot) =>
convertToIso(slot.date, slot.time)
const groupedTimeSlots = groupConsecutiveSlots(allAvailableTimes);

const isoTimeSlotsForExport = groupedTimeSlots.map((slot) =>
convertToIso(slot)
);

setApplicationData({
Expand Down Expand Up @@ -144,8 +186,10 @@ export default function Schedule({
});
});

const isoTimeSlotsForExport = dataToSend.map((slot) =>
convertToIso(slot.date, slot.time)
const groupedTimeSlots = groupConsecutiveSlots(dataToSend);

const isoTimeSlotsForExport = groupedTimeSlots.map((slot) =>
convertToIso(slot)
);

setApplicationData({
Expand Down

0 comments on commit 524a191

Please sign in to comment.