MaketRandevu/assets/js/pages/account.js

198 lines
5.9 KiB
JavaScript
Raw Normal View History

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web 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.0.0
* ---------------------------------------------------------------------------- */
/**
* Account
*
* Contains the functionality of the account page.
*/
App.Pages.Account = (function () {
const $userId = $('#user-id');
const $firstName = $('#first-name');
const $lastName = $('#last-name');
const $email = $('#email');
const $mobileNumber = $('#mobile-number');
const $phoneNumber = $('#phone-number');
const $address = $('#address');
const $city = $('#city');
const $state = $('#state');
const $zipCode = $('#zip-code');
const $notes = $('#notes');
const $timezones = $('#timezone');
const $username = $('#username');
const $password = $('#password');
const $retypePassword = $('#retype-password');
const $calendarView = $('#calendar-view');
const notifications = $('#notifications');
const $saveSettings = $('#save-settings');
2021-12-18 20:56:08 +03:00
const $footerUserDisplayName = $('#footer-user-display-name');
2021-12-18 20:56:08 +03:00
/**
* Check if the form has invalid values.
*
* @return {Boolean}
*/
function isInvalid() {
try {
$('#account .is-invalid').removeClass('is-invalid');
// Validate required fields.
let missingRequiredFields = false;
$('#account .required').each((index, requiredField) => {
2021-12-18 20:56:08 +03:00
const $requiredField = $(requiredField);
if (!$requiredField.val()) {
$requiredField.addClass('is-invalid');
missingRequiredFields = true;
}
});
if (missingRequiredFields) {
throw new Error(App.Lang.fields_are_required);
}
// Validate passwords (if values provided).
if ($password.val() && $password.val() !== $retypePassword.val()) {
$password.addClass('is-invalid');
$retypePassword.addClass('is-invalid');
throw new Error(App.Lang.passwords_mismatch);
}
// Validate user email.
const emailValue = $email.val();
if (!App.Utils.Validation.email(emailValue)) {
$email.addClass('is-invalid');
throw new Error(App.Lang.invalid_email);
}
2021-12-18 21:02:35 +03:00
if ($username.hasClass('is-invalid')) {
2021-12-18 20:56:08 +03:00
throw new Error(App.Lang.username_already_exists);
}
return false;
} catch (error) {
Backend.displayNotification(error.message);
return true;
}
}
/**
* Apply the account values to the form.
*
* @param {Object} account
*/
function deserialize(account) {
$userId.val(account.id);
$firstName.val(account.first_name);
$lastName.val(account.last_name);
$email.val(account.email);
$mobileNumber.val(account.mobile_number);
$phoneNumber.val(account.phone_number);
$address.val(account.address);
$city.val(account.city);
$state.val(account.state);
$zipCode.val(account.zip_code);
$notes.val(account.notes);
$timezones.val(account.timezone);
$username.val(account.settings.username);
$password.val('');
$retypePassword.val('');
$calendarView.val(account.settings.calendar_view);
notifications.prop('checked', Boolean(Number(account.settings.notifications)));
}
2021-12-18 20:56:08 +03:00
/**
* Get the account information from the form.
*
* @return {Object}
*/
function serialize() {
return {
id: $userId.val(),
first_name: $firstName.val(),
last_name: $lastName.val(),
email: $email.val(),
mobile_number: $mobileNumber.val(),
phone_number: $phoneNumber.val(),
address: $address.val(),
city: $city.val(),
state: $state.val(),
zip_code: $zipCode.val(),
notes: $notes.val(),
timezone: $timezones.val(),
settings: {
username: $username.val(),
2021-12-18 20:56:08 +03:00
password: $password.val() || null,
calendar_view: $calendarView.val(),
notifications: Number(notifications.prop('checked'))
}
};
}
2021-12-18 20:56:08 +03:00
/**
* Save the account information.
*/
function onSaveSettingsClick() {
2021-12-18 20:56:08 +03:00
if (isInvalid()) {
Backend.displayNotification(App.Lang.user_settings_are_invalid);
return;
}
const account = serialize();
App.Http.Account.save(account).done(() => {
2021-12-18 20:56:08 +03:00
Backend.displayNotification(App.Lang.settings_saved);
$footerUserDisplayName.text('Hello, ' + $firstName.val() + ' ' + $lastName.val() + '!');
});
}
2021-12-18 20:56:08 +03:00
/**
* Make sure the username is unique.
*/
function onUsernameChange() {
const username = $username.val();
App.Http.Account.validateUsername(username).done((response) => {
2021-12-18 21:02:35 +03:00
const isValid = response.is_valid;
$username.toggleClass('is-invalid', !isValid);
if (!isValid) {
Backend.displayNotification(App.Lang.username_already_exists);
2021-12-18 20:56:08 +03:00
}
});
}
/**
* Initialize the page.
*/
function init() {
const account = App.Vars.account;
deserialize(account);
$saveSettings.on('click', onSaveSettingsClick);
2021-12-18 20:56:08 +03:00
$username.on('change', onUsernameChange);
Backend.placeFooterToBottom();
}
document.addEventListener('DOMContentLoaded', init);
return {};
})();