diff --git a/src/assets/js/protocol/protocol-newsletter.js b/src/assets/js/protocol/protocol-newsletter.js index ba410fc7..0d1f6146 100644 --- a/src/assets/js/protocol/protocol-newsletter.js +++ b/src/assets/js/protocol/protocol-newsletter.js @@ -3,38 +3,9 @@ // !! This file assumes only one signup form per page !! - var newsletterForm = document.getElementById('newsletter-form'); - - // Handle errors - var errorArray = []; - var newsletterErrors = document.getElementById('newsletter-errors'); - function newsletterError() { - var errorList = document.createElement('ul'); - - if(errorArray.length) { - for (var i = 0; i < errorArray.length; i++) { - var item = document.createElement('li'); - item.appendChild(document.createTextNode(errorArray[i])); - errorList.appendChild(item); - } - } else { - // No error messages, forward to server for better troubleshooting - newsletterForm.setAttribute('data-skip-xhr', true); - newsletterForm.submit(); - } - - newsletterErrors.appendChild(errorList); - newsletterErrors.style.display = 'block'; - } - - // Show success message - function newsletterThanks() { - var thanks = document.getElementById('newsletter-thanks'); - thanks.style.display = 'block'; - } - // Expand email form on input focus or submit if details aren't visible function initEmailForm() { + var newsletterForm = document.getElementById('newsletter-form'); var submitButton = document.getElementById('newsletter-submit'); var formDetails = document.getElementById('newsletter-details'); var formControls = document.querySelectorAll('.mzp-c-newsletter-form input, .mzp-c-newsletter-form select'); @@ -57,82 +28,14 @@ emailFormShowDetails(); } }); - } - initEmailForm(); - // XHR subscribe; handle errors; display thanks message on success. - function newsletterSubscribe(evt) { - var skipXHR = newsletterForm.getAttribute('data-skip-xhr'); - if (skipXHR) { - return true; - } - evt.preventDefault(); - evt.stopPropagation(); - - // New submission, clear old errors - errorArray = []; - newsletterErrors.style.display = 'none'; - while (newsletterErrors.firstChild) { - newsletterErrors.removeChild(newsletterErrors.firstChild); - } - - var email = document.getElementById('email').value; - var newsletter = document.getElementById('newsletters').value; - var privacy = document.querySelector('input[name="privacy"]:checked') ? '&privacy=true' : ''; - var lang = document.getElementById('language').value; - var fmt = document.querySelector('input[name="fmt"]:checked').value; - var sourceUrl = document.getElementById('source-url').value; - var params = 'email=' + encodeURIComponent(email) + - '&newsletters=' + newsletter + - privacy + - '&lang=' + lang + - '&fmt=' + fmt + - '&source_url=' + sourceUrl; - - var xhr = new XMLHttpRequest(); - - xhr.onload = function(r) { - if (r.target.status >= 200 && r.target.status < 300) { - // response is null if handled by service worker - if(response === null ) { - newsletterError(new Error()); - return; - } - var response = r.target.response; - if (response.success === true) { - newsletterForm.style.display = 'none'; - newsletterThanks(); - } - else { - if(response.errors) { - for (var i = 0; i < response.errors.length; i++) { - errorArray.push(response.errors[i]); - } - } - newsletterError(new Error()); - } - } - else { - newsletterError(new Error()); + newsletterForm.addEventListener('submit', function(e) { + if (window.getComputedStyle(formDetails).display === 'none') { + e.preventDefault(); + emailFormShowDetails(); } - }; - - xhr.onerror = function(e) { - newsletterError(e); - }; - - var url = newsletterForm.getAttribute('action'); - - xhr.open('POST', url, true); - xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - xhr.setRequestHeader('X-Requested-With','XMLHttpRequest'); - xhr.timeout = 5000; - xhr.ontimeout = newsletterError; - xhr.responseType = 'json'; - xhr.send(params); - - return false; + }); } + initEmailForm(); - newsletterForm.addEventListener('submit', newsletterSubscribe, false); })(); diff --git a/src/patterns/organisms/newsletter/newsletter.hbs b/src/patterns/organisms/newsletter/newsletter.hbs index 593dc4a0..c448374e 100644 --- a/src/patterns/organisms/newsletter/newsletter.hbs +++ b/src/patterns/organisms/newsletter/newsletter.hbs @@ -4,9 +4,10 @@ description: | The standard newsletter subscription form. The example below is **better viewed in isolation** on the [newsletter demo page](/demos/newsletter.html), where it can be viewed full screen. + + This example only provides the essentials of the form component. Making this subscription form fully functional requires some additional functionality. See the [Basket example](https://github.com/mozilla/basket-example/) for more. notes: | - Some newsletters are only available in one language, so don’t require a language selection. - - This example is a fully-functional form that will attempt to subscribe any submitted address to the actual Mozilla newsletter. Use the mock addresses "success@example.com" and "failure@example.com" for testing. ---