Code refactoring and improvements for the secretaries page.

This commit is contained in:
Alex Tselegidis 2022-01-17 14:56:08 +01:00
parent 390d66f8d2
commit f004807014

View file

@ -16,21 +16,41 @@
*/ */
App.Pages.Secretaries = (function () { App.Pages.Secretaries = (function () {
const $secretaries = $('#secretaries'); const $secretaries = $('#secretaries');
const $id = $('#secretary-id');
const $firstName = $('#secretary-first-name');
const $lastName = $('#secretary-last-name');
const $email = $('#secretary-email');
const $mobileNumber = $('#secretary-mobile-number');
const $phoneNumber = $('#secretary-phone-number');
const $address = $('#secretary-address');
const $city = $('#secretary-city');
const $state = $('#secretary-state');
const $zipCode = $('#secretary-zip-code');
const $notes = $('#secretary-notes');
const $timezone = $('#secretary-timezone');
const $username = $('#secretary-username');
const $password = $('#secretary-password');
const $passwordConfirmation = $('#secretary-password-confirm');
const $notifications = $('#secretary-notifications');
const $calendarView = $('#secretary-calendar-view');
const $filterSecretaries = $('#filter-secretaries');
let filterResults = {}; let filterResults = {};
let filterLimit = 20; let filterLimit = 20;
/** /**
* Bind the event handlers. * Add the page event listeners.
*/ */
function bindEventHandlers() { function addEventListeners() {
/** /**
* Event: Admin Username "Blur" * Event: Admin Username "Blur"
* *
* When the admin leaves the username input field we will need to check if the username * When the admin leaves the username input field we will need to check if the username
* is not taken by another record in the system. * is not taken by another record in the system.
*
* @param {jQuery.Event} event
*/ */
$secretaries.on('blur', '#secretary-username', function () { $secretaries.on('blur', '#secretary-username', (event) => {
const $input = $(this); const $input = $(event.target);
if ($input.prop('readonly') === true || $input.val() === '') { if ($input.prop('readonly') === true || $input.val() === '') {
return; return;
@ -67,27 +87,20 @@ App.Pages.Secretaries = (function () {
* *
* @param {jQuery.Event} event * @param {jQuery.Event} event
*/ */
$secretaries.on( $secretaries.on('submit', '#filter-secretaries form', (event) => {
'submit',
'#filter-secretaries form',
function (event) {
event.preventDefault(); event.preventDefault();
const key = $('#filter-secretaries .key').val(); const key = $('#filter-secretaries .key').val();
$('#filter-secretaries .selected').removeClass('selected'); $filterSecretaries.find('.selected').removeClass('selected');
resetForm(); resetForm();
filter(key); filter(key);
}.bind(this) });
);
/** /**
* Event: Filter Secretary Row "Click" * Event: Filter Secretary Row "Click"
* *
* Display the selected secretary data to the user. * Display the selected secretary data to the user.
*/ */
$secretaries.on( $secretaries.on('click', '.secretary-row', (event) => {
'click',
'.secretary-row',
function (event) {
if ($('#filter-secretaries .filter').prop('disabled')) { if ($('#filter-secretaries .filter').prop('disabled')) {
$('#filter-secretaries .results').css('color', '#AAA'); $('#filter-secretaries .results').css('color', '#AAA');
return; // exit because we are currently on edit mode return; // exit because we are currently on edit mode
@ -95,48 +108,41 @@ App.Pages.Secretaries = (function () {
const secretaryId = $(event.currentTarget).attr('data-id'); const secretaryId = $(event.currentTarget).attr('data-id');
const secretary = filterResults.find(function (filterResult) { const secretary = filterResults.find((filterResult) => Number(filterResult.id) === Number(secretaryId));
return Number(filterResult.id) === Number(secretaryId);
});
display(secretary); display(secretary);
$('#filter-secretaries .selected').removeClass('selected'); $('#filter-secretaries .selected').removeClass('selected');
$(event.currentTarget).addClass('selected'); $(event.currentTarget).addClass('selected');
$('#edit-secretary, #delete-secretary').prop('disabled', false); $('#edit-secretary, #delete-secretary').prop('disabled', false);
}.bind(this) });
);
/** /**
* Event: Add New Secretary Button "Click" * Event: Add New Secretary Button "Click"
*/ */
$secretaries.on( $secretaries.on('click', '#add-secretary', () => {
'click',
'#add-secretary',
function () {
resetForm(); resetForm();
$('#filter-secretaries button').prop('disabled', true); $filterSecretaries.find('button').prop('disabled', true);
$('#filter-secretaries .results').css('color', '#AAA'); $filterSecretaries.find('.results').css('color', '#AAA');
$('#secretaries .add-edit-delete-group').hide(); $secretaries.find('.add-edit-delete-group').hide();
$('#secretaries .save-cancel-group').show(); $secretaries.find('.save-cancel-group').show();
$('#secretaries .record-details').find('input, textarea').prop('disabled', false); $secretaries.find('.record-details').find('input, textarea').prop('disabled', false);
$('#secretaries .record-details').find('select').prop('disabled', false); $secretaries.find('.record-details').find('select').prop('disabled', false);
$('#secretary-password, #secretary-password-confirm').addClass('required'); $('#secretary-password, #secretary-password-confirm').addClass('required');
$('#secretary-providers input:checkbox').prop('disabled', false); $('#secretary-providers input:checkbox').prop('disabled', false);
}.bind(this) });
);
/** /**
* Event: Edit Secretary Button "Click" * Event: Edit Secretary Button "Click"
*/ */
$secretaries.on('click', '#edit-secretary', function () { $secretaries.on('click', '#edit-secretary', () => {
$('#filter-secretaries button').prop('disabled', true); $filterSecretaries.find('button').prop('disabled', true);
$('#filter-secretaries .results').css('color', '#AAA'); $filterSecretaries.find('.results').css('color', '#AAA');
$('#secretaries .add-edit-delete-group').hide(); $secretaries.find('.add-edit-delete-group').hide();
$('#secretaries .save-cancel-group').show(); $secretaries.find('.save-cancel-group').show();
$('#secretaries .record-details').find('input, textarea').prop('disabled', false); $secretaries.find('.record-details').find('input, textarea').prop('disabled', false);
$('#secretaries .record-details').find('select').prop('disabled', false); $secretaries.find('.record-details').find('select').prop('disabled', false);
$('#secretary-password, #secretary-password-confirm').removeClass('required'); $('#secretary-password, #secretary-password-confirm').removeClass('required');
$('#secretary-providers input:checkbox').prop('disabled', false); $('#secretary-providers input:checkbox').prop('disabled', false);
}); });
@ -144,74 +150,68 @@ App.Pages.Secretaries = (function () {
/** /**
* Event: Delete Secretary Button "Click" * Event: Delete Secretary Button "Click"
*/ */
$secretaries.on( $secretaries.on('click', '#delete-secretary', () => {
'click', const secretaryId = $id.val();
'#delete-secretary',
function () {
const secretaryId = $('#secretary-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(secretaryId); remove(secretaryId);
$('#message-box').dialog('close'); $('#message-box').dialog('close');
}.bind(this) }
} }
]; ];
App.Utils.Message.show(App.Lang.delete_secretary, App.Lang.delete_record_prompt, buttons); App.Utils.Message.show(App.Lang.delete_secretary, App.Lang.delete_record_prompt, buttons);
}.bind(this) });
);
/** /**
* Event: Save Secretary Button "Click" * Event: Save Secretary Button "Click"
*/ */
$secretaries.on( $secretaries.on('click', '#save-secretary', () => {
'click',
'#save-secretary',
function () {
const secretary = { const secretary = {
first_name: $('#secretary-first-name').val(), first_name: $firstName.val(),
last_name: $('#secretary-last-name').val(), last_name: $lastName.val(),
email: $('#secretary-email').val(), email: $email.val(),
mobile_number: $('#secretary-mobile-number').val(), mobile_number: $mobileNumber.val(),
phone_number: $('#secretary-phone-number').val(), phone_number: $phoneNumber.val(),
address: $('#secretary-address').val(), address: $address.val(),
city: $('#secretary-city').val(), city: $city.val(),
state: $('#secretary-state').val(), state: $state.val(),
zip_code: $('#secretary-zip-code').val(), zip_code: $zipCode.val(),
notes: $('#secretary-notes').val(), notes: $notes.val(),
timezone: $('#secretary-timezone').val(), timezone: $timezone.val(),
settings: { settings: {
username: $('#secretary-username').val(), username: $username.val(),
notifications: Number($('#secretary-notifications').prop('checked')), notifications: Number($notifications.prop('checked')),
calendar_view: $('#secretary-calendar-view').val() calendar_view: $calendarView.val()
} }
}; };
// Include secretary services. // Include secretary services.
secretary.providers = []; secretary.providers = [];
$('#secretary-providers input:checkbox').each(function (index, checkbox) { $('#secretary-providers input:checkbox').each((index, checkbox) => {
if ($(checkbox).prop('checked')) { if ($(checkbox).prop('checked')) {
secretary.providers.push($(checkbox).attr('data-id')); secretary.providers.push($(checkbox).attr('data-id'));
} }
}); });
// Include password if changed. // Include password if changed.
if ($('#secretary-password').val() !== '') { if ($password.val() !== '') {
secretary.settings.password = $('#secretary-password').val(); secretary.settings.password = $password.val();
} }
// Include ID if changed. // Include ID if changed.
if ($('#secretary-id').val() !== '') { if ($id.val() !== '') {
secretary.id = $('#secretary-id').val(); secretary.id = $id.val();
} }
if (!validate()) { if (!validate()) {
@ -219,25 +219,20 @@ App.Pages.Secretaries = (function () {
} }
save(secretary); save(secretary);
}.bind(this) });
);
/** /**
* Event: Cancel Secretary Button "Click" * Event: Cancel Secretary Button "Click"
* *
* Cancel add or edit of an secretary record. * Cancel add or edit of an secretary record.
*/ */
$secretaries.on( $secretaries.on('click', '#cancel-secretary', () => {
'click', const id = $id.val();
'#cancel-secretary',
function () {
const id = $('#secretary-id').val();
resetForm(); resetForm();
if (id) { if (id) {
select(id, true); select(id, true);
} }
}.bind(this) });
);
} }
/** /**
@ -275,13 +270,13 @@ App.Pages.Secretaries = (function () {
*/ */
function validate() { function validate() {
$('#secretaries .is-invalid').removeClass('is-invalid'); $('#secretaries .is-invalid').removeClass('is-invalid');
$('#secretaries .form-message').removeClass('alert-danger'); $secretaries.find('.form-message').removeClass('alert-danger');
try { try {
// Validate required fields. // Validate required fields.
let missingRequired = false; let missingRequired = false;
$('#secretaries .required').each(function (index, requiredField) { $secretaries.find('.required').each((index, requiredField) => {
if (!$(requiredField).val()) { if (!$(requiredField).val()) {
$(requiredField).addClass('is-invalid'); $(requiredField).addClass('is-invalid');
missingRequired = true; missingRequired = true;
@ -292,15 +287,12 @@ App.Pages.Secretaries = (function () {
} }
// Validate passwords. // Validate passwords.
if ($('#secretary-password').val() !== $('#secretary-password-confirm').val()) { if ($password.val() !== $passwordConfirmation.val()) {
$('#secretary-password, #secretary-password-confirm').addClass('is-invalid'); $('#secretary-password, #secretary-password-confirm').addClass('is-invalid');
throw new Error('Passwords mismatch!'); throw new Error('Passwords mismatch!');
} }
if ( if ($password.val().length < App.Vars.min_password_length && $password.val() !== '') {
$('#secretary-password').val().length < App.Vars.min_password_length &&
$('#secretary-password').val() !== ''
) {
$('#secretary-password, #secretary-password-confirm').addClass('is-invalid'); $('#secretary-password, #secretary-password-confirm').addClass('is-invalid');
throw new Error( throw new Error(
'Password must be at least ' + BackendSecretaries.MIN_PASSWORD_LENGTH + ' characters long.' 'Password must be at least ' + BackendSecretaries.MIN_PASSWORD_LENGTH + ' characters long.'
@ -308,14 +300,14 @@ App.Pages.Secretaries = (function () {
} }
// Validate user email. // Validate user email.
if (!App.Utils.Validation.email($('#secretary-email').val())) { if (!App.Utils.Validation.email($email.val())) {
$('#secretary-email').addClass('is-invalid'); $email.addClass('is-invalid');
throw new Error('Invalid email address!'); throw new Error('Invalid email address!');
} }
// Check if username exists // Check if username exists
if ($('#secretary-username').attr('already-exists') === 'true') { if ($username.attr('already-exists') === 'true') {
$('#secretary-username').addClass('is-invalid'); $username.addClass('is-invalid');
throw new Error('Username already exists.'); throw new Error('Username already exists.');
} }
@ -330,19 +322,18 @@ App.Pages.Secretaries = (function () {
* Resets the secretary tab form back to its initial state. * Resets the secretary tab form back to its initial state.
*/ */
function resetForm() { function resetForm() {
$('#filter-secretaries .selected').removeClass('selected'); $filterSecretaries.find('.selected').removeClass('selected');
$('#filter-secretaries button').prop('disabled', false); $filterSecretaries.find('button').prop('disabled', false);
$('#filter-secretaries .results').css('color', ''); $filterSecretaries.find('.results').css('color', '');
$secretaries.find('.record-details').find('input, select, textarea').val('').prop('disabled', true);
$('#secretaries .record-details').find('input, select, textarea').val('').prop('disabled', true); $secretaries.find('.record-details #secretary-calendar-view').val('default');
$('#secretaries .record-details #secretary-calendar-view').val('default'); $secretaries.find('.record-details #secretary-timezone').val('UTC');
$('#secretaries .record-details #secretary-timezone').val('UTC'); $secretaries.find('.add-edit-delete-group').show();
$('#secretaries .add-edit-delete-group').show(); $secretaries.find('.save-cancel-group').hide();
$('#secretaries .save-cancel-group').hide(); $secretaries.find('.form-message').hide();
$secretaries.find('.is-invalid').removeClass('is-invalid');
$('#edit-secretary, #delete-secretary').prop('disabled', true); $('#edit-secretary, #delete-secretary').prop('disabled', true);
$('#secretaries .form-message').hide();
$('#secretary-providers input:checkbox').prop('checked', false); $('#secretary-providers input:checkbox').prop('checked', false);
$('#secretaries .is-invalid').removeClass('is-invalid');
} }
/** /**
@ -351,26 +342,26 @@ App.Pages.Secretaries = (function () {
* @param {Object} secretary Contains the secretary record data. * @param {Object} secretary Contains the secretary record data.
*/ */
function display(secretary) { function display(secretary) {
$('#secretary-id').val(secretary.id); $id.val(secretary.id);
$('#secretary-first-name').val(secretary.first_name); $firstName.val(secretary.first_name);
$('#secretary-last-name').val(secretary.last_name); $lastName.val(secretary.last_name);
$('#secretary-email').val(secretary.email); $email.val(secretary.email);
$('#secretary-mobile-number').val(secretary.mobile_number); $mobileNumber.val(secretary.mobile_number);
$('#secretary-phone-number').val(secretary.phone_number); $phoneNumber.val(secretary.phone_number);
$('#secretary-address').val(secretary.address); $address.val(secretary.address);
$('#secretary-city').val(secretary.city); $city.val(secretary.city);
$('#secretary-state').val(secretary.state); $state.val(secretary.state);
$('#secretary-zip-code').val(secretary.zip_code); $zipCode.val(secretary.zip_code);
$('#secretary-notes').val(secretary.notes); $notes.val(secretary.notes);
$('#secretary-timezone').val(secretary.timezone); $timezone.val(secretary.timezone);
$('#secretary-username').val(secretary.settings.username); $username.val(secretary.settings.username);
$('#secretary-calendar-view').val(secretary.settings.calendar_view); $calendarView.val(secretary.settings.calendar_view);
$('#secretary-notifications').prop('checked', Boolean(Number(secretary.settings.notifications))); $notifications.prop('checked', Boolean(Number(secretary.settings.notifications)));
$('#secretary-providers input:checkbox').prop('checked', false); $('#secretary-providers input:checkbox').prop('checked', false);
secretary.providers.forEach(function (secretaryProviderId) { secretary.providers.forEach((secretaryProviderId) => {
const $checkbox = $('#secretary-providers input[data-id="' + secretaryProviderId + '"]'); const $checkbox = $('#secretary-providers input[data-id="' + secretaryProviderId + '"]');
if (!$checkbox.length) { if (!$checkbox.length) {
@ -393,13 +384,11 @@ App.Pages.Secretaries = (function () {
App.Http.Secretaries.search(keyword, filterLimit).done((response) => { App.Http.Secretaries.search(keyword, filterLimit).done((response) => {
filterResults = response; filterResults = response;
$('#filter-secretaries .results').empty(); $filterSecretaries.find('.results').empty();
response.forEach( response.forEach((secretary) => {
function (secretary) { $filterSecretaries.find('.results').append(getFilterHtml(secretary)).append($('<hr/>'));
$('#filter-secretaries .results').append(getFilterHtml(secretary)).append($('<hr/>')); });
}.bind(this)
);
if (!response.length) { if (!response.length) {
$('#filter-secretaries .results').append( $('#filter-secretaries .results').append(
@ -412,10 +401,10 @@ App.Pages.Secretaries = (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-secretaries .results'); }).appendTo('#filter-secretaries .results');
} }
@ -465,16 +454,12 @@ App.Pages.Secretaries = (function () {
* @param {Boolean} show Optional (false), if true the method will display the record in the form. * @param {Boolean} show Optional (false), if true the method will display the record in the form.
*/ */
function select(id, show = false) { function select(id, show = false) {
$('#filter-secretaries .selected').removeClass('selected'); $filterSecretaries.find('.selected').removeClass('selected');
$('#filter-secretaries .secretary-row[data-id="' + id + '"]').addClass('selected'); $('#filter-secretaries .secretary-row[data-id="' + id + '"]').addClass('selected');
if (show) { if (show) {
const secretary = filterResults.find( const secretary = filterResults.find((filterResult) => Number(filterResult.id) === Number(id));
function (filterResult) {
return Number(filterResult.id) === Number(id);
}.bind(this)
);
display(secretary); display(secretary);
@ -488,9 +473,9 @@ App.Pages.Secretaries = (function () {
function initialize() { function initialize() {
resetForm(); resetForm();
filter(''); filter('');
bindEventHandlers(); addEventListeners();
App.Vars.providers.forEach(function (provider) { App.Vars.providers.forEach((provider) => {
$('<div/>', { $('<div/>', {
'class': 'checkbox', 'class': 'checkbox',
'html': [ 'html': [
@ -516,5 +501,12 @@ App.Pages.Secretaries = (function () {
document.addEventListener('DOMContentLoaded', initialize); document.addEventListener('DOMContentLoaded', initialize);
return {}; return {
filter,
save,
remove,
getFilterHtml,
resetForm,
select
};
})(); })();