account settings: Replace logic with existing functions.

Replace settings api calls with existing function
`settings_ui.do_settings_change()`. This commit also
adds a ui element for save alert notificaiton.
This commit is contained in:
Yashashvi Dave 2019-07-10 22:47:26 +05:30 committed by Tim Abbott
parent e2e7d288a5
commit ff75c77f7a
3 changed files with 42 additions and 54 deletions

View File

@ -62,10 +62,6 @@ function settings_change_error(message, xhr) {
ui_report.error(message, xhr, $('#account-settings-status').expectOne()); ui_report.error(message, xhr, $('#account-settings-status').expectOne());
} }
function settings_change_success(message) {
ui_report.success(message, $('#account-settings-status').expectOne());
}
function update_user_custom_profile_fields(fields, method) { function update_user_custom_profile_fields(fields, method) {
if (method === undefined) { if (method === undefined) {
blueslip.error("Undefined method in update_user_custom_profile_fields"); blueslip.error("Undefined method in update_user_custom_profile_fields");
@ -331,7 +327,7 @@ exports.set_up = function () {
$('#change_password_button').on('click', function (e) { $('#change_password_button').on('click', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
var change_password_info = $('#change_password_modal').find(".change_password_info").expectOne(); var change_password_error = $('#change_password_modal').find(".change_password_info").expectOne();
var data = { var data = {
old_password: $('#old_password').val(), old_password: $('#old_password').val(),
@ -355,19 +351,15 @@ exports.set_up = function () {
} }
} }
channel.patch({ var opts = {
url: "/json/settings", success_continuation: function () {
data: data, overlays.close_modal("change_password_modal");
success: function () {
settings_change_success(i18n.t("Updated settings!"));
overlays.close_modal('change_password_modal');
clear_password_change();
}, },
error: function (xhr) { error_msg_element: change_password_error,
ui_report.error(i18n.t("Failed"), xhr, change_password_info); };
settings_ui.do_settings_change(channel.patch, '/json/settings', data,
$('#account-settings-status').expectOne(), opts);
clear_password_change(); clear_password_change();
},
});
}); });
$('#new_password').on('input', function () { $('#new_password').on('input', function () {
@ -378,54 +370,40 @@ exports.set_up = function () {
$("#change_full_name_button").on('click', function (e) { $("#change_full_name_button").on('click', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
var change_full_name_info = $('#change_full_name_modal').find(".change_full_name_info").expectOne(); var change_full_name_error = $('#change_full_name_modal').find(".change_full_name_info").expectOne();
var data = {}; var data = {};
data.full_name = $('.full_name_change_container').find("input[name='full_name']").val(); data.full_name = $('.full_name_change_container').find("input[name='full_name']").val();
channel.patch({
url: '/json/settings', var opts = {
data: data, success_continuation: function () {
success: function (data) { overlays.close_modal("change_full_name_modal");
if ('full_name' in data) {
settings_change_success(i18n.t("Updated settings!"));
} else {
settings_change_success(i18n.t("No changes made."));
}
overlays.close_modal('change_full_name_modal');
}, },
error: function (xhr) { error_msg_element: change_full_name_error,
ui_report.error(i18n.t("Failed"), xhr, change_full_name_info); };
}, settings_ui.do_settings_change(channel.patch, '/json/settings', data,
}); $('#account-settings-status').expectOne(), opts);
}); });
$('#change_email_button').on('click', function (e) { $('#change_email_button').on('click', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
var change_email_info = $('#change_email_modal').find(".change_email_info").expectOne(); var change_email_error = $('#change_email_modal').find(".change_email_info").expectOne();
var data = {}; var data = {};
data.email = $('.email_change_container').find("input[name='email']").val(); data.email = $('.email_change_container').find("input[name='email']").val();
channel.patch({ var opts = {
url: '/json/settings', success_continuation: function () {
data: data,
success: function (data) {
if ('account_email' in data) {
settings_change_success(data.account_email);
if (page_params.development_environment) { if (page_params.development_environment) {
var email_msg = templates.render('settings/dev_env_email_access'); var email_msg = templates.render('settings/dev_env_email_access');
$("#account-settings-status").append(email_msg); ui_report.success(email_msg, $("#dev-account-settings-status").expectOne(), 4000);
}
} else {
settings_change_success(i18n.t("No changes made."));
} }
overlays.close_modal('change_email_modal'); overlays.close_modal('change_email_modal');
}, },
error: function (xhr) { error_msg_element: change_email_error,
ui_report.error(i18n.t("Failed"), xhr, change_email_info); };
}, settings_ui.do_settings_change(channel.patch, '/json/settings', data,
}); $('#account-settings-status').expectOne(), opts);
}); });
$('#change_email').on('click', function (e) { $('#change_email').on('click', function (e) {

View File

@ -49,11 +49,20 @@ exports.do_settings_change = function (request_method, url, data, status_element
settings_ui.display_checkmark(spinner); settings_ui.display_checkmark(spinner);
}, appear_after); }, appear_after);
if (success_continuation !== undefined) { if (success_continuation !== undefined) {
if (opts !== undefined && opts.success_continuation_arg) {
success_continuation(opts.success_continuation_arg);
} else {
success_continuation(reponse_data); success_continuation(reponse_data);
} }
}
}, },
error: function (xhr) { error: function (xhr) {
if (opts !== undefined && opts.error_msg_element) {
loading.destroy_indicator(spinner);
ui_report.error(exports.strings.failure, xhr, opts.error_msg_element);
} else {
ui_report.error(exports.strings.failure, xhr, spinner); ui_report.error(exports.strings.failure, xhr, spinner);
}
if (error_continuation !== undefined) { if (error_continuation !== undefined) {
error_continuation(xhr); error_continuation(xhr);
} }

View File

@ -1,9 +1,10 @@
<div id="account-settings" class="settings-section show" data-name="your-account"> <div id="account-settings" class="settings-section show" data-name="your-account">
<div class="alert" id="account-settings-status"></div> <div class="alert" id="dev-account-settings-status"></div>
<div class="account-settings-form"> <div class="account-settings-form">
<div class="inline-block"> <div class="inline-block">
<form class="email-change-form grid"> <form class="email-change-form grid">
<h3>{{t "User settings" }}</h3> <h3 class="inline-block">{{t "User settings" }}</h3>
<div class="alert-notification" id="account-settings-status"></div>
<div class="input-group user-name-section"> <div class="input-group user-name-section">
<label class="inline-block title">{{t "Email" }}</label> <label class="inline-block title">{{t "Email" }}</label>
<a id="change_email" {{#if page_params.is_admin}}href="#change_email"{{else}}{{#unless page_params.realm_email_changes_disabled}}href="#change_email"{{/unless}}{{/if}}> <a id="change_email" {{#if page_params.is_admin}}href="#change_email"{{else}}{{#unless page_params.realm_email_changes_disabled}}href="#change_email"{{/unless}}{{/if}}>