Skip to content

Instantly share code, notes, and snippets.

@iampato
Created June 2, 2020 16:16
Show Gist options
  • Select an option

  • Save iampato/b5832979aef3462588da93ea6ac88ab9 to your computer and use it in GitHub Desktop.

Select an option

Save iampato/b5832979aef3462588da93ea6ac88ab9 to your computer and use it in GitHub Desktop.
<script>
// window.calculateAvailableDates takes in an array of options as a
// parameter then traverse the list for each it will run the calculateAvailableDatesInternal
// that returns a list of available dates
window.calculateAvailableDates = function(optionsArray) {
if (optionsArray.length == null) {
console.error("We cannot traverse an empty list");
}
optionsArray.array.forEach(oneOption => {
window.calculateAvailableDatesInternal(oneOption);
});
}
window.calculateAvailableDatesInternal = function(options) {
console.log('calculateAvailableDates, options', options);
try {
// don't assume exact milliseconds etc.
// Diagnosing issue on Safari on iPhone and iPad2
// Safari does not like: new Date('2021-01-01 09:00:00')
// Safari does like: new Date(1609455600000)
var avail_size = options.avail_size_mins * 60 * 1000;
var slot_size = options.slot_size_mins * 60 * 1000;
var bookingsCount = options.booking_start_times_ms.length;
var bbIndex = 0;
var loopsCount = 0;
var loopsMax = 5000;
var avail_dates = [];
var iiDate = options.range_start_time_ms;
var count = 0;
var maxRepeatDay = 0;
var nextAvailableDates = [];
while (iiDate < options.range_end_time_ms) {
console.log("Visiting first loop: " + (count++));
var isAvail = true;
var nextDate = iiDate + avail_size;
if (nextDate > options.range_end_time_ms + 60 * 1000) {
isAvail = false;
break;
}
while (bbIndex < bookingsCount) {
console.log("Visiting second loop: " + bbIndex);
if (options.booking_end_times_ms[bbIndex] < iiDate + 60 * 1000) {
console.log("First if endtimes: " + options.booking_end_times_ms[bbIndex] + "doctor start time plus 1s: " + (iiDate + 60 * 1000));
bbIndex++;
} else {
if (options.booking_start_times_ms[bbIndex] < nextDate - 60 * 1000) {
console.log("else if starttimes: " + options.booking_start_times_ms[bbIndex] + "doctor start time plus slottime(30s): " + (iiDate + 60 * 1000));
isAvail = false;
}
break;
}
if (++loopsCount >= loopsMax) {
console.error("Reached maximum loop count of", loopsMax);
break;
}
}
if (isAvail) {
console.log("Adding to available list: " + iiDate);
avail_dates.push(iiDate);
} else {
}
iiDate += slot_size;
if (++loopsCount >= loopsMax) {
console.error("Reached maximum loop count of", loopsMax);
break;
}
}
// bubble_fn_avail_dates(avail_dates);
/*
if(avail_dates.length == null){
while(maxRepeatDay<3){
var options ={
range_start_time_ms:,
range_end_time_ms:,
avail_size_mins: options.avail_size_mins,//will be the same
slot_size_mins: options.slot_size_mins,//will be the same
booking_start_times_ms:,
}
}*/
return avail_dates;
} catch (err) {
// bubble_fn_err(JSON.stringify(err));
console.error(err);
}
}
</script>
@iampato
Copy link
Author

iampato commented Jun 2, 2020

Notice the window.calculateAvailableDates now takes a list a parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment