easyappointments/assets/js/http/booking_http_client.min.js

1 line
8.5 KiB
JavaScript
Executable File

"use strict";function _createForOfIteratorHelper(o,allowArrayLike){var it="undefined"!=typeof Symbol&&o[Symbol.iterator]||o["@@iterator"];if(!it){if(Array.isArray(o)||(it=_unsupportedIterableToArray(o))||allowArrayLike&&o&&"number"==typeof o.length){it&&(o=it);var i=0,F=function(){};return{s:F,n:function n(){return i>=o.length?{done:!0}:{done:!1,value:o[i++]}},e:function e(_e){throw _e},f:F}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var err,normalCompletion=!0,didErr=!1;return{s:function s(){it=it.call(o)},n:function n(){var step=it.next();return normalCompletion=step.done,step},e:function e(_e2){didErr=!0,err=_e2},f:function f(){try{normalCompletion||null==it["return"]||it["return"]()}finally{if(didErr)throw err}}}}function _unsupportedIterableToArray(o,minLen){if(o){if("string"==typeof o)return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);return"Object"===n&&o.constructor&&(n=o.constructor.name),"Map"===n||"Set"===n?Array.from(o):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(o,minLen):void 0}}function _arrayLikeToArray(arr,len){(null==len||len>arr.length)&&(len=arr.length);for(var i=0,arr2=Array(len);i<len;i++)arr2[i]=arr[i];return arr2}App.Http.Booking=function(){function getAvailableHours(selectedDate){$availableHours.empty();var serviceId=$selectService.val(),serviceDuration=15,service=vars("available_services").find(function(availableService){return+availableService.id===+serviceId});service&&(serviceDuration=service.duration);var appointmentId=vars("manage_mode")?vars("appointment_data").id:null,url=App.Utils.Url.siteUrl("booking/get_available_hours"),data={csrf_token:vars("csrf_token"),service_id:$selectService.val(),provider_id:$selectProvider.val(),selected_date:selectedDate,service_duration:serviceDuration,manage_mode:+(vars("manage_mode")||0),appointment_id:appointmentId};$.post(url,data).done(function(response){if($availableHours.empty(),0<response.length){var providerId=$selectProvider.val();if("any-provider"===providerId){var _step,_iterator=_createForOfIteratorHelper(vars("available_providers"));try{for(_iterator.s();!(_step=_iterator.n()).done;){var availableProvider=_step.value;if(-1!==availableProvider.services.indexOf(+serviceId)){providerId=availableProvider.id;break}}}catch(err){_iterator.e(err)}finally{_iterator.f()}}var provider=vars("available_providers").find(function(availableProvider){return+providerId===+availableProvider.id});if(!provider)throw new Error("Could not find provider.");var providerTimezone=provider.timezone,selectedTimezone=$("#select-timezone").val(),timeFormat="regular"===vars("time_format")?"h:mm a":"HH:mm";response.forEach(function(availableHour){var availableHourMoment=moment.tz(selectedDate+" "+availableHour+":00",providerTimezone).tz(selectedTimezone);availableHourMoment.format("YYYY-MM-DD")!==selectedDate||$availableHours.append($("<button/>",{class:"btn btn-outline-secondary w-100 shadow-none available-hour",data:{value:availableHour},text:availableHourMoment.format(timeFormat)}))}),App.Pages.Booking.manageMode?$(".available-hour").removeClass("selected-hour").filter(function(index,availableHourEl){return $(availableHourEl).text()===moment(vars("appointment_data").start_datetime).format(timeFormat)}).addClass("selected-hour"):$(".available-hour:eq(0)").addClass("selected-hour"),App.Pages.Booking.updateConfirmFrame()}$availableHours.find(".available-hour").length||$availableHours.text(lang("no_available_hours"))})}function getUnavailableDates(providerId,serviceId,selectedDateString){var monthChangeStep=3<arguments.length&&arguments[3]!==void 0?arguments[3]:1;if(!processingUnavailableDates&&providerId&&serviceId){var appointmentId=App.Pages.Booking.manageMode?vars("appointment_data").id:null,url=App.Utils.Url.siteUrl("booking/get_unavailable_dates"),data={provider_id:providerId,service_id:serviceId,selected_date:encodeURIComponent(selectedDateString),csrf_token:vars("csrf_token"),manage_mode:+App.Pages.Booking.manageMode,appointment_id:appointmentId};$.ajax({url:url,type:"GET",data:data,dataType:"json"}).done(function(response){if(response.is_month_unavailable){if(searchedMonthStart||(searchedMonthStart=selectedDateString),2<=searchedMonthCounter){for(var _selectedDateMoment=moment(searchedMonthStart),startOfMonthMoment=_selectedDateMoment.clone().startOf("month"),endOfMonthMoment=_selectedDateMoment.clone().endOf("month"),unavailableDates=[];startOfMonthMoment.isSameOrBefore(endOfMonthMoment);)unavailableDates.push(startOfMonthMoment.format("YYYY-MM-DD")),startOfMonthMoment.add(monthChangeStep,"days");return applyUnavailableDates(unavailableDates,searchedMonthStart,!0),searchedMonthStart=void 0,void(searchedMonthCounter=0)}searchedMonthCounter++;var selectedDateMoment=moment(selectedDateString);selectedDateMoment.add(1,"month");var nextSelectedDate=selectedDateMoment.format("YYYY-MM-DD");return void getUnavailableDates(providerId,serviceId,nextSelectedDate,monthChangeStep)}unavailableDatesBackup=response,selectedDateStringBackup=selectedDateString,applyUnavailableDates(response,selectedDateString,!0)}).fail(function(){$selectDate.parent().fadeTo(400,1)})}}function applyPreviousUnavailableDates(){applyUnavailableDates(unavailableDatesBackup,selectedDateStringBackup)}function applyUnavailableDates(unavailableDates,selectedDateString,setDate){setDate=setDate||!1,$selectDate.parent().fadeTo(400,1),processingUnavailableDates=!0;var selectedDateMoment=moment(selectedDateString),selectedDate=selectedDateMoment.toDate(),numberOfDays=selectedDateMoment.daysInMonth();if(unavailableDates.length===numberOfDays&&$availableHours.text(lang("no_available_hours")),$selectDate[0]._flatpickr.set("disable",unavailableDates.map(function(unavailableDate){return new Date(unavailableDate)})),setDate&&!vars("manage_mode"))for(var currentDate,i=1;i<=numberOfDays;i++)if(currentDate=new Date(selectedDate.getFullYear(),selectedDate.getMonth(),i),-1===unavailableDates.indexOf(moment(currentDate).format("YYYY-MM-DD"))){App.Utils.UI.setDateTimePickerValue($selectDate,currentDate),getAvailableHours(moment(currentDate).format("YYYY-MM-DD"));break}var dateQueryParam=App.Utils.Url.queryParam("date");if(dateQueryParam){var dateQueryParamMoment=moment(dateQueryParam);dateQueryParamMoment.isValid()&&!unavailableDates.includes(dateQueryParam)&&dateQueryParamMoment.format("YYYY-MM")===selectedDateMoment.format("YYYY-MM")&&App.Utils.UI.setDateTimePickerValue($selectDate,dateQueryParamMoment.toDate())}searchedMonthStart=void 0,searchedMonthCounter=0,processingUnavailableDates=!1}var unavailableDatesBackup,selectedDateStringBackup,searchedMonthStart,$selectDate=$("#select-date"),$selectService=$("#select-service"),$selectProvider=$("#select-provider"),$availableHours=$("#available-hours"),$captchaHint=$("#captcha-hint"),$captchaTitle=$(".captcha-title"),moment=window.moment,processingUnavailableDates=!1,searchedMonthCounter=0;return{registerAppointment:function registerAppointment(){var $captchaText=$(".captcha-text");if(0<$captchaText.length&&($captchaText.removeClass("is-invalid"),""===$captchaText.val()))return void $captchaText.addClass("is-invalid");var formData=JSON.parse($("input[name=\"post_data\"]").val()),data={csrf_token:vars("csrf_token"),post_data:formData};0<$captchaText.length&&(data.captcha=$captchaText.val()),vars("manage_mode")&&(data.exclude_appointment_id=vars("appointment_data").id);var url=App.Utils.Url.siteUrl("booking/register"),$layer=$("<div/>");$.ajax({url:url,method:"post",data:data,dataType:"json",beforeSend:function beforeSend(){$layer.appendTo("body").css({background:"white",position:"fixed",top:"0",left:"0",height:"100vh",width:"100vw",opacity:"0.5"})}}).done(function(response){return!1===response.captcha_verification?($captchaHint.text(lang("captcha_is_wrong")).fadeTo(400,1),setTimeout(function(){$captchaHint.fadeTo(400,0)},3e3),$captchaTitle.find("button").trigger("click"),$captchaText.addClass("is-invalid"),!1):void(window.location.href=App.Utils.Url.siteUrl("booking_confirmation/of/"+response.appointment_hash))}).fail(function(){$captchaTitle.find("button").trigger("click")}).always(function(){$layer.remove()})},getAvailableHours:getAvailableHours,getUnavailableDates:getUnavailableDates,applyPreviousUnavailableDates:applyPreviousUnavailableDates,deletePersonalInformation:function deletePersonalInformation(customerToken){var url=App.Utils.Url.siteUrl("privacy/delete_personal_information"),data={csrf_token:vars("csrf_token"),customer_token:customerToken};$.post(url,data).done(function(){window.location.href=vars("base_url")})}}}();