quick-switcher: handle errors using Swal, and create a new helper to make calls the same across whole app.

This commit is contained in:
Marcin Kuzminski 2020-05-13 12:40:04 +02:00
parent 6da9683ece
commit aedb8a1855
5 changed files with 44 additions and 49 deletions

View file

@ -564,13 +564,15 @@
$.extend(ajaxSettings, options.ajaxSettings);
that.currentRequest = $.ajax(ajaxSettings).done(function (data) {
that.currentRequest = $.ajax(ajaxSettings)
.done(function (data) {
var result;
that.currentRequest = null;
result = options.transformResult(data);
that.processResponse(result, query, cacheKey);
options.onSearchComplete.call(that.element, query, result.suggestions);
}).fail(function (jqXHR, textStatus, errorThrown) {
})
.fail(function (jqXHR, textStatus, errorThrown) {
options.onSearchError.call(that.element, query, jqXHR, textStatus, errorThrown);
});
}

View file

@ -588,21 +588,12 @@ var CommentsController = function() {
var $comment = $(node).closest('.comment');
var comment_id = $comment.attr('data-comment-id');
Swal.fire({
SwalNoAnimation.fire({
title: 'Delete this comment?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#84a5d2',
cancelButtonColor: '#e85e4d',
confirmButtonText: _gettext('Yes, delete comment #{0}!').format(comment_id),
showClass: {
popup: 'swal2-noanimation',
backdrop: 'swal2-noanimation'
},
hideClass: {
popup: '',
backdrop: ''
}
}).then(function(result) {
if (result.value) {
self._deleteComment(node);

View file

@ -50,19 +50,9 @@ var UsersAutoComplete = function(input_id, user_id) {
var _showAuthToken = function (authTokenId, showUrl) {
Swal.fire({
SwalNoAnimation.fire({
title: _gettext('Show this authentication token?'),
showCancelButton: true,
confirmButtonColor: '#84a5d2',
cancelButtonColor: '#e85e4d',
showClass: {
popup: 'swal2-noanimation',
backdrop: 'swal2-noanimation'
},
hideClass: {
popup: '',
backdrop: ''
},
confirmButtonText: _gettext('Show'),
showLoaderOnConfirm: true,
allowOutsideClick: function () {
@ -98,19 +88,9 @@ var _showAuthToken = function (authTokenId, showUrl) {
var tmpl = ('<code>{0}</code>' +
'<i class="tooltip icon-clipboard clipboard-action" data-clipboard-text="{1}" title="Copy Token"></i>');
Swal.fire({
SwalNoAnimation.fire({
title: _gettext('Authentication Token'),
html: tmpl.format(result.value.auth_token, result.value.auth_token),
confirmButtonColor: '#84a5d2',
cancelButtonColor: '#e85e4d',
showClass: {
popup: 'swal2-noanimation',
backdrop: 'swal2-noanimation'
},
hideClass: {
popup: '',
backdrop: ''
},
})
}
})

View file

@ -80,6 +80,30 @@ var ajaxPOST = function (url, postData, success, failure) {
return request;
};
SwalNoAnimation = Swal.mixin({
confirmButtonColor: '#84a5d2',
cancelButtonColor: '#e85e4d',
showClass: {
popup: 'swal2-noanimation',
backdrop: 'swal2-noanimation'
},
hideClass: {
popup: '',
backdrop: ''
},
})
/* Example usage:
*
error: function(jqXHR, textStatus, errorThrown) {
var prefix = "Error while fetching entries.\n"
var message = formatErrorMessage(jqXHR, textStatus, errorThrown, prefix);
ajaxErrorSwal(message);
}
*
* */
function formatErrorMessage(jqXHR, textStatus, errorThrown, prefix) {
if(typeof prefix === "undefined") {
prefix = ''
@ -107,7 +131,7 @@ function formatErrorMessage(jqXHR, textStatus, errorThrown, prefix) {
}
function ajaxErrorSwal(message) {
Swal.fire({
SwalNoAnimation.fire({
icon: 'error',
title: _gettext('Ajax Request Error'),
html: '<span style="white-space: pre-line">{0}</span>'.format(message),
@ -135,21 +159,13 @@ function submitConfirm(event, self, question, confirmText, htmlText) {
}
event.preventDefault();
Swal.fire({
SwalNoAnimation.fire({
title: question,
icon: 'warning',
html: htmlText,
showClass: {
popup: 'swal2-noanimation',
backdrop: 'swal2-noanimation'
},
hideClass: {
popup: '',
backdrop: ''
},
showCancelButton: true,
confirmButtonColor: '#84a5d2',
cancelButtonColor: '#e85e4d',
confirmButtonText: confirmText
}).then(function(result) {
if (result.value) {

View file

@ -1069,8 +1069,14 @@
},
onSearchError: function (element, query, jqXHR, textStatus, errorThrown) {
if (jqXHR !== 'abort') {
alert("Error during search.\nError code: {0}".format(textStatus));
window.location = '';
var message = formatErrorMessage(jqXHR, textStatus, errorThrown);
SwalNoAnimation.fire({
icon: 'error',
title: _gettext('Error during search operation'),
html: '<span style="white-space: pre-line">{0}</span>'.format(message),
}).then(function(result) {
window.location.reload();
})
}
},
onSearchStart: function (params) {