From 2285c08c01a8235c29bd1783acca82a802f55df7 Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Mon, 26 Jun 2017 13:51:53 +0100 Subject: [PATCH] Revert "Revert "Improve ajax errors [MAILPOET-929]"" This reverts commit 81c3e2facfdcadd738509b94c91bf099c2f27e88. --- assets/js/src/ajax.js | 159 ++++++++++++++++++++++-------------------- views/layout.html | 3 + 2 files changed, 88 insertions(+), 74 deletions(-) diff --git a/assets/js/src/ajax.js b/assets/js/src/ajax.js index e76f3ad76d..ed0994528f 100644 --- a/assets/js/src/ajax.js +++ b/assets/js/src/ajax.js @@ -1,74 +1,85 @@ -define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) { - 'use strict'; - MailPoet.Ajax = { - version: 0.5, - options: {}, - defaults: { - url: null, - api_version: null, - endpoint: null, - action: null, - token: null, - data: {} - }, - post: function(options) { - return this.request('post', options); - }, - init: function(options) { - // merge options - this.options = jQuery.extend({}, this.defaults, options); - - // set default url - if(this.options.url === null) { - this.options.url = ajaxurl; - } - - // set default token - if(this.options.token === null) { - this.options.token = window.mailpoet_token; - } - }, - getParams: function() { - return { - action: 'mailpoet', - api_version: this.options.api_version, - token: this.options.token, - endpoint: this.options.endpoint, - method: this.options.action, - data: this.options.data || {} - } - }, - request: function(method, options) { - // set options - this.init(options); - - // set request params - var params = this.getParams(); - var deferred = jQuery.Deferred(); - - // remove null values from the data object - if (_.isObject(params.data)) { - params.data = _.pick(params.data, function(value) { - return (value !== null) - }) - } - - // ajax request - deferred = jQuery.post( - this.options.url, - params, - null, - 'json' - ).then(function(data) { - return data; - }, function(xhr) { - return xhr.responseJSON; - }); - - // clear options - this.options = {}; - - return deferred; - } - }; -}); +function requestFailed(errorMessage, xhr) { + if (xhr.responseJSON) { + return xhr.responseJSON; + } + var message = errorMessage.replace("%d", xhr.status); + return { + errors: [ + { + message: message + } + ] + } +} + +define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) { + + MailPoet.Ajax = { + version: 0.5, + options: {}, + defaults: { + url: null, + api_version: null, + endpoint: null, + action: null, + token: null, + data: {} + }, + post: function(options) { + return this.request('post', options); + }, + init: function(options) { + // merge options + this.options = jQuery.extend({}, this.defaults, options); + + // set default url + if(this.options.url === null) { + this.options.url = ajaxurl; + } + + // set default token + if(this.options.token === null) { + this.options.token = window.mailpoet_token; + } + }, + getParams: function() { + return { + action: 'mailpoet', + api_version: this.options.api_version, + token: this.options.token, + endpoint: this.options.endpoint, + method: this.options.action, + data: this.options.data || {} + } + }, + request: function(method, options) { + // set options + this.init(options); + + // set request params + var params = this.getParams(); + + // remove null values from the data object + if (_.isObject(params.data)) { + params.data = _.pick(params.data, function(value) { + return (value !== null) + }) + } + + // ajax request + var deferred = jQuery.post( + this.options.url, + params, + null, + 'json' + ).then(function(data) { + return data; + }, _.partial(requestFailed, MailPoet.I18n.t('ajaxFailedErrorMessage'))); + + // clear options + this.options = {}; + + return deferred; + } + }; +}); diff --git a/views/layout.html b/views/layout.html index 056cfa588d..c4884a9832 100644 --- a/views/layout.html +++ b/views/layout.html @@ -55,6 +55,9 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last') 'mailpoet.js' )%> +<%= localize({ + 'ajaxFailedErrorMessage': __('An error has happened while performing a request, the server has responded with response code %d'), +}) %> <% block translations %><% endblock %> <% block after_translations %><% endblock %>