/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) Alex Tselegidis * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 * @link https://easyappointments.org * @since v1.0.0 * ---------------------------------------------------------------------------- */ /** * Booking HTTP Client * * This module serves as the API consumer for the booking wizard of the app. * * Old Name: FrontendBookApi */ App.Http.Booking = (function () { let unavailableDatesBackup; let selectedDateStringBackup; let processingUnavailabilities = false; /** * Get Available Hours * * This function makes an AJAX call and returns the available hours for the selected service, * provider and date. * * @param {String} selectedDate The selected date of the available hours we need. */ function getAvailableHours(selectedDate) { $('#available-hours').empty(); // Find the selected service duration (it is going to be send within the "data" object). const serviceId = $('#select-service').val(); // Default value of duration (in minutes). let serviceDuration = 15; const service = App.Vars.available_services.find( (availableService) => Number(availableService.id) === Number(serviceId) ); if (service) { serviceDuration = service.duration; } // If the manage mode is true then the appointment's start date should return as available too. const appointmentId = App.Pages.Booking.manageMode ? App.Vars.appointment_data.id : null; // Make ajax post request and get the available hours. const url = App.Utils.Url.siteUrl('booking/ajax_get_available_hours'); const data = { csrf_token: App.Vars.csrf_token, service_id: $('#select-service').val(), provider_id: $('#select-provider').val(), selected_date: selectedDate, service_duration: serviceDuration, manage_mode: App.Pages.Booking.manageMode, appointment_id: appointmentId }; $.post(url, data).done((response) => { // The response contains the available hours for the selected provider and service. Fill the available // hours div with response data. if (response.length > 0) { let providerId = $('#select-provider').val(); if (providerId === 'any-provider') { for (const availableProvider of App.Vars.available_providers) { if (availableProvider.services.indexOf(Number(serviceId)) !== -1) { providerId = availableProvider.id; // Use first available provider. break; } } } const provider = App.Vars.available_providers.find( (availableProvider) => Number(providerId) === Number(availableProvider.id) ); if (!provider) { throw new Error('Could not find provider.'); } const providerTimezone = provider.timezone; const selectedTimezone = $('#select-timezone').val(); const timeFormat = App.Vars.time_format === 'regular' ? 'h:mm a' : 'HH:mm'; response.forEach((availableHour) => { const availableHourMoment = moment .tz(selectedDate + ' ' + availableHour + ':00', providerTimezone) .tz(selectedTimezone); if (availableHourMoment.format('YYYY-MM-DD') !== selectedDate) { return; // Due to the selected timezone the available hour belongs to another date. } $('#available-hours').append( $('