diff --git a/assets/js/src/public.js b/assets/js/src/public.js index 787da900b6..8175dddef0 100644 --- a/assets/js/src/public.js +++ b/assets/js/src/public.js @@ -62,49 +62,51 @@ function ( // eslint-disable-line func-names endpoint: 'subscribers', action: 'subscribe', data: formData.data - }).fail(function (response) { // eslint-disable-line func-names - form.find('.mailpoet_validate_error').html( - response.errors.map(function (error) { // eslint-disable-line func-names - return error.message; - }).join('
') - ).show(); - }).done(function (response) { // eslint-disable-line func-names - if (window.grecaptcha && formData.recaptcha) { - window.grecaptcha.reset(formData.recaptcha); - } - return response; - }).done(function (response) { // eslint-disable-line func-names - // successfully subscribed - if ( - response.meta !== undefined + }) + .fail(function handleFailedPost(response) { + form.find('.mailpoet_validate_error').html( + response.errors.map(function buildErrorMessage(error) { + return error.message; + }).join('
') + ).show(); + }) + .done(function handleRecaptcha(response) { + if (window.grecaptcha && formData.recaptcha) { + window.grecaptcha.reset(formData.recaptcha); + } + return response; + }) + .done(function handleSuccess(response) { + // successfully subscribed + if ( + response.meta !== undefined && response.meta.redirect_url !== undefined - ) { + ) { // go to page - window.location.href = response.meta.redirect_url; - } else { + window.location.href = response.meta.redirect_url; + } else { // display success message - form.find('.mailpoet_validate_success').show(); - } + form.find('.mailpoet_validate_success').show(); + } - // reset form - form.trigger('reset'); - // reset validation - parsley.reset(); - // reset captcha - if (window.grecaptcha && formData.recaptcha) { - window.grecaptcha.reset(formData.recaptcha); - } + // reset form + form.trigger('reset'); + // reset validation + parsley.reset(); + // reset captcha + if (window.grecaptcha && formData.recaptcha) { + window.grecaptcha.reset(formData.recaptcha); + } - // resize iframe - if ( - window.frameElement !== null + // resize iframe + if ( + window.frameElement !== null && MailPoet !== undefined && MailPoet.Iframe - ) { - MailPoet.Iframe.autoSize(window.frameElement); - } - }); - + ) { + MailPoet.Iframe.autoSize(window.frameElement); + } + }); return false; }); });