mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-25 01:12:21 +03:00
107 lines
2.8 KiB
JavaScript
107 lines
2.8 KiB
JavaScript
/* ----------------------------------------------------------------------------
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
*
|
|
* @package EasyAppointments
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
* @link https://easyappointments.org
|
|
* @since v1.5.0
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
* API settings page.
|
|
*
|
|
* This module implements the functionality of the API settings page.
|
|
*/
|
|
App.Pages.ApiSettings = (function () {
|
|
const $saveSettings = $('#save-settings');
|
|
|
|
/**
|
|
* Check if the form has invalid values.
|
|
*
|
|
* @return {Boolean}
|
|
*/
|
|
function isInvalid() {
|
|
try {
|
|
$('#api-settings .is-invalid').removeClass('is-invalid');
|
|
|
|
// Validate required fields.
|
|
|
|
let missingRequiredFields = false;
|
|
|
|
$('#api-settings .required').each((index, requiredField) => {
|
|
const $requiredField = $(requiredField);
|
|
|
|
if (!$requiredField.val()) {
|
|
$requiredField.addClass('is-invalid');
|
|
missingRequiredFields = true;
|
|
}
|
|
});
|
|
|
|
if (missingRequiredFields) {
|
|
throw new Error(lang('fields_are_required'));
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
App.Layouts.Backend.displayNotification(error.message);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function deserialize(apiSettings) {
|
|
apiSettings.forEach((apiSetting) => {
|
|
$('[data-field="' + apiSetting.name + '"]').val(apiSetting.value);
|
|
});
|
|
}
|
|
|
|
function serialize() {
|
|
const apiSettings = [];
|
|
|
|
$('[data-field]').each((index, field) => {
|
|
const $field = $(field);
|
|
|
|
apiSettings.push({
|
|
name: $field.data('field'),
|
|
value: $field.val()
|
|
});
|
|
});
|
|
|
|
return apiSettings;
|
|
}
|
|
|
|
/**
|
|
* Save the account information.
|
|
*/
|
|
function onSaveSettingsClick() {
|
|
if (isInvalid()) {
|
|
App.Layouts.Backend.displayNotification(lang('settings_are_invalid'));
|
|
|
|
return;
|
|
}
|
|
|
|
const apiSettings = serialize();
|
|
|
|
App.Http.ApiSettings.save(apiSettings).done(() => {
|
|
App.Layouts.Backend.displayNotification(lang('settings_saved'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Initialize the module.
|
|
*/
|
|
function initialize() {
|
|
$saveSettings.on('click', onSaveSettingsClick);
|
|
|
|
const apiSettings = vars('api_settings');
|
|
|
|
deserialize(apiSettings);
|
|
|
|
App.Layouts.Backend.placeFooterToBottom();
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
|
|
|
return {};
|
|
})();
|