/* ---------------------------------------------------------------------------- * 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 implements the booking related HTTP requests. * * Old Name: FrontendBookApi */ App.Http.Booking = (function () { const $selectService = $('#select-service'); const $selectProvider = $('#select-provider'); const $availableHours = $('#available-hours'); const $captchaHint = $('#captcha-hint'); const $captchaTitle = $('.captcha-title'); let unavailabilityDatesBackup; 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) { $availableHours.empty(); // Find the selected service duration (it is going to be send within the "data" object). const serviceId = $selectService.val(); // 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 = App.Pages.Booking.manageMode ? 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: $selectProvider.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 = $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( $('