From 7af93aba765c2776e96bd7affad03803cc2e8a3a Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Thu, 7 May 2020 19:00:33 +0200 Subject: [PATCH] Use the array find method directly. --- .../js/backend_calendar_appointments_modal.js | 47 +++++++++---------- assets/js/backend_calendar_default_view.js | 4 +- assets/js/frontend_book.js | 4 +- assets/js/frontend_book_api.js | 8 ++-- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/assets/js/backend_calendar_appointments_modal.js b/assets/js/backend_calendar_appointments_modal.js index a7e1f632..c6075c39 100755 --- a/assets/js/backend_calendar_appointments_modal.js +++ b/assets/js/backend_calendar_appointments_modal.js @@ -25,9 +25,9 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa function updateTimezone() { var providerId = $('#select-provider').val(); - var provider = GlobalVariables.availableProviders.filter(function(availableProvider) { + var provider = GlobalVariables.availableProviders.find(function(availableProvider) { return Number(availableProvider.id) === Number(providerId); - }).shift(); + }); if (provider && provider.timezone) { $('.provider-timezone').text(GlobalVariables.timezones[provider.timezone]); @@ -145,12 +145,10 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa + $('#select-filter-item').val() + '"]').prop('selected', true); } - var serviceDuration = 0; - $.each(GlobalVariables.availableServices, function (index, service) { - if (Number(service.id) === Number($dialog.find('#select-service').val())) { - serviceDuration = service.duration; - return false; // exit loop - } + var serviceId = $dialog.find('#select-service').val(); + + var service = GlobalVariables.availableServices.find(function(availableService) { + return Number(availableService.id) === Number(serviceId); }); var start = new Date(); @@ -167,7 +165,7 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa } $dialog.find('#start-datetime').val(GeneralFunctions.formatDate(start, GlobalVariables.dateFormat, true)); - $dialog.find('#end-datetime').val(GeneralFunctions.formatDate(start.addMinutes(serviceDuration), + $dialog.find('#end-datetime').val(GeneralFunctions.formatDate(start.addMinutes(service.duration), GlobalVariables.dateFormat, true)); // Display modal form. @@ -205,24 +203,25 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa * Event: Select Existing Customer From List "Click" */ $('#manage-appointment').on('click', '#existing-customers-list div', function () { - var id = $(this).attr('data-id'); + var customerId = $(this).attr('data-id'); - $.each(GlobalVariables.customers, function (index, customer) { - if (Number(customer.id) === Number(id)) { - $('#customer-id').val(customer.id); - $('#first-name').val(customer.first_name); - $('#last-name').val(customer.last_name); - $('#email').val(customer.email); - $('#phone-number').val(customer.phone_number); - $('#address').val(customer.address); - $('#city').val(customer.city); - $('#zip-code').val(customer.zip_code); - $('#customer-notes').val(customer.notes); - return false; - } + var customer = GlobalVariables.customers.find(function (customer) { + return Number(customer.id) === Number(customerId); }); - $('#select-customer').trigger('click'); // hide list + if (customer) { + $('#customer-id').val(customer.id); + $('#first-name').val(customer.first_name); + $('#last-name').val(customer.last_name); + $('#email').val(customer.email); + $('#phone-number').val(customer.phone_number); + $('#address').val(customer.address); + $('#city').val(customer.city); + $('#zip-code').val(customer.zip_code); + $('#customer-notes').val(customer.notes); + } + + $('#select-customer').trigger('click'); // Hide the list. }); /** diff --git a/assets/js/backend_calendar_default_view.js b/assets/js/backend_calendar_default_view.js index fae70db8..4da8e9cf 100755 --- a/assets/js/backend_calendar_default_view.js +++ b/assets/js/backend_calendar_default_view.js @@ -234,9 +234,9 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; var providerId = $('#select-filter-item').val(); - var provider = GlobalVariables.availableProviders.filter(function(availableProvider) { + var provider = GlobalVariables.availableProviders.find(function(availableProvider) { return Number(availableProvider.id) === Number(providerId); - }).shift(); + }); if (provider && provider.timezone) { $('.provider-timezone').text(GlobalVariables.timezones[provider.timezone]); diff --git a/assets/js/frontend_book.js b/assets/js/frontend_book.js index 13c2e3ed..c138961f 100644 --- a/assets/js/frontend_book.js +++ b/assets/js/frontend_book.js @@ -721,9 +721,9 @@ window.FrontendBook = window.FrontendBook || {}; $serviceDescription.empty(); - var service = GlobalVariables.availableServices.filter(function(availableService) { + var service = GlobalVariables.availableServices.find(function(availableService) { return Number(availableService.id) === Number(serviceId); - }).shift(); + }); if (!service) { return; diff --git a/assets/js/frontend_book_api.js b/assets/js/frontend_book_api.js index f606cce4..dc2ef92c 100755 --- a/assets/js/frontend_book_api.js +++ b/assets/js/frontend_book_api.js @@ -43,9 +43,9 @@ window.FrontendBookApi = window.FrontendBookApi || {}; // Default value of duration (in minutes). var serviceDuration = 15; - var service = GlobalVariables.availableServices.filter(function(availableService) { + var service = GlobalVariables.availableServices.find(function(availableService) { return Number(availableService.id) === Number(serviceId); - }).shift(); + }); if (service) { serviceDuration = service.duration; @@ -78,9 +78,9 @@ window.FrontendBookApi = window.FrontendBookApi || {}; providerId = GlobalVariables.availableProviders[0].id; // Use first available provider. } - var provider = GlobalVariables.availableProviders.filter(function(availableProvider) { + var provider = GlobalVariables.availableProviders.find(function(availableProvider) { return Number(providerId) === Number(availableProvider.id); - }).shift(); + }); if (!provider) { throw new Error('Could not find provider.');