diff options
Diffstat (limited to 'js/codeq')
-rw-r--r-- | js/codeq/change_password.js | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/js/codeq/change_password.js b/js/codeq/change_password.js index 6311764..8091a6d 100644 --- a/js/codeq/change_password.js +++ b/js/codeq/change_password.js @@ -1,5 +1,5 @@ /* CodeQ: an online programming tutor. - Copyright (C) 2015 UL FRI + Copyright (C) 2015,2016 UL FRI This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free @@ -14,44 +14,47 @@ details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ - - -/** - * Created by robert on 10/14/15. - * - */ -(function(){ +(function () { "use strict"; var jqScreen = $('#screen-change-password'), jqChangePassForm = $('#change-password-form'), jqNew = $('#change-password-new'), jqVerify = $('#change-password-verify'), jqCancelBtn = $('#change-password-cancel'), - jqErrorMismatch = jqChangePassForm.find('.error.mismatch'); + jqMessages = jqChangePassForm.find('.error,.success'), + redirectTimeout = null; - codeq.globalStateMachine.register('changePassword',{ + codeq.globalStateMachine.register('changePassword', { 'jqScreen': jqScreen, 'isModal': true, - 'enter': function() { - jqErrorMismatch.hide(); + 'enter': function () { + jqChangePassForm.find('button,input').prop('disabled', false); + jqMessages.hide(); jqCancelBtn.on('click',function(){ history.back();//forces a transition to the previous state }); jqChangePassForm.on('submit', function (e) { - jqErrorMismatch.hide(); + jqMessages.hide(); if (jqNew.val() != jqVerify.val()) { - jqErrorMismatch.show(); + jqChangePassForm.find('.error.mismatch').show(); } else { codeq.comms.changePassword(jqNew.val()) .then(function (data) { - if (data.code !== 0) throw new Error('Password change failed, code: ' + data.code + ', message: ' + data.message); - alert('Password changed.'); - history.back();//force to return to previous state + switch (data.code) { + case 0: + jqChangePassForm.find('button,input').prop('disabled', true); + jqChangePassForm.find('.success').show(); + redirectTimeout = setTimeout(function () { + history.back(); + }, 2000); + break; + default: + jqChangePassForm.find('.error.failed').show(); + } }) .fail(function (reason) { - codeq.log.error('Password change failed: ' + reason, reason); alert('Password change failed: ' + reason); }) .done(); @@ -59,12 +62,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ e.preventDefault(); // Prevent the form from submitting via the browser. }); - jqScreen.css('display', ''); $('#disabled').css('display', 'none'); }, - 'exit' : function(){ - jqErrorMismatch.hide(); + 'exit': function () { + if (redirectTimeout !== null) { + clearTimeout(redirectTimeout); + redirectTimeout = null; + } + jqChangePassForm.find('button,input').prop('disabled', false); + jqMessages.hide(); jqChangePassForm.off('submit'); jqCancelBtn.off('click'); jqScreen.css('display', 'none'); @@ -72,5 +79,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ jqVerify.val(''); } }); - })(); |