From 6c3e4fa5a674063e61fb92f5d9001649476c9cc0 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 26 Feb 2016 18:10:32 +0100 Subject: Replace several alerts with friendlier messages --- js/codeq/signup.js | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'js/codeq/signup.js') diff --git a/js/codeq/signup.js b/js/codeq/signup.js index d41f1d5..7f28e9f 100644 --- a/js/codeq/signup.js +++ b/js/codeq/signup.js @@ -31,12 +31,15 @@ along with this program. If not, see . */ jqExitSignUpBtn = $('#signup-cancel'), jqLangSelect = jqScreen.find('.lang-select'), jqNavBarRight = $('.nav.navbar-nav.navbar-right'), - jqNavigationHomeBtn = $('#navigation-home'); + jqNavigationHomeBtn = $('#navigation-home'), + jqMessages = jqFormSignUp.find('.error,.success'), + redirectTimeout = null; codeq.globalStateMachine.register('signup',{ 'jqScreen': jqScreen, - 'enter': function(){ + 'enter': function() { + jqMessages.hide(); jqNavigationHomeBtn.off('click');//remove the click listener of this element here jqNavBarRight.css('display','none');//hide settings etc. $('#signed-in-title').html(''); @@ -47,22 +50,32 @@ along with this program. If not, see . */ }); //prepare listener for successfull signup - jqFormSignUp.on('submit',function(event) { + jqFormSignUp.on('submit', function(e) { + jqMessages.hide(); if (jqPassword.val() != jqVerify.val()) { - alert('Passwords do not match.'); + jqFormSignUp.find('.error.mismatch').show(); } else { codeq.comms.signup(jqUsername.val(), jqName.val(), jqEmail.val(), jqPassword.val()) .then(function (data) { - if (data.code !== 0) throw new Error('Signup failed, code: ' + data.code + ', message: ' + data.message); - - var newUsername = jqUsername.val();//saving before transition (which will reset the value of the field) - //back to login - codeq.globalStateMachine.transition('login'); - alert('Welcome '+ newUsername +'. Thanks for signing up.'); - //assume user wants to sign-in with signed username - $("#username").val(newUsername); - + var jqSuccess = jqFormSignUp.find('.success'), + username = jqUsername.val(); + switch (data.code) { + case 0: // everything OK + $("#username").val(username); + jqSuccess.find('.username').text(username); + jqSuccess.show(); + // back to login after short pause + redirectTimeout = setTimeout(function() { + codeq.globalStateMachine.transition('login'); + }, 2000); + break; + case 10: // username exists + jqFormSignUp.find('.error.username-exists').show(); + break; + default: + throw new Error(data.message); + } }) .fail(function (reason) { codeq.log.error('Signup failed: ' + reason, reason); @@ -70,7 +83,7 @@ along with this program. If not, see . */ }) .done(); } - event.preventDefault(); // Prevent the form from submitting via the browser. + e.preventDefault(); // Prevent the form from submitting via the browser. }); // setup language selection links @@ -80,6 +93,11 @@ along with this program. If not, see . */ }); }, 'exit' : function(){ + if (redirectTimeout !== null) { + clearTimeout(redirectTimeout); + redirectTimeout = null; + } + jqMessages.hide(); jqScreen.css('display', 'none'); //re-enable the click listener jqNavigationHomeBtn.on('click', function(e){ -- cgit v1.2.1