Here are two methods that check the availability of a person for each day within a specified date range. These methods use two variables: one to count the total days between the start and end dates (dayCount) and the other to track the number of available days for the person (validDays).
If these two variables are not equal, an error message is displayed indicating that the person is not available.
The checkAvailable() function invokes the setAvailable() function, which in turn makes an AJAX call to verify the availability of the person on a particular date. The AJAX call returns either true or false, and this process works correctly.
The problem I'm encountering is that I have to click the button twice before the dayCount and validDays variables display the correct values. For example, if I select a PM booking, knowing that the person is available during that time, the first click on the button shows "Unavailable," while subsequent clicks correctly show "Available."
I initially thought the issue was related to asynchronous processes not completing before comparing the two variables, but upon adding some delays for verification, it seems like that's not the cause.
Javascript code:
var validDays = 0;
function checkAvailable() {
event.preventDefault();
var teacher = $("#selectedTeacher").val();
var startDate = new Date($("#startDate").val());
var endDate = new Date($("#endDate").val());
var dayCount = 0;
// Loop through each day
for (var day = startDate; day <= endDate; day.setDate(day.getDate() + 1)) {
console.log('date: ' + day);
var duration;
var starttime;
var endtime;
var dayOfWeek = day.getDay();
var monday = document.getElementById('enableMon');
var tuesday = document.getElementById('enableTue');
var wednesday = document.getElementById('enableWed');
var thursday = document.getElementById('enableThu');
var friday = document.getElementById('enableFri');
if (dayOfWeek == "1" && monday.checked) {
duration = $("#DurationMonday").val();
starttime = $("#startTimeMon").val();
endtime = $("#endTimeMon").val();
console.log('Add 1 to day count - ' + dayCount + '. Valid days are - ' + validDays);
setAvailable(teacher, day, duration);
dayCount++;
console.log('Day count is now - ' + dayCount + '. Valid days are - ' + validDays);
}
else if (dayOfWeek == "2" && tuesday.checked) {
duration = $("#DurationTuesday").val();
starttime = $("#startTimeTue").val();
endtime = $("#endTimeTue").val();
console.log('Add 1 to day count - ' + dayCount + '. Valid days are - ' + validDays);
setAvailable(teacher, day, duration);
dayCount++;
console.log('Day count is now - ' + dayCount + '. Valid days are - ' + validDays);
}
else if (dayOfWeek == "3" && wednesday.checked) {
duration = $("#DurationWednesday").val();
starttime = $("#startTimeWed").val();
endtime = $("#endTimeWed").val();
console.log('Add 1 to day count - ' + dayCount + '. Valid days are - ' + validDays);
setAvailable(teacher, day, duration);
dayCount++;
console.log('Day count is now - ' + dayCount + '. Valid days are - ' + validDays);
}
else if (dayOfWeek == "4" && thursday.checked) {
duration = $("#DurationThursday").val();
starttime = $("#startTimeThu").val();
endtime = $("#endTimeThu").val();
console.log('Add 1 to day count - ' + dayCount + '. Valid days are - ' + validDays);
setAvailable(teacher, day, duration);
dayCount++;
console.log('Day count is now - ' + dayCount + '. Valid days are - ' + validDays);
}
else if (dayOfWeek == "5" && friday.checked) {
duration = $("#DurationFriday").val();
starttime = $("#startTimeFri").val();
endtime = $("#endTimeFri").val();
console.log('Add 1 to day count - ' + dayCount + '. Valid days are - ' + validDays);
setAvailable(teacher, day, duration);
dayCount++;
console.log('Day count is now - ' + dayCount + '. Valid days are - ' + validDays);
}
}
if(dayCount == validDays)
{
toastNotifySuccess("Available", 2000);
$("#btnSave").attr('disabled', false);
}
else
{
toastNotifyError("The selected teacher is not available", 2000);
$("#btnSave").attr('disabled', true);
}
dayCount = 0;
validDays = 0;
};
async function setAvailable(teacher, day, duration) {
return await $.ajax({
url: '/Availability/CheckAvailabilityForDate?teacher=' + teacher + '&date=' + day.toISOString() + '&duration=' + duration,
type: 'POST',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response == false) {
}
else if(response == true) {
console.log('add 1 to valid days - ' + validDays);
validDays++;
console.log('added 1 to valid days - ' + validDays);
}
}
});
};
MVC:
[HttpPost]
public async Task<string> CheckAvailabilityForDate(int teacher, DateTime date, BookingDuration duration, DateTime startTime, DateTime endTime)
{
bool available = true;
available = await _availabilityService.CheckAvailable(date, teacher, duration, startTime, endTime, null);
return JsonSerializer.Serialize(available);
}