diff --git a/static/script.js b/static/script.js index cdbc9eb..559ab3d 100644 --- a/static/script.js +++ b/static/script.js @@ -12,7 +12,7 @@ function configureSummarizeButtons() { e.preventDefault(); e.stopPropagation(); if (target.href) { - summarizeButtonClick(target.href, target); + summarizeButtonClick(button); } break; } @@ -20,21 +20,43 @@ function configureSummarizeButtons() { }, false); } -function summarizeButtonClick(url, button) { +function summarizeButtonClick(button) { + var url = button.href; + var loadingMsg = button.dataset.loading; + var errorMsg = button.dataset.error; var container = button.parentNode; + container.classList.add('alert'); + container.classList.add('alert-warning'); + container.innerHTML = loadingMsg; + var request = new XMLHttpRequest(); request.open('POST', url, true); request.responseType = 'json'; request.onload = function(e) { if (this.status != 200) { + return request.onerror(e); } + + var response = xmlHttpRequestJson(this); + if (!response) { + return request.onerror(e); + } + + if (response.status !== 200) { + return request.onerror(e); + } + + container.classList.remove('alert-warning'); + container.classList.add('alert-success'); + container.innerHTML = 'placeholder'; } request.onerror = function(e) { badAjax(this.status == 403); - container.classList.add('alert'); + container.classList.remove('alert-warning'); container.classList.add('alert-error'); + containter.innerHTML = errorMsg; } }