forked from mirrors/easyappointments
Code refactoring and improvements for the admins page.
This commit is contained in:
parent
22439eb60a
commit
ea6b13f1d2
1 changed files with 82 additions and 62 deletions
|
@ -16,13 +16,31 @@
|
||||||
*/
|
*/
|
||||||
App.Pages.Admins = (function () {
|
App.Pages.Admins = (function () {
|
||||||
const $admins = $('#admins');
|
const $admins = $('#admins');
|
||||||
|
const $adminId = $('#admin-id');
|
||||||
|
const $firstName = $('#admin-first-name');
|
||||||
|
const $lastName = $('#admin-last-name');
|
||||||
|
const $email = $('#admin-email');
|
||||||
|
const $mobileNumber = $('#admin-mobile-number');
|
||||||
|
const $phoneNumber = $('#admin-phone-number');
|
||||||
|
const $address = $('#admin-address');
|
||||||
|
const $city = $('#admin-city');
|
||||||
|
const $state = $('#admin-state');
|
||||||
|
const $zipCode = $('#admin-zip-code');
|
||||||
|
const $notes = $('#admin-notes');
|
||||||
|
const $timezone = $('#admin-timezone');
|
||||||
|
const $username = $('#admin-username');
|
||||||
|
const $password = $('#admin-password');
|
||||||
|
const $passwordConfirmation = $('#admin-password-confirm');
|
||||||
|
const $notifications = $('#admin-notifications');
|
||||||
|
const $calendarView = $('#admin-calendar-view');
|
||||||
|
const $filterAdmins = $('#filter-admins');
|
||||||
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"
|
||||||
*
|
*
|
||||||
|
@ -103,10 +121,10 @@ App.Pages.Admins = (function () {
|
||||||
*/
|
*/
|
||||||
$admins.on('click', '#add-admin', function () {
|
$admins.on('click', '#add-admin', function () {
|
||||||
resetForm();
|
resetForm();
|
||||||
$('#admins .add-edit-delete-group').hide();
|
$admins.find('.add-edit-delete-group').hide();
|
||||||
$('#admins .save-cancel-group').show();
|
$admins.find('.save-cancel-group').show();
|
||||||
$('#admins .record-details').find('input, textarea').prop('disabled', false);
|
$admins.find('.record-details').find('input, textarea').prop('disabled', false);
|
||||||
$('#admins .record-details').find('select').prop('disabled', false);
|
$admins.find('.record-details').find('select').prop('disabled', false);
|
||||||
$('#admin-password, #admin-password-confirm').addClass('required');
|
$('#admin-password, #admin-password-confirm').addClass('required');
|
||||||
$('#filter-admins button').prop('disabled', true);
|
$('#filter-admins button').prop('disabled', true);
|
||||||
$('#filter-admins .results').css('color', '#AAA');
|
$('#filter-admins .results').css('color', '#AAA');
|
||||||
|
@ -116,10 +134,10 @@ App.Pages.Admins = (function () {
|
||||||
* Event: Edit Admin Button "Click"
|
* Event: Edit Admin Button "Click"
|
||||||
*/
|
*/
|
||||||
$admins.on('click', '#edit-admin', function () {
|
$admins.on('click', '#edit-admin', function () {
|
||||||
$('#admins .add-edit-delete-group').hide();
|
$admins.find('.add-edit-delete-group').hide();
|
||||||
$('#admins .save-cancel-group').show();
|
$admins.find('.save-cancel-group').show();
|
||||||
$('#admins .record-details').find('input, textarea').prop('disabled', false);
|
$admins.find('.record-details').find('input, textarea').prop('disabled', false);
|
||||||
$('#admins .record-details').find('select').prop('disabled', false);
|
$admins.find('.record-details').find('select').prop('disabled', false);
|
||||||
$('#admin-password, #admin-password-confirm').removeClass('required');
|
$('#admin-password, #admin-password-confirm').removeClass('required');
|
||||||
$('#filter-admins button').prop('disabled', true);
|
$('#filter-admins button').prop('disabled', true);
|
||||||
$('#filter-admins .results').css('color', '#AAA');
|
$('#filter-admins .results').css('color', '#AAA');
|
||||||
|
@ -155,31 +173,31 @@ App.Pages.Admins = (function () {
|
||||||
*/
|
*/
|
||||||
$admins.on('click', '#save-admin', function () {
|
$admins.on('click', '#save-admin', function () {
|
||||||
const admin = {
|
const admin = {
|
||||||
first_name: $('#admin-first-name').val(),
|
first_name: $firstName.val(),
|
||||||
last_name: $('#admin-last-name').val(),
|
last_name: $lastName.val(),
|
||||||
email: $('#admin-email').val(),
|
email: $email.val(),
|
||||||
mobile_number: $('#admin-mobile-number').val(),
|
mobile_number: $mobileNumber.val(),
|
||||||
phone_number: $('#admin-phone-number').val(),
|
phone_number: $phoneNumber.val(),
|
||||||
address: $('#admin-address').val(),
|
address: $address.val(),
|
||||||
city: $('#admin-city').val(),
|
city: $city.val(),
|
||||||
state: $('#admin-state').val(),
|
state: $state.val(),
|
||||||
zip_code: $('#admin-zip-code').val(),
|
zip_code: $zipCode.val(),
|
||||||
notes: $('#admin-notes').val(),
|
notes: $notes.val(),
|
||||||
timezone: $('#admin-timezone').val(),
|
timezone: $timezone.val(),
|
||||||
settings: {
|
settings: {
|
||||||
username: $('#admin-username').val(),
|
username: $username.val(),
|
||||||
notifications: Number($('#admin-notifications').prop('checked')),
|
notifications: Number($notifications.prop('checked')),
|
||||||
calendar_view: $('#admin-calendar-view').val()
|
calendar_view: $calendarView.val()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Include password if changed.
|
// Include password if changed.
|
||||||
if ($('#admin-password').val() !== '') {
|
if ($password.val() !== '') {
|
||||||
admin.settings.password = $('#admin-password').val();
|
admin.settings.password = $password.val();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include id if changed.
|
// Include id if changed.
|
||||||
if ($('#admin-id').val() !== '') {
|
if ($adminId.val() !== '') {
|
||||||
admin.id = $('#admin-id').val();
|
admin.id = $('#admin-id').val();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,8 +214,10 @@ App.Pages.Admins = (function () {
|
||||||
* Cancel add or edit of an admin record.
|
* Cancel add or edit of an admin record.
|
||||||
*/
|
*/
|
||||||
$admins.on('click', '#cancel-admin', function () {
|
$admins.on('click', '#cancel-admin', function () {
|
||||||
const id = $('#admin-id').val();
|
const id = $adminId.val();
|
||||||
|
|
||||||
resetForm();
|
resetForm();
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
select(id, true);
|
select(id, true);
|
||||||
}
|
}
|
||||||
|
@ -238,13 +258,13 @@ App.Pages.Admins = (function () {
|
||||||
* @return {Boolean} Returns the validation result.
|
* @return {Boolean} Returns the validation result.
|
||||||
*/
|
*/
|
||||||
function validate() {
|
function validate() {
|
||||||
$('#admins .is-invalid').removeClass('is-invalid');
|
$admins.find('.is-invalid').removeClass('is-invalid');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Validate required fields.
|
// Validate required fields.
|
||||||
let missingRequired = false;
|
let missingRequired = false;
|
||||||
|
|
||||||
$('#admins .required').each(function (index, requiredField) {
|
$admins.find('.required').each(function (index, requiredField) {
|
||||||
if (!$(requiredField).val()) {
|
if (!$(requiredField).val()) {
|
||||||
$(requiredField).addClass('is-invalid');
|
$(requiredField).addClass('is-invalid');
|
||||||
missingRequired = true;
|
missingRequired = true;
|
||||||
|
@ -256,31 +276,31 @@ App.Pages.Admins = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate passwords.
|
// Validate passwords.
|
||||||
if ($('#admin-password').val() !== $('#admin-password-confirm').val()) {
|
if ($password.val() !== $passwordConfirmation.val()) {
|
||||||
$('#admin-password, #admin-password-confirm').addClass('is-invalid');
|
$('#admin-password, #admin-password-confirm').addClass('is-invalid');
|
||||||
throw new Error(App.Lang.passwords_mismatch);
|
throw new Error(App.Lang.passwords_mismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($('#admin-password').val().length < App.Vars.min_password_length && $('#admin-password').val() !== '') {
|
if ($password.val().length < App.Vars.min_password_length && $password.val() !== '') {
|
||||||
$('#admin-password, #admin-password-confirm').addClass('is-invalid');
|
$('#admin-password, #admin-password-confirm').addClass('is-invalid');
|
||||||
throw new Error(App.Lang.password_length_notice.replace('$number', BackendAdmins.MIN_PASSWORD_LENGTH));
|
throw new Error(App.Lang.password_length_notice.replace('$number', BackendAdmins.MIN_PASSWORD_LENGTH));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate user email.
|
// Validate user email.
|
||||||
if (!App.Utils.Validation.email($('#admin-email').val())) {
|
if (!App.Utils.Validation.email($email.val())) {
|
||||||
$('#admin-email').addClass('is-invalid');
|
$email.addClass('is-invalid');
|
||||||
throw new Error(App.Lang.invalid_email);
|
throw new Error(App.Lang.invalid_email);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if username exists
|
// Check if username exists
|
||||||
if ($('#admin-username').attr('already-exists') === 'true') {
|
if ($username.attr('already-exists') === 'true') {
|
||||||
$('#admin-username').addClass('is-invalid');
|
$username.addClass('is-invalid');
|
||||||
throw new Error(App.Lang.username_already_exists);
|
throw new Error(App.Lang.username_already_exists);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
$('#admins .form-message').addClass('alert-danger').text(error.message).show();
|
$admins.find('.form-message').addClass('alert-danger').text(error.message).show();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -293,11 +313,11 @@ App.Pages.Admins = (function () {
|
||||||
$('#filter-admins button').prop('disabled', false);
|
$('#filter-admins button').prop('disabled', false);
|
||||||
$('#filter-admins .results').css('color', '');
|
$('#filter-admins .results').css('color', '');
|
||||||
|
|
||||||
$('#admins .add-edit-delete-group').show();
|
$admins.find('.add-edit-delete-group').show();
|
||||||
$('#admins .save-cancel-group').hide();
|
$admins.find('.save-cancel-group').hide();
|
||||||
$('#admins .record-details').find('input, select, textarea').val('').prop('disabled', true);
|
$admins.find('.record-details').find('input, select, textarea').val('').prop('disabled', true);
|
||||||
$('#admins .record-details #admin-calendar-view').val('default');
|
$admins.find('.record-details #admin-calendar-view').val('default');
|
||||||
$('#admins .record-details #admin-timezone').val('UTC');
|
$admins.find('.record-details #admin-timezone').val('UTC');
|
||||||
$('#edit-admin, #delete-admin').prop('disabled', true);
|
$('#edit-admin, #delete-admin').prop('disabled', true);
|
||||||
|
|
||||||
$('#admins .is-invalid').removeClass('is-invalid');
|
$('#admins .is-invalid').removeClass('is-invalid');
|
||||||
|
@ -310,22 +330,22 @@ App.Pages.Admins = (function () {
|
||||||
* @param {Object} admin Contains the admin record data.
|
* @param {Object} admin Contains the admin record data.
|
||||||
*/
|
*/
|
||||||
function display(admin) {
|
function display(admin) {
|
||||||
$('#admin-id').val(admin.id);
|
$adminId.val(admin.id);
|
||||||
$('#admin-first-name').val(admin.first_name);
|
$firstName.val(admin.first_name);
|
||||||
$('#admin-last-name').val(admin.last_name);
|
$lastName.val(admin.last_name);
|
||||||
$('#admin-email').val(admin.email);
|
$email.val(admin.email);
|
||||||
$('#admin-mobile-number').val(admin.mobile_number);
|
$mobileNumber.val(admin.mobile_number);
|
||||||
$('#admin-phone-number').val(admin.phone_number);
|
$phoneNumber.val(admin.phone_number);
|
||||||
$('#admin-address').val(admin.address);
|
$address.val(admin.address);
|
||||||
$('#admin-city').val(admin.city);
|
$city.val(admin.city);
|
||||||
$('#admin-state').val(admin.state);
|
$state.val(admin.state);
|
||||||
$('#admin-zip-code').val(admin.zip_code);
|
$zipCode.val(admin.zip_code);
|
||||||
$('#admin-notes').val(admin.notes);
|
$notes.val(admin.notes);
|
||||||
$('#admin-timezone').val(admin.timezone);
|
$timezone.val(admin.timezone);
|
||||||
|
|
||||||
$('#admin-username').val(admin.settings.username);
|
$username.val(admin.settings.username);
|
||||||
$('#admin-calendar-view').val(admin.settings.calendar_view);
|
$calendarView.val(admin.settings.calendar_view);
|
||||||
$('#admin-notifications').prop('checked', Boolean(Number(admin.settings.notifications)));
|
$notifications.prop('checked', Boolean(Number(admin.settings.notifications)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -341,14 +361,14 @@ App.Pages.Admins = (function () {
|
||||||
App.Http.Admins.search(keyword, filterLimit).then((response) => {
|
App.Http.Admins.search(keyword, filterLimit).then((response) => {
|
||||||
filterResults = response;
|
filterResults = response;
|
||||||
|
|
||||||
$('#filter-admins .results').empty();
|
$filterAdmins.find('.results').empty();
|
||||||
|
|
||||||
response.forEach(function (admin) {
|
response.forEach(function (admin) {
|
||||||
$('#filter-admins .results').append(getFilterHtml(admin)).append($('<hr/>'));
|
$('#filter-admins .results').append(getFilterHtml(admin)).append($('<hr/>'));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.length) {
|
if (!response.length) {
|
||||||
$('#filter-admins .results').append(
|
$filterAdmins.find('.results').append(
|
||||||
$('<em/>', {
|
$('<em/>', {
|
||||||
'text': App.Lang.no_records_found
|
'text': App.Lang.no_records_found
|
||||||
})
|
})
|
||||||
|
@ -412,9 +432,9 @@ App.Pages.Admins = (function () {
|
||||||
* on the form.
|
* on the form.
|
||||||
*/
|
*/
|
||||||
function select(id, show = false) {
|
function select(id, show = false) {
|
||||||
$('#filter-admins .selected').removeClass('selected');
|
$filterAdmins.find('.selected').removeClass('selected');
|
||||||
|
|
||||||
$('#filter-admins .admin-row[data-id="' + id + '"]').addClass('selected');
|
$filterAdmins.find('.admin-row[data-id="' + id + '"]').addClass('selected');
|
||||||
|
|
||||||
if (show) {
|
if (show) {
|
||||||
const admin = filterResults.find(function (filterResult) {
|
const admin = filterResults.find(function (filterResult) {
|
||||||
|
@ -433,7 +453,7 @@ App.Pages.Admins = (function () {
|
||||||
function initialize() {
|
function initialize() {
|
||||||
resetForm();
|
resetForm();
|
||||||
filter('');
|
filter('');
|
||||||
bindEventHandlers();
|
addEventListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', initialize);
|
document.addEventListener('DOMContentLoaded', initialize);
|
||||||
|
|
Loading…
Reference in a new issue