Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applicant time slots are now merged together #150

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions components/committee/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,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 @@ -91,6 +102,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 @@ -101,8 +141,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 @@ -146,8 +188,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