summaryrefslogtreecommitdiff
path: root/js/codeq/signup.js
blob: 34982012ad8d6a4f75cd8827fe2e2a9cc6a6c652 (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
/**
 * Created by robert on 10/14/15.
 */
(function(){

    var jqScreen = $("#screen_signup"),
        jqUsername = $("#modalSignUpUsername"),
        jqName = $("#modalSignUpName"),
        jqEmail = $("#modalSignUpEmail"),
        jqPassword = $("#modalSignUpPassword"),
        jqVerify = $("#modalSignUpVerify"),
        jqFormSignUp = $('#formSignUp'),
        jqExitSignUpBtn = $('#exit_signup_btn'),
        jqLangSelect = jqScreen.find('.lang-select'),
        jqNavBarRight = $('.nav.navbar-nav.navbar-right'),
        jqNavigationHomeBtn = $('#navigation-home');

    codeq.globalStateMachine.register('signup',{
        '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('');
        }
    });

})();