Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Created January 11, 2023 22:02
Show Gist options
  • Save abel-masila/65e26944771f48dda8ce139145a74d32 to your computer and use it in GitHub Desktop.
Save abel-masila/65e26944771f48dda8ce139145a74d32 to your computer and use it in GitHub Desktop.
const handleDBReponse = (response) => {
const appointments = response;
const today = moment().startOf("day"); //start of today 12 am
const initialSchedule = {};
initialSchedule[today.format("YYYY-DD-MM")] = true;
const schedule = !appointments.length
? initialSchedule
: appointments.reduce((currentSchedule, appointment) => {
const { slot_date, slot_time } = appointment;
const dateString = moment(slot_date, "YYYY-DD-MM").format("YYYY-DD-MM");
!currentSchedule[slot_date]
? (currentSchedule[dateString] = Array(8).fill(false))
: null;
Array.isArray(currentSchedule[dateString])
? (currentSchedule[dateString][slot_time] = true)
: null;
return currentSchedule;
}, initialSchedule);
for (let day in schedule) {
let slots = schedule[day];
slots.length
? slots.every((slot) => slot === true)
? (schedule[day] = true)
: null
: null;
}
setSchedule(schedule);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment