Clean up code duplication in settings.js error reporting.

We can save a bunch of code by using the handle ui.js error reporting
helpers.

(imported from commit 7e993cae6e314c84d4ba8eaefd2ff7711c4bc19d)
This commit is contained in:
David Roe
2015-08-20 16:23:53 -07:00
committed by Tim Abbott
parent 21ac93ae9c
commit 9590d988c5
2 changed files with 20 additions and 81 deletions

View File

@@ -196,15 +196,18 @@ exports.setup_page = function () {
$('.new-bot-ui').hide(); $('.new-bot-ui').hide();
} }
function settings_change_error(message, xhr) {
function settings_change_error(message) {
// Scroll to the top so the error message is visible. // Scroll to the top so the error message is visible.
// We would scroll anyway if we end up submitting the form. // We would scroll anyway if we end up submitting the form.
viewport.scrollTop(0); viewport.scrollTop(0);
var settings_status = $('#settings-status').expectOne(); ui.report_error(message, xhr, $('#settings-status').expectOne());
settings_status.removeClass(status_classes) }
.addClass('alert-error')
.text(message).stop(true).fadeTo(0,1); function settings_change_success(message) {
// Scroll to the top so the error message is visible.
// We would scroll anyway if we end up submitting the form.
viewport.scrollTop(0);
ui.report_success(message, $('#settings-status').expectOne());
} }
$("form.your-account-settings").ajaxForm({ $("form.your-account-settings").ajaxForm({
@@ -231,22 +234,10 @@ exports.setup_page = function () {
return true; return true;
}, },
success: function (resp, statusText, xhr, form) { success: function (resp, statusText, xhr, form) {
var message = "Updated settings!"; settings_change_success("Updated settings!");
var result = $.parseJSON(xhr.responseText);
var settings_status = $('#settings-status').expectOne();
settings_status.removeClass(status_classes)
.addClass('alert-success')
.text(message).stop(true).fadeTo(0,1);
}, },
error: function (xhr, error_type, xhn) { error: function (xhr, error_type, xhn) {
var response = "Error changing settings"; settings_change_error("Error changing settings", xhr);
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
settings_change_error(response);
}, },
complete: function (xhr, statusText) { complete: function (xhr, statusText) {
// Whether successful or not, clear the password boxes. // Whether successful or not, clear the password boxes.
@@ -292,24 +283,11 @@ exports.setup_page = function () {
page_params.enable_digest_emails = result.enable_digest_emails; page_params.enable_digest_emails = result.enable_digest_emails;
} }
notify_settings_status.removeClass(status_classes) ui.report_success("Updated notification settings!", notify_settings_status);
.addClass('alert-success')
.text(message).stop(true).fadeTo(0,1);
} }
function update_notification_settings_error(xhr, error_type, xhn) { function update_notification_settings_error(xhr, error_type, xhn) {
var response = "Error changing settings"; ui.report_error("Error changing settings", xhr, $('#notify-settings-status').expectOne());
var notify_settings_status = $('#notify-settings-status').expectOne();
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
notify_settings_status.removeClass(status_classes)
.addClass('alert-error')
.text(response).stop(true).fadeTo(0,1);
} }
function post_notify_settings_changes(notification_changes, success_func, function post_notify_settings_changes(notification_changes, success_func,
@@ -393,27 +371,11 @@ exports.setup_page = function () {
url: '/json/time_setting', url: '/json/time_setting',
data: data, data: data,
success: function (resp, statusText, xhr, form) { success: function (resp, statusText, xhr, form) {
var message = "Updated display settings! You will need to reload for the changes to take effect"; ui.report_success("Updated display settings! You will need to reload for the changes to take effect",
var result = $.parseJSON(xhr.responseText); $('#display-settings-status').expectOne());
var display_settings_status = $('#display-settings-status').expectOne();
display_settings_status.removeClass(status_classes)
.addClass('alert-success')
.text(message).stop(true).fadeTo(0,1);
}, },
error: function (xhr, error_type, xhn) { error: function (xhr, error_type, xhn) {
var response = "Error updating display settings"; ui.report_error("Error updating display settings", xhr, $('#display-settings-status').expectOne());
var display_settings_status = $('#display-settings-status').expectOne();
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
display_settings_status.removeClass(status_classes)
.addClass('alert-error')
.text(response).stop(true).fadeTo(0,1);
} }
}); });
}); });
@@ -435,17 +397,7 @@ exports.setup_page = function () {
settings_status.hide(); settings_status.hide();
}, },
error: function (xhr, error_type, xhn) { error: function (xhr, error_type, xhn) {
var response = "Error getting API key"; ui.report_error("Error getting API key", xhr, $('#settings-status').expectOne());
var settings_status = $('#settings-status').expectOne();
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
settings_status.removeClass(status_classes)
.addClass('alert-error')
.text(response).stop(true).fadeTo(0,1);
$("#show_api_key_box").hide(); $("#show_api_key_box").hide();
$("#get_api_key_box").show(); $("#get_api_key_box").show();
} }
@@ -693,23 +645,10 @@ exports.setup_page = function () {
resize.resize_page_components(); resize.resize_page_components();
} }
ui_settings_status.removeClass(status_classes) ui.report_success(message, ui_settings_status);
.addClass('alert-success')
.text(message).stop(true).fadeTo(0,1);
}, },
error: function (xhr, error_type, xhn) { error: function (xhr, error_type, xhn) {
var response = "Error changing settings"; ui.report_error("Error changing settings", xhr, $('#ui-settings-status').expectOne());
var ui_settings_status = $('#ui-settings-status').expectOne();
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
ui_settings_status.removeClass(status_classes)
.addClass('alert-error')
.text(response).stop(true).fadeTo(0,1);
} }
}); });
}); });

View File

@@ -133,7 +133,7 @@ exports.report_message = function (response, status_box, cls) {
}; };
exports.report_error = function (response, xhr, status_box) { exports.report_error = function (response, xhr, status_box) {
if (xhr.status.toString().charAt(0) === "4") { if (xhr && xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted // Only display the error response for 4XX, where we've crafted
// a nice response. // a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg; response += ": " + $.parseJSON(xhr.responseText).msg;