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,76 +87,62 @@ App.Pages.Secretaries = (function () {
* *
* @param {jQuery.Event} event * @param {jQuery.Event} event
*/ */
$secretaries.on( $secretaries.on('submit', '#filter-secretaries form', (event) => {
'submit', event.preventDefault();
'#filter-secretaries form', const key = $('#filter-secretaries .key').val();
function (event) { $filterSecretaries.find('.selected').removeClass('selected');
event.preventDefault(); resetForm();
const key = $('#filter-secretaries .key').val(); filter(key);
$('#filter-secretaries .selected').removeClass('selected'); });
resetForm();
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', if ($('#filter-secretaries .filter').prop('disabled')) {
'.secretary-row', $('#filter-secretaries .results').css('color', '#AAA');
function (event) { return; // exit because we are currently on edit mode
if ($('#filter-secretaries .filter').prop('disabled')) { }
$('#filter-secretaries .results').css('color', '#AAA');
return; // exit because we are currently on edit mode
}
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', resetForm();
'#add-secretary', $filterSecretaries.find('button').prop('disabled', true);
function () { $filterSecretaries.find('.results').css('color', '#AAA');
resetForm();
$('#filter-secretaries button').prop('disabled', true);
$('#filter-secretaries .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,100 +150,89 @@ 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 = [
{
text: App.Lang.cancel,
click: function () {
$('#message-box').dialog('close');
}
},
{
text: App.Lang.delete,
click: function () {
remove(secretaryId);
$('#message-box').dialog('close');
}.bind(this)
}
];
App.Utils.Message.show(App.Lang.delete_secretary, App.Lang.delete_record_prompt, buttons); const buttons = [
}.bind(this) {
); text: App.Lang.cancel,
click: () => {
$('#message-box').dialog('close');
}
},
{
text: App.Lang.delete,
click: () => {
remove(secretaryId);
$('#message-box').dialog('close');
}
}
];
App.Utils.Message.show(App.Lang.delete_secretary, App.Lang.delete_record_prompt, buttons);
});
/** /**
* Event: Save Secretary Button "Click" * Event: Save Secretary Button "Click"
*/ */
$secretaries.on( $secretaries.on('click', '#save-secretary', () => {
'click', const secretary = {
'#save-secretary', first_name: $firstName.val(),
function () { last_name: $lastName.val(),
const secretary = { email: $email.val(),
first_name: $('#secretary-first-name').val(), mobile_number: $mobileNumber.val(),
last_name: $('#secretary-last-name').val(), phone_number: $phoneNumber.val(),
email: $('#secretary-email').val(), address: $address.val(),
mobile_number: $('#secretary-mobile-number').val(), city: $city.val(),
phone_number: $('#secretary-phone-number').val(), state: $state.val(),
address: $('#secretary-address').val(), zip_code: $zipCode.val(),
city: $('#secretary-city').val(), notes: $notes.val(),
state: $('#secretary-state').val(), timezone: $timezone.val(),
zip_code: $('#secretary-zip-code').val(), settings: {
notes: $('#secretary-notes').val(), username: $username.val(),
timezone: $('#secretary-timezone').val(), notifications: Number($notifications.prop('checked')),
settings: { calendar_view: $calendarView.val()
username: $('#secretary-username').val(),
notifications: Number($('#secretary-notifications').prop('checked')),
calendar_view: $('#secretary-calendar-view').val()
}
};
// Include secretary services.
secretary.providers = [];
$('#secretary-providers input:checkbox').each(function (index, checkbox) {
if ($(checkbox).prop('checked')) {
secretary.providers.push($(checkbox).attr('data-id'));
}
});
// Include password if changed.
if ($('#secretary-password').val() !== '') {
secretary.settings.password = $('#secretary-password').val();
} }
};
// Include ID if changed. // Include secretary services.
if ($('#secretary-id').val() !== '') { secretary.providers = [];
secretary.id = $('#secretary-id').val();
$('#secretary-providers input:checkbox').each((index, checkbox) => {
if ($(checkbox).prop('checked')) {
secretary.providers.push($(checkbox).attr('data-id'));
} }
});
if (!validate()) { // Include password if changed.
return; if ($password.val() !== '') {
} secretary.settings.password = $password.val();
}
save(secretary); // Include ID if changed.
}.bind(this) if ($id.val() !== '') {
); secretary.id = $id.val();
}
if (!validate()) {
return;
}
save(secretary);
});
/** /**
* 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', resetForm();
function () { if (id) {
const id = $('#secretary-id').val(); select(id, true);
resetForm(); }
if (id) { });
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
};
})(); })();