Use the array find method directly.

This commit is contained in:
Alex Tselegidis 2020-05-07 19:00:33 +02:00
parent 70948615df
commit 7af93aba76
4 changed files with 31 additions and 32 deletions

View file

@ -25,9 +25,9 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa
function updateTimezone() { function updateTimezone() {
var providerId = $('#select-provider').val(); 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); return Number(availableProvider.id) === Number(providerId);
}).shift(); });
if (provider && provider.timezone) { if (provider && provider.timezone) {
$('.provider-timezone').text(GlobalVariables.timezones[provider.timezone]); $('.provider-timezone').text(GlobalVariables.timezones[provider.timezone]);
@ -145,12 +145,10 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa
+ $('#select-filter-item').val() + '"]').prop('selected', true); + $('#select-filter-item').val() + '"]').prop('selected', true);
} }
var serviceDuration = 0; var serviceId = $dialog.find('#select-service').val();
$.each(GlobalVariables.availableServices, function (index, service) {
if (Number(service.id) === Number($dialog.find('#select-service').val())) { var service = GlobalVariables.availableServices.find(function(availableService) {
serviceDuration = service.duration; return Number(availableService.id) === Number(serviceId);
return false; // exit loop
}
}); });
var start = new Date(); 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('#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)); GlobalVariables.dateFormat, true));
// Display modal form. // Display modal form.
@ -205,24 +203,25 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa
* Event: Select Existing Customer From List "Click" * Event: Select Existing Customer From List "Click"
*/ */
$('#manage-appointment').on('click', '#existing-customers-list div', function () { $('#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) { var customer = GlobalVariables.customers.find(function (customer) {
if (Number(customer.id) === Number(id)) { return Number(customer.id) === Number(customerId);
$('#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;
}
}); });
$('#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.
}); });
/** /**

View file

@ -234,9 +234,9 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {};
var providerId = $('#select-filter-item').val(); 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); return Number(availableProvider.id) === Number(providerId);
}).shift(); });
if (provider && provider.timezone) { if (provider && provider.timezone) {
$('.provider-timezone').text(GlobalVariables.timezones[provider.timezone]); $('.provider-timezone').text(GlobalVariables.timezones[provider.timezone]);

View file

@ -721,9 +721,9 @@ window.FrontendBook = window.FrontendBook || {};
$serviceDescription.empty(); $serviceDescription.empty();
var service = GlobalVariables.availableServices.filter(function(availableService) { var service = GlobalVariables.availableServices.find(function(availableService) {
return Number(availableService.id) === Number(serviceId); return Number(availableService.id) === Number(serviceId);
}).shift(); });
if (!service) { if (!service) {
return; return;

View file

@ -43,9 +43,9 @@ window.FrontendBookApi = window.FrontendBookApi || {};
// Default value of duration (in minutes). // Default value of duration (in minutes).
var serviceDuration = 15; var serviceDuration = 15;
var service = GlobalVariables.availableServices.filter(function(availableService) { var service = GlobalVariables.availableServices.find(function(availableService) {
return Number(availableService.id) === Number(serviceId); return Number(availableService.id) === Number(serviceId);
}).shift(); });
if (service) { if (service) {
serviceDuration = service.duration; serviceDuration = service.duration;
@ -78,9 +78,9 @@ window.FrontendBookApi = window.FrontendBookApi || {};
providerId = GlobalVariables.availableProviders[0].id; // Use first available provider. 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); return Number(providerId) === Number(availableProvider.id);
}).shift(); });
if (!provider) { if (!provider) {
throw new Error('Could not find provider.'); throw new Error('Could not find provider.');