Code refactoring and improvements for the customers page.

This commit is contained in:
Alex Tselegidis 2022-01-17 14:24:58 +01:00
parent 8b74297fb3
commit ce853c99ef

View file

@ -16,22 +16,36 @@
*/ */
App.Pages.Customers = (function () { App.Pages.Customers = (function () {
const $customers = $('#customers'); const $customers = $('#customers');
const $filterCustomers = $('#filter-customers');
const $id = $('#customer-id');
const $firstName = $('#first-name');
const $lastName = $('#last-name');
const $email = $('#email');
const $phoneNumber = $('#phone-number');
const $address = $('#address');
const $city = $('#city');
const $zipCode = $('#zip-code');
const $timezone = $('#timezone');
const $language = $('#language');
const $notes = $('#notes');
const $formMessage = $('#form-message');
const $customerAppointments = $('#customer-appointments');
let filterResults = {}; let filterResults = {};
let filterLimit = 20; let filterLimit = 20;
/** /**
* Bind the event handlers. * Add the page event listeners.
*/ */
function bindEventHandlers() { function addEventListeners() {
/** /**
* Event: Filter Customers Form "Submit" * Event: Filter Customers Form "Submit"
* *
* @param {jQuery.Event} event * @param {jQuery.Event} event
*/ */
$customers.on('submit', '#filter-customers form', function (event) { $customers.on('submit', '#filter-customers form', (event) => {
event.preventDefault(); event.preventDefault();
const key = $('#filter-customers .key').val(); const key = $filterCustomers.find('.key').val();
$('#filter-customers .selected').removeClass('selected'); $filterCustomers.find('.selected').removeClass('selected');
filterLimit = 20; filterLimit = 20;
resetForm(); resetForm();
filter(key); filter(key);
@ -41,52 +55,54 @@ App.Pages.Customers = (function () {
* Event: Filter Entry "Click" * Event: Filter Entry "Click"
* *
* Display the customer data of the selected row. * Display the customer data of the selected row.
*
* @param {jQuery.Event} event
*/ */
$customers.on('click', '.customer-row', function () { $customers.on('click', '.customer-row', (event) => {
if ($('#filter-customers .filter').prop('disabled')) { if ($filterCustomers.find('.filter').prop('disabled')) {
return; // Do nothing when user edits a customer record. return; // Do nothing when user edits a customer record.
} }
const customerId = $(this).attr('data-id'); const customerId = $(event.target).attr('data-id');
const customer = filterResults.find(function (filterResult) { const customer = filterResults.find((filterResult) => Number(filterResult.id) === Number(customerId));
return Number(filterResult.id) === Number(customerId);
});
display(customer); display(customer);
$('#filter-customers .selected').removeClass('selected'); $('#filter-customers .selected').removeClass('selected');
$(this).addClass('selected'); $(event.target).addClass('selected');
$('#edit-customer, #delete-customer').prop('disabled', false); $('#edit-customer, #delete-customer').prop('disabled', false);
}); });
/** /**
* Event: Add Customer Button "Click" * Event: Add Customer Button "Click"
*/ */
$customers.on('click', '#add-customer', function () { $customers.on('click', '#add-customer', () => {
resetForm(); resetForm();
$('#add-edit-delete-group').hide(); $customers.find('#add-edit-delete-group').hide();
$('#save-cancel-group').show(); $customers.find('#save-cancel-group').show();
$('.record-details').find('input, select, textarea').prop('disabled', false); $customers.find('.record-details').find('input, select, textarea').prop('disabled', false);
$('#filter-customers button').prop('disabled', true); $filterCustomers.find('button').prop('disabled', true);
$('#filter-customers .results').css('color', '#AAA'); $filterCustomers.find('.results').css('color', '#AAA');
}); });
/** /**
* Event: Edit Customer Button "Click" * Event: Edit Customer Button "Click"
*/ */
$customers.on('click', '#edit-customer', function () { $customers.on('click', '#edit-customer', () => {
$('.record-details').find('input, select, textarea').prop('disabled', false); $customers.find('.record-details').find('input, select, textarea').prop('disabled', false);
$('#add-edit-delete-group').hide(); $customers.find('#add-edit-delete-group').hide();
$('#save-cancel-group').show(); $customers.find('#save-cancel-group').show();
$('#filter-customers button').prop('disabled', true); $filterCustomers.find('button').prop('disabled', true);
$('#filter-customers .results').css('color', '#AAA'); $filterCustomers.find('.results').css('color', '#AAA');
}); });
/** /**
* Event: Cancel Customer Add/Edit Operation Button "Click" * Event: Cancel Customer Add/Edit Operation Button "Click"
*/ */
$customers.on('click', '#cancel-customer', function () { $customers.on('click', '#cancel-customer', () => {
const id = $('#customer-id').val(); const id = $id.val();
resetForm(); resetForm();
if (id) { if (id) {
select(id, true); select(id, true);
} }
@ -95,22 +111,22 @@ App.Pages.Customers = (function () {
/** /**
* Event: Save Add/Edit Customer Operation "Click" * Event: Save Add/Edit Customer Operation "Click"
*/ */
$customers.on('click', '#save-customer', function () { $customers.on('click', '#save-customer', () => {
const customer = { const customer = {
first_name: $('#first-name').val(), first_name: $firstName.val(),
last_name: $('#last-name').val(), last_name: $lastName.val(),
email: $('#email').val(), email: $email.val(),
phone_number: $('#phone-number').val(), phone_number: $phoneNumber.val(),
address: $('#address').val(), address: $address.val(),
city: $('#city').val(), city: $city.val(),
zip_code: $('#zip-code').val(), zip_code: $zipCode.val(),
notes: $('#notes').val(), notes: $notes.val(),
timezone: $('#timezone').val(), timezone: $timezone.val(),
language: $('#language').val() || 'english' language: $language.val() || 'english'
}; };
if ($('#customer-id').val()) { if ($id.val()) {
customer.id = $('#customer-id').val(); customer.id = $id.val();
} }
if (!validate()) { if (!validate()) {
@ -123,18 +139,18 @@ App.Pages.Customers = (function () {
/** /**
* Event: Delete Customer Button "Click" * Event: Delete Customer Button "Click"
*/ */
$customers.on('click', '#delete-customer', function () { $customers.on('click', '#delete-customer', () => {
const customerId = $('#customer-id').val(); const customerId = $id.val();
const buttons = [ const buttons = [
{ {
text: App.Lang.cancel, text: App.Lang.cancel,
click: function () { click: () => {
$('#message-box').dialog('close'); $('#message-box').dialog('close');
} }
}, },
{ {
text: App.Lang.delete, text: App.Lang.delete,
click: function () { click: () => {
remove(customerId); remove(customerId);
$('#message-box').dialog('close'); $('#message-box').dialog('close');
} }
@ -176,14 +192,14 @@ App.Pages.Customers = (function () {
* Validate customer data before save (insert or update). * Validate customer data before save (insert or update).
*/ */
function validate() { function validate() {
$('#form-message').removeClass('alert-danger').hide(); $formMessage.removeClass('alert-danger').hide();
$('.is-invalid').removeClass('is-invalid'); $('.is-invalid').removeClass('is-invalid');
try { try {
// Validate required fields. // Validate required fields.
let missingRequired = false; let missingRequired = false;
$('.required').each(function (index, requiredField) { $('.required').each((index, requiredField) => {
if ($(requiredField).val() === '') { if ($(requiredField).val() === '') {
$(requiredField).addClass('is-invalid'); $(requiredField).addClass('is-invalid');
missingRequired = true; missingRequired = true;
@ -195,14 +211,14 @@ App.Pages.Customers = (function () {
} }
// Validate email address. // Validate email address.
if (!App.Utils.Validation.email($('#email').val())) { if (!App.Utils.Validation.email($email.val())) {
$('#email').addClass('is-invalid'); $email.addClass('is-invalid');
throw new Error(App.Lang.invalid_email); throw new Error(App.Lang.invalid_email);
} }
return true; return true;
} catch (error) { } catch (error) {
$('#form-message').addClass('alert-danger').text(error.message).show(); $formMessage.addClass('alert-danger').text(error.message).show();
return false; return false;
} }
} }
@ -211,22 +227,23 @@ App.Pages.Customers = (function () {
* Bring the customer form back to its initial state. * Bring the customer form back to its initial state.
*/ */
function resetForm() { function resetForm() {
$('.record-details').find('input, select, textarea').val('').prop('disabled', true); $customers.find('.record-details').find('input, select, textarea').val('').prop('disabled', true);
$('.record-details #timezone').val('UTC'); $customers.find('.record-details #timezone').val('UTC');
$('#language').val('english'); $language.val('english');
$('#customer-appointments').empty(); $customerAppointments.empty();
$('#edit-customer, #delete-customer').prop('disabled', true);
$('#add-edit-delete-group').show();
$('#save-cancel-group').hide();
$('.record-details .is-invalid').removeClass('is-invalid'); $customers.find('#edit-customer, #delete-customer').prop('disabled', true);
$('.record-details #form-message').hide(); $customers.find('#add-edit-delete-group').show();
$customers.find('#save-cancel-group').hide();
$('#filter-customers button').prop('disabled', false); $customers.find('.record-details .is-invalid').removeClass('is-invalid');
$('#filter-customers .selected').removeClass('selected'); $customers.find('.record-details #form-message').hide();
$('#filter-customers .results').css('color', '');
$filterCustomers.find('button').prop('disabled', false);
$filterCustomers.find('.selected').removeClass('selected');
$filterCustomers.find('.results').css('color', '');
} }
/** /**
@ -235,27 +252,27 @@ App.Pages.Customers = (function () {
* @param {Object} customer Contains the customer record data. * @param {Object} customer Contains the customer record data.
*/ */
function display(customer) { function display(customer) {
$('#customer-id').val(customer.id); $id.val(customer.id);
$('#first-name').val(customer.first_name); $firstName.val(customer.first_name);
$('#last-name').val(customer.last_name); $lastName.val(customer.last_name);
$('#email').val(customer.email); $email.val(customer.email);
$('#phone-number').val(customer.phone_number); $phoneNumber.val(customer.phone_number);
$('#address').val(customer.address); $address.val(customer.address);
$('#city').val(customer.city); $city.val(customer.city);
$('#zip-code').val(customer.zip_code); $zipCode.val(customer.zip_code);
$('#notes').val(customer.notes); $notes.val(customer.notes);
$('#timezone').val(customer.timezone); $timezone.val(customer.timezone);
$('#language').val(customer.language || 'english'); $language.val(customer.language || 'english');
$('#customer-appointments').empty(); $customerAppointments.empty();
if (!customer.appointments.length) { if (!customer.appointments.length) {
$('<p/>', { $('<p/>', {
'text': App.Lang.no_records_found 'text': App.Lang.no_records_found
}).appendTo('#customer-appointments'); }).appendTo($customerAppointments);
} }
customer.appointments.forEach(function (appointment) { customer.appointments.forEach((appointment) => {
if ( if (
App.Vars.role_slug === App.Layouts.Backend.DB_SLUG_PROVIDER && App.Vars.role_slug === App.Layouts.Backend.DB_SLUG_PROVIDER &&
parseInt(appointment.id_users_provider) !== App.Vars.user_id parseInt(appointment.id_users_provider) !== App.Vars.user_id
@ -344,16 +361,14 @@ App.Pages.Customers = (function () {
App.Http.Customers.search(keyword, filterLimit).then((response) => { App.Http.Customers.search(keyword, filterLimit).then((response) => {
filterResults = response; filterResults = response;
$('#filter-customers .results').empty(); $filterCustomers.find('.results').empty();
response.forEach( response.forEach((customer) => {
function (customer) { $('#filter-customers .results').append(getFilterHtml(customer)).append($('<hr/>'));
$('#filter-customers .results').append(getFilterHtml(customer)).append($('<hr/>')); });
}.bind(this)
);
if (!response.length) { if (!response.length) {
$('#filter-customers .results').append( $filterCustomers.find('.results').append(
$('<em/>', { $('<em/>', {
'text': App.Lang.no_records_found 'text': App.Lang.no_records_found
}) })
@ -363,10 +378,10 @@ App.Pages.Customers = (function () {
'type': 'button', 'type': 'button',
'class': 'btn btn-outline-secondary w-100 load-more text-center', 'class': 'btn btn-outline-secondary w-100 load-more text-center',
'text': App.Lang.load_more, 'text': App.Lang.load_more,
'click': function () { 'click': () => {
filterLimit += 20; filterLimit += 20;
filter(keyword, selectId, show); filter(keyword, selectId, show);
}.bind(this) }
}).appendTo('#filter-customers .results'); }).appendTo('#filter-customers .results');
} }
@ -412,8 +427,7 @@ App.Pages.Customers = (function () {
* If the customer id does not exist in the list then no record will be selected. * If the customer id does not exist in the list then no record will be selected.
* *
* @param {Number} id The record id to be selected from the filter results. * @param {Number} id The record id to be selected from the filter results.
* @param {Boolean} show Optional (false), if true then the method will display the record * @param {Boolean} show Optional (false), if true then the method will display the record on the form.
* on the form.
*/ */
function select(id, show = false) { function select(id, show = false) {
$('#filter-customers .selected').removeClass('selected'); $('#filter-customers .selected').removeClass('selected');
@ -421,9 +435,7 @@ App.Pages.Customers = (function () {
$('#filter-customers .entry[data-id="' + id + '"]').addClass('selected'); $('#filter-customers .entry[data-id="' + id + '"]').addClass('selected');
if (show) { if (show) {
const customer = filterResults.find(function (filterResult) { const customer = filterResults.find((filterResult) => Number(filterResult.id) === Number(id));
return Number(filterResult.id) === Number(id);
});
display(customer); display(customer);
@ -436,7 +448,7 @@ App.Pages.Customers = (function () {
*/ */
function initialize() { function initialize() {
resetForm(); resetForm();
bindEventHandlers(); addEventListeners();
filter(''); filter('');
} }