forked from binoy14/quagsire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.js
145 lines (129 loc) · 3.44 KB
/
device.js
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
var device = require('./helpers/findDevice');
var getId = require('./helpers/getId');
var moment = require('moment');
var config = require('./config');
var _ = require('lodash');
// Firebase
var Firebase = require('firebase');
var rootRef = new Firebase(config.firebaseUrl);
rootRef.authWithCustomToken(config.firebaseSecret, function(err, authData) {
if (err) {
alert("Error with authentication");
throw err;
} else {
console.log("Login succeeded!", authData);
}
});
var membersRef = rootRef.child('members');
var date = moment().format("MM-DD-YYYY");
var meetingRef = rootRef.child('meetings').child(date);
var finalId;
if (device) {
device.on('data', function(num) {
finalId = getId(num);
var name;
if (finalId != false) {
hideElem('#error');
hideElem('#success');
membersRef.child(finalId).on('value', function(snap) {
if (!snap.val()) {
showElem('#info-form');
} else {
name = snap.val().firstName + ' ' + snap.val().lastName;
var obj = {};
obj[finalId] = true;
meetingRef.update(obj);
showSuccessfullMessage(name);
}
});
showStudentId(finalId);
} else {
showElem('#error');
}
});
device.on('error', function(err) {
if (err) throw err;
showElem('#error');
device.close();
});
} else {
showElem('#error-2');
}
/*
Helper Functions
*/
$('.selectpicker').selectpicker();
function showElem(id) {
$(id).removeClass('hidden').addClass('animated fadeIn');
$(id).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
$(this).removeClass('animated fadeIn');
});
}
function hideElem(id) {
$(id).addClass('animated fadeOut');
$(id).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
$(this).removeClass('animated fadeOut').addClass('hidden');
});
}
function showStudentId(id) {
$('#student-id').html(id);
}
function showSuccessfullMessage(name) {
$('#signed-name').html(name);
showElem('#success');
}
$.validator.addMethod('ucid', function(value, element) {
return /^[a-z].+\d$/ig.test(value);
}, 'Please enter a valid UCID');
$('#info-form').validate({
rules: {
ucid: "required ucid",
firstName: "required",
lastName: "required",
major: "required",
standing: "required"
},
ignore: ':not(select:hidden, input:visible, textarea:visible)',
errorPlacement: function(error, element) {
if (element.hasClass('selectpicker')) {
error.insertAfter('.bootstrap-select');
} else {
error.insertAfter(element);
}
},
messages: {
'standing': "Please select class standing"
}
});
var onComplete = function(err){
if(err){
throw err;
alert('Please swipe again');
} else {
$('#ucid').val('');
$('#first-name').val('');
$('#last-name').val('');
$('#major').val('');
$('#standing').val('');
}
};
$('#info-form').submit(function(e) {
e.preventDefault();
if ($(this).valid()) {
var ucid = $('#ucid').val();
var firstName = $('#first-name').val();
var lastName = $('#last-name').val();
var major = $('#major').val();
var standing = $('#standing').val();
var email = ucid + '@njit.edu';
membersRef.child(finalId).set({
ucid: ucid,
email: email,
firstName: firstName,
lastName: lastName,
standing: standing,
major: major
}, onComplete);
hideElem('#info-form');
}
});