From 2f415bc811f7378091bd2285e9e822cb39eed3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Pu=C5=A1nik?= Date: Mon, 5 Oct 2015 19:59:05 +0200 Subject: sign up, reset password and profile pages + related comm messages --- js/codeq/comms.js | 21 ++++++++++++++++++- js/codeq/login.js | 9 +++++--- js/codeq/navigation.js | 10 +++++++++ js/codeq/profile.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 4 deletions(-) (limited to 'js') diff --git a/js/codeq/comms.js b/js/codeq/comms.js index 3671e95..54a912f 100644 --- a/js/codeq/comms.js +++ b/js/codeq/comms.js @@ -297,10 +297,29 @@ }); }; if (socket) return performLogin(); - // this is the only method where we do connect() first, if we're not already connected + // this is the 1st method where we do connect() first, if we're not already connected return this.connect().then(performLogin); }, + 'logout': function () { + return this.send({'action': 'logout'}); + }, + + 'signup': function (username, password) { + var myself = this, + performSignUp = function () { + return myself.send({'action': 'signup', 'username': username, 'password': password}); + }; + + if (socket) return performSignUp(); + // this is the 2nd method where we do connect() first, if we're not already connected + return this.connect().then(performSignUp); + }, + + 'changePassword': function (newpassword) { + return this.send({'action': 'change_password', 'password': newpassword}); + }, + 'updateSettings': function (new_settings){ return this.send({'action': 'settings', 'sid': sid, 'settings': new_settings}); }, diff --git a/js/codeq/login.js b/js/codeq/login.js index 391592b..d5bcd90 100644 --- a/js/codeq/login.js +++ b/js/codeq/login.js @@ -15,6 +15,9 @@ $('#disabled').css('cursor', ''); if (data.code !== 0) throw new Error('Login failed, code: ' + data.code + ', message: ' + data.message); + //merge profile + $('#profileUsername').html($('#username').val()); + //merge these settings into the already existing default settings var sett = data.settings; $.merge(true, codeq.settings, sett); @@ -47,14 +50,14 @@ } }); $('#modalLogIn').modal(); - - $("#screen_login").css('display', ''); + //$("#screen_login").css('display', ''); $('#disabled').css('display', 'none'); }, 'exit' : function(){ $("#submit").off('click', loginFun); formInputs.off('keyup'); - $("#screen_login").css('display', 'none'); + $('#modalLogIn').modal('hide'); + //$("#screen_login").css('display', 'none'); $('#signed-in-title').html('Signed in as '+$('#username').val()); $("#password").val(''); } diff --git a/js/codeq/navigation.js b/js/codeq/navigation.js index 4eff834..a1b8f3d 100644 --- a/js/codeq/navigation.js +++ b/js/codeq/navigation.js @@ -102,6 +102,16 @@ e.preventDefault(); }); $('#navigation-logout').on('click', function(e){ + codeq.comms.logout() + .then(function (data) { + console.log(data); + //codeq.globalStateMachine.transition('login'); + }) + .fail(function (reason) { + console.log(reason); + }) + .done(); + codeq.globalStateMachine.transition('login'); e.preventDefault();//prevent this since we'll trigger a page reload otherwise }); diff --git a/js/codeq/profile.js b/js/codeq/profile.js index 5dcda05..be0bb1e 100644 --- a/js/codeq/profile.js +++ b/js/codeq/profile.js @@ -3,6 +3,62 @@ */ (function(){ + + codeq.profile = { + + 'signup': function () { + var jqUsername = $("#modalSignUpUsername"), + jqPassword = $("#modalSignUpPassword"), + jqVerify = $("#modalSignUpVerify"); + + if (jqUsername.val() == "" || jqPassword.val() == "" || jqVerify.val() == "") { + alert('Some fields are empty.'); + } + else if (jqPassword.val() != jqVerify.val()) { + alert('Passwords do not match.'); + } + else { + codeq.comms.signup(jqUsername.val(), jqPassword.val()) + .then(function (data) { + //back to login + $('#modalSignUp').modal('hide'); + alert('Welcome '+ jqUsername.val() +'. Thanks for signing up.'); + $('#modalLogIn').modal('show'); + //assume user wants to sign-in with signed username + $("#username").val(jqUsername.val()); + //clear + jqUsername.val(''); + jqPassword.val(''); + jqVerify.val(''); + }) + .done(); + } + + }, + + 'changePassword': function () { + var jqNew = $("#modalChangePasswordNew"), + jqVerify = $("#modalChangePasswordVerify"); + + if (jqNew.val() == "" || jqVerify.val() == "") { + alert('Some fields are empty.'); + } + else if (jqNew.val() != jqVerify.val()) { + alert('Passwords do not match.'); + } + else { + codeq.comms.changePassword(jqNew.val()) + .then(function (data) { + $('#modalChangePassword').modal('hide'); + jqNew.val(''); + jqVerify.val(''); + alert('Password changed.'); + }) + .done(); + } + } + }; + codeq.globalStateMachine.register('profile',{ 'enter': function(){ -- cgit v1.2.1