/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 * @link http://easyappointments.org * @since v1.0.0 * ---------------------------------------------------------------------------- */ window.FrontendBookApi = window.FrontendBookApi || {}; /** * Frontend Book API * * This module serves as the API consumer for the booking wizard of the app. * * @module FrontendBookApi */ (function (exports) { 'use strict'; var unavailableDatesBackup; var selectedDateStringBackup; var 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. */ exports.getAvailableHours = function (selectedDate) { $('#available-hours').empty(); // Find the selected service duration (it is going to be send within the "data" object). var serviceId = $('#select-service').val(); // Default value of duration (in minutes). var serviceDuration = 15; var service = GlobalVariables.availableServices.find(function (availableService) { return 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. var appointmentId = FrontendBook.manageMode ? GlobalVariables.appointmentData.id : null; // Make ajax post request and get the available hours. var url = GlobalVariables.baseUrl + '/index.php/appointments/ajax_get_available_hours'; var data = { csrfToken: GlobalVariables.csrfToken, service_id: $('#select-service').val(), provider_id: $('#select-provider').val(), selected_date: selectedDate, service_duration: serviceDuration, manage_mode: FrontendBook.manageMode, appointment_id: appointmentId }; $.post(url, data) .done(function (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) { var providerId = $('#select-provider').val(); if (providerId === 'any-provider') { for (var availableProvider of GlobalVariables.availableProviders) { if (availableProvider.services.indexOf(serviceId) !== -1) { providerId = availableProvider.id; // Use first available provider. break; } } } var provider = GlobalVariables.availableProviders.find(function (availableProvider) { return Number(providerId) === Number(availableProvider.id); }); if (!provider) { throw new Error('Could not find provider.'); } var providerTimezone = provider.timezone; var selectedTimezone = $('#select-timezone').val(); var timeFormat = GlobalVariables.timeFormat === 'regular' ? 'h:mm a' : 'HH:mm'; response.forEach(function (availableHour) { var 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( $('