summaryrefslogtreecommitdiff
path: root/js/codeq/signup.js
blob: d41f1d5c5365e16ce13a458760d03120df297933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* CodeQ: an online programming tutor.
   Copyright (C) 2015 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
Software Foundation, either version 3 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
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(){
    "use strict";
    var jqScreen = $("#screen-signup"),
        jqUsername = $("#signup-username"),
        jqName = $("#signup-name"),
        jqEmail = $("#signup-email"),
        jqPassword = $("#signup-password"),
        jqVerify = $("#signup-password-verify"),
        jqFormSignUp = $('#signup-form'),
        jqExitSignUpBtn = $('#signup-cancel'),
        jqLangSelect = jqScreen.find('.lang-select'),
        jqNavBarRight = $('.nav.navbar-nav.navbar-right'),
        jqNavigationHomeBtn = $('#navigation-home');

    codeq.globalStateMachine.register('signup',{
        'jqScreen': jqScreen,

        'enter': function(){
            jqNavigationHomeBtn.off('click');//remove the click listener of this element here
            jqNavBarRight.css('display','none');//hide settings etc.
            $('#signed-in-title').html('');
            jqScreen.css('display', '');
            $('#disabled').css('display', 'none');
            jqExitSignUpBtn.on('click',function(){
                codeq.globalStateMachine.transition('login');
            });

            //prepare listener for successfull signup
            jqFormSignUp.on('submit',function(event) {
                if (jqPassword.val() != jqVerify.val()) {
                    alert('Passwords do not match.');
                }
                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);

                        })
                        .fail(function (reason) {
                            codeq.log.error('Signup failed: ' + reason, reason);
                            alert('Signup failed: ' + reason);
                        })
                        .done();
                }
                event.preventDefault(); // Prevent the form from submitting via the browser.
            });

            // setup language selection links
            jqLangSelect.on('click', function (e) {
                codeq.setLang($(this).data('lang'));
                e.preventDefault();
            });
        },
        'exit' : function(){
            jqScreen.css('display', 'none');
            //re-enable the click listener
            jqNavigationHomeBtn.on('click', function(e){
                codeq.globalStateMachine.transition('language');
                e.preventDefault();
            });
            //disable listeners on stuff from this page only
            jqExitSignUpBtn.off('click');
            jqFormSignUp.off('submit');
            jqLangSelect.off('click');
            //show the menu buttons
            jqNavBarRight.css('display','');

            jqUsername.val('');
            jqName.val('');
            jqEmail.val('');
            jqPassword.val('');
            jqVerify.val('');
        }
    });

})();