From 96037111961dafd0fa76a3e55f8c7cf0dee76975 Mon Sep 17 00:00:00 2001 From: Alessandro Verdura Date: Wed, 27 May 2015 12:09:15 +0200 Subject: [PATCH] Improvements on error handling + misc TNL-925 --- cms/static/js/factories/import.js | 32 +++++++++++++++++++++++-------- cms/static/js/views/import.js | 18 ++++++++--------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/cms/static/js/factories/import.js b/cms/static/js/factories/import.js index 5ed47b67a4..ad1da5124d 100644 --- a/cms/static/js/factories/import.js +++ b/cms/static/js/factories/import.js @@ -5,6 +5,14 @@ define([ 'use strict'; return function (feedbackUrl, library) { + var dbError; + + if (library) { + dbError = gettext('There was an error while importing the new library to our database.'); + } else { + dbError = gettext('There was an error while importing the new course to our database.'); + } + var bar = $('.progress-bar'), fill = $('.progress-fill'), submitBtn = $('.submit-button'), @@ -15,8 +23,8 @@ define([ gettext('There was an error while verifying the file you submitted.') + '\n', dbError + '\n' ], + unloading = false, previousImport = Import.storedImport(), - dbError, file; var onComplete = function () { @@ -26,11 +34,7 @@ define([ .show(); } - if (library) { - dbError = gettext('There was an error while importing the new library to our database.'); - } else { - dbError = gettext('There was an error while importing the new course to our database.'); - } + $(window).on('beforeunload', function (event) { unloading = true; }); // Display the status of last file upload on page load if (previousImport) { @@ -77,9 +81,21 @@ define([ } errMsg = serverMsg.hasOwnProperty('ErrMsg') ? serverMsg.ErrMsg : ''; - stage = Math.abs(serverMsg.Stage || 0); - Import.error(defaults[stage] + errMsg, stage); + if (serverMsg.hasOwnProperty('Stage')) { + stage = Math.abs(serverMsg.Stage); + Import.error(defaults[stage] + errMsg, stage); + } + // It could be that the user is simply refreshing the page + // so we need to be sure this is an actual error from the server + else if (!unloading) { + $(window).off('beforeunload.import'); + + Import.reset(); + onComplete(); + + alert(gettext('Your import has failed.') + '\n\n' + errMsg); + } } }); }); diff --git a/cms/static/js/views/import.js b/cms/static/js/views/import.js index 08245bde85..94babed3c9 100644 --- a/cms/static/js/views/import.js +++ b/cms/static/js/views/import.js @@ -9,7 +9,7 @@ define( /********** Private properties ****************************************/ - var COOKIE_NAME = 'lastfileupload'; + var COOKIE_NAME = 'lastimportupload'; var STAGE = { 'UPLOADING': 0, @@ -44,7 +44,7 @@ define( * */ var destroyEventListeners = function () { - window.onbeforeunload = null; + $(window).off('beforeunload.import'); }; /** @@ -60,11 +60,11 @@ define( * */ var initEventListeners = function () { - window.onbeforeunload = function () { + $(window).on('beforeunload.import', function () { if (current.stage <= STAGE.UNPACKING) { return gettext('Your import is in progress; navigating away will abort it.'); } - } + }); }; /** @@ -119,16 +119,13 @@ define( } function errorStage(stage) { - var $stage = $(stage); - var error = currStageMsg; - - if (!$stage.hasClass('has-error')) { - $stage + if (!$(stage).hasClass('has-error')) { + $(stage) .removeClass('is-started') .addClass('has-error') .find('p.copy') .hide() - .after("

" + error + "

"); + .after("

" + currStageMsg + "

"); } } @@ -251,6 +248,7 @@ define( current.stage = STAGE.UPLOADING; current.state = STATE.READY; + clearTimeout(timeout.id); updateFeedbackList(); },