2021-12-18 20:25:03 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-12-18 20:25:03 +03:00
|
|
|
*
|
|
|
|
* @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
|
2022-01-18 15:01:22 +03:00
|
|
|
* @since v1.5.0
|
2021-12-18 20:25:03 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2022-01-14 11:26:44 +03:00
|
|
|
* Account page.
|
2021-12-18 20:25:03 +03:00
|
|
|
*
|
2022-01-14 11:26:44 +03:00
|
|
|
* This module implements the functionality of the account page.
|
2021-12-18 20:25:03 +03:00
|
|
|
*/
|
|
|
|
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');
|
2022-05-20 19:13:26 +03:00
|
|
|
const $language = $('#language');
|
|
|
|
const $timezone = $('#timezone');
|
2021-12-18 20:25:03 +03:00
|
|
|
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:25:03 +03:00
|
|
|
|
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;
|
|
|
|
|
2021-12-18 23:14:10 +03:00
|
|
|
$('#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) {
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('fields_are_required'));
|
2021-12-18 20:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate passwords (if values provided).
|
|
|
|
|
|
|
|
if ($password.val() && $password.val() !== $retypePassword.val()) {
|
|
|
|
$password.addClass('is-invalid');
|
|
|
|
$retypePassword.addClass('is-invalid');
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('passwords_mismatch'));
|
2021-12-18 20:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate user email.
|
|
|
|
|
|
|
|
const emailValue = $email.val();
|
|
|
|
|
|
|
|
if (!App.Utils.Validation.email(emailValue)) {
|
|
|
|
$email.addClass('is-invalid');
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('invalid_email'));
|
2021-12-18 20:56:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-18 21:02:35 +03:00
|
|
|
if ($username.hasClass('is-invalid')) {
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('username_already_exists'));
|
2021-12-18 20:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (error) {
|
2022-01-17 07:26:57 +03:00
|
|
|
App.Layouts.Backend.displayNotification(error.message);
|
2021-12-18 20:56:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the account values to the form.
|
|
|
|
*
|
|
|
|
* @param {Object} account
|
|
|
|
*/
|
2021-12-18 20:25:03 +03:00
|
|
|
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);
|
2022-05-20 19:13:26 +03:00
|
|
|
$language.val(account.language);
|
|
|
|
$timezone.val(account.timezone);
|
2021-12-18 20:25:03 +03:00
|
|
|
$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}
|
|
|
|
*/
|
2021-12-18 20:25:03 +03:00
|
|
|
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(),
|
2022-05-20 19:13:26 +03:00
|
|
|
language: $language.val(),
|
|
|
|
timezone: $timezone.val(),
|
2021-12-18 20:25:03 +03:00
|
|
|
settings: {
|
|
|
|
username: $username.val(),
|
2022-05-20 19:14:45 +03:00
|
|
|
password: $password.val() || undefined,
|
2021-12-18 20:25:03 +03:00
|
|
|
calendar_view: $calendarView.val(),
|
2023-12-22 13:35:41 +03:00
|
|
|
notifications: Number(notifications.prop('checked')),
|
|
|
|
},
|
2021-12-18 20:25:03 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-18 20:56:08 +03:00
|
|
|
/**
|
|
|
|
* Save the account information.
|
|
|
|
*/
|
2021-12-18 20:25:03 +03:00
|
|
|
function onSaveSettingsClick() {
|
2021-12-18 20:56:08 +03:00
|
|
|
if (isInvalid()) {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('user_settings_are_invalid'));
|
2021-12-18 20:56:08 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-18 20:25:03 +03:00
|
|
|
const account = serialize();
|
|
|
|
|
2021-12-18 23:14:10 +03:00
|
|
|
App.Http.Account.save(account).done(() => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('settings_saved'));
|
2021-12-18 20:56:08 +03:00
|
|
|
|
|
|
|
$footerUserDisplayName.text('Hello, ' + $firstName.val() + ' ' + $lastName.val() + '!');
|
2021-12-18 20:25:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-18 20:56:08 +03:00
|
|
|
/**
|
|
|
|
* Make sure the username is unique.
|
|
|
|
*/
|
|
|
|
function onUsernameChange() {
|
|
|
|
const username = $username.val();
|
|
|
|
|
2022-10-03 17:01:21 +03:00
|
|
|
App.Http.Account.validateUsername(vars('user_id'), username).done((response) => {
|
2021-12-18 21:02:35 +03:00
|
|
|
const isValid = response.is_valid;
|
|
|
|
$username.toggleClass('is-invalid', !isValid);
|
|
|
|
if (!isValid) {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('username_already_exists'));
|
2021-12-18 20:56:08 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the page.
|
|
|
|
*/
|
2022-01-14 10:31:12 +03:00
|
|
|
function initialize() {
|
2022-01-18 10:18:22 +03:00
|
|
|
const account = vars('account');
|
2021-12-18 20:25:03 +03:00
|
|
|
|
|
|
|
deserialize(account);
|
|
|
|
|
|
|
|
$saveSettings.on('click', onSaveSettingsClick);
|
|
|
|
|
2021-12-18 20:56:08 +03:00
|
|
|
$username.on('change', onUsernameChange);
|
2021-12-18 20:25:03 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 10:31:12 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
2021-12-18 20:25:03 +03:00
|
|
|
|
|
|
|
return {};
|
|
|
|
})();
|