/* ---------------------------------------------------------------------------- * Easy!Appointments - Online Appointment 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.5.0 * ---------------------------------------------------------------------------- */ /** * Booking HTTP client. * * This module implements the booking related HTTP requests. * * Old Name: FrontendBookApi */ App.Http.Booking = (function () { const $selectDate = $('#select-date'); const $selectService = $('#select-service'); const $selectProvider = $('#select-provider'); const $availableHours = $('#available-hours'); const $captchaHint = $('#captcha-hint'); const $captchaTitle = $('.captcha-title'); const MONTH_SEARCH_LIMIT = 2; // Months in the future const moment = window.moment; let unavailableDatesBackup; let selectedDateStringBackup; let processingUnavailableDates = false; let searchedMonthStart; let searchedMonthCounter = 0; /** * 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) { $availableHours.empty(); // Find the selected service duration (it is going to be send within the "data" object). const serviceId = $selectService.val(); const providerId = $selectProvider.val() ? $selectProvider.val() : App.Utils.Url.queryParam('provider'); // Default value of duration (in minutes). let serviceDuration = 15; const service = 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 = vars('manage_mode') ? vars('appointment_data').id : null; // Make ajax post request and get the available hours. const url = App.Utils.Url.siteUrl('booking/get_available_hours'); const data = { csrf_token: vars('csrf_token'), service_id: $selectService.val(), provider_id: providerId, selected_date: selectedDate, service_duration: serviceDuration, manage_mode: Number(vars('manage_mode') || 0), appointment_id: appointmentId, }; $.post(url, data).done((response) => { $availableHours.empty(); // 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 = $selectProvider.val(); // if (providerId === 'any-provider') { // for (const availableProvider of vars('available_providers')) { // if (availableProvider.services.indexOf(Number(serviceId)) !== -1) { // providerId = availableProvider.id; // Use first available provider. // break; // } // } // } const provider = 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 = 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. } $availableHours.append( $('