forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.js
More file actions
163 lines (138 loc) · 5.03 KB
/
signup.js
File metadata and controls
163 lines (138 loc) · 5.03 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import $ from 'jquery';
window.SignupManager = function (options) {
this.options = options;
var self = this;
// Check for URL having: /users/sign_up?user%5Buser_type%5D=teacher
if (self.options.isTeacher === "true") {
// Select teacher in dropdown.
$("#user_user_type").val("teacher");
showTeacher();
}
function formSuccess(success) {
var url;
if (self.options.returnToUrl !== "") {
url = self.options.returnToUrl;
} else if (isTeacherSelected()) {
url = self.options.teacherDashboardUrl;
} else {
url = "/";
}
window.location.href = url;
}
function formError(err) {
// Define the fields that can have specific errors attached to them.
var fields = ["user_type", "name", "email", "password", "password_confirmation", "schoolname", "schooladdress", "age", "gender", "terms_of_service_version"];
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
if (err.responseJSON.errors[field]) {
var errorField = $(`#${field}-block .error_in_field`);
// We have a custom inline message for user_type errors already set in the DOM.
if (field === "terms_of_service_version") {
errorField.text(self.options.acceptTermsString);
} else if (field !== "user_type") {
errorField.text(err.responseJSON.errors[field][0]);
}
errorField.fadeTo("normal", 1);
}
}
}
$("#user_user_type").change(function () {
var value = $(this).val();
if (value === "student") {
showStudent();
} else if (value === "teacher") {
showTeacher();
}
});
function showStudent() {
// Show correct form elements.
$("#age-block").fadeIn();
$("#gender-block").fadeIn();
$("#schoolname-block").hide();
$("#schooladdress-block").hide();
// Show correct terms below form.
$("#student-terms").fadeIn();
$("#teacher-terms").hide();
// Implicitly accept terms of service for students.
$("#user_terms_of_service_version").prop('checked', true);
}
function showTeacher() {
// Show correct form elements.
$("#age-block").hide();
$("#gender-block").hide();
$("#schoolname-block").fadeIn();
$("#schooladdress-block").fadeIn();
// Show correct terms below form.
$("#student-terms").hide();
$("#teacher-terms").fadeIn();
// Force teachers to explicitly accept terms of service.
$("#user_terms_of_service_version").prop('checked', false);
}
function isTeacherSelected() {
var formData = $('#new_user').serializeArray();
var userType = $.grep(formData, e => e.name === "user[user_type]");
if (userType.length === 1 && userType[0].value === "teacher") {
return true;
}
return false;
}
$(".signupform").submit(function () {
// Clear the prior hashed email.
$('#user_hashed_email').val('');
// Hash the email.
window.dashboard.hashEmail({email_selector: "#user_email",
hashed_email_selector: "#user_hashed_email",
age_selector: "#user_user_age",
skip_clear_email: true});
var formData = $("#new_user").serializeArray();
if (isTeacherSelected()) {
// Teachers get age 21 in the form data.
formData.push({name: "user[age]", value: "21+"});
} else {
// Students have their email cleared from the form data.
// (But we left it showing in the form UI in case they
// reattempt.)
$.grep(formData, e => e.name === "user[email]")[0].value = "";
}
// Hide all errors that might be showing from a previous attempt.
$(".error_in_field").css("opacity", 0);
// Hide any other hint messages that might be showing based on input.
$("#password_message").text("");
$("#password_message_confirmation").text("");
$.ajax({
url: "/users.json",
type: "post",
dataType: "json",
data: formData
}).done(formSuccess).fail(formError);
return false;
});
$("#user_password").on('input',function (e) {
var password = $(this).val();
var password_message = $("#password-block .error_in_field");
var password_message_confirmation = $("#password_confirmation-block .error_in_field");
if (!password || password.length < 6) {
password_message.text(self.options.invalidPasswordString);
password_message.fadeTo("normal", 1);
} else {
password_message.text("");
}
password_message_confirmation.text("");
});
$("#user_password_confirmation").on('input', function (e) {
var conf_password = $(this).val();
var origin_password = $('#user_password').val();
var password_message = $("#password-block .error_in_field");
var password_message_confirmation = $("#password_confirmation-block .error_in_field");
if (conf_password !== origin_password) {
password_message_confirmation.text(self.options.passwordMismatchString);
password_message_confirmation.fadeTo("normal", 1);
} else {
password_message_confirmation.text("");
}
password_message.text("");
});
$("#user_name").placeholder();
$("#user_email").placeholder();
$("#user_school").placeholder();
};