-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (157 loc) · 6.03 KB
/
script.js
File metadata and controls
171 lines (157 loc) · 6.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
164
165
166
167
168
169
170
171
//create firebase reference
var dbRef = new Firebase("https://contactsbooks.firebaseio.com/");
var contactsRef = dbRef.child('contacts')
var usersRef = dbRef.child('users')
var auth = null;
var auth = null;
//Register
$('#doRegister').on('click', function (e) {
e.preventDefault();
$('#registerModal').modal('hide');
$('#messageModalLabel').html('<span class="text-center text-info"><i class="fa fa-cog fa-spin"></i></span>');
$('#messageModal').modal('show');
if( $('#registerEmail').val() != '' && $('#registerPassword').val() != '' && $('#registerConfirmPassword').val() != '' ){
if( $('#registerPassword').val() == $('#registerConfirmPassword').val() ){
//create the user
dbRef.createUser({
email : $('#registerEmail').val(),
password : $('#registerPassword').val()
}, function(error, userData) {
if (error) {
console.log("Error creating user:", error);
$('#messageModalLabel').html('<span class="text-danger">ERROR: '+ error.code + '</span>')
} else {
//now user is needed to be logged in to save data
dbRef.authWithPassword({
email : $('#registerEmail').val(),
password : $('#registerPassword').val()
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
$('#messageModalLabel').html('<span class="text-danger">ERROR: '+ error.code + '</span>')
} else {
console.log("Authenticated successfully with payload:", authData);
auth = authData;
//now saving the profile data
usersRef
.child(userData.uid)
.set({
firstName : $('#registerFirstName').val(),
lastName : $('#registerLastName').val(),
email : $('#registerEmail').val(),
}, function(){
console.log("User Information Saved:", userData.uid);
})
$('#messageModalLabel').html('<span class="text-center text-success">Success!</span>')
//hide the modal automatically
setTimeout( function () {
$('#messageModal').modal('hide');
$('.unauthenticated, .userAuth').toggleClass('unauthenticated').toggleClass('authenticated');
contactsRef
.child(auth.uid)
.on("child_added", function(snap) {
console.log("added", snap.key(), snap.val());
$('#contacts').append(contactHtmlFromObject(snap.val()));
});
}, 500)
}
});
console.log("Successfully created user account with uid:", userData.uid);
$('#messageModalLabel').html('<span class="text-success">Successfully created user account!</span>')
}
});
} else {
//password and confirm password didn't match
$('#messageModalLabel').html('<span class="text-danger">ERROR: Passwords didn\'t match</span>')
}
}
});
//Login
$('#doLogin').on('click', function (e) {
e.preventDefault();
$('#loginModal').modal('hide');
$('#messageModalLabel').html('<span class="text-center text-info"><i class="fa fa-cog fa-spin"></i></span>');
$('#messageModal').modal('show');
//var provider = new firebase.auth.GoogleAuthProvider();
//this.auth.signInWithPopup(provider);
if( $('#loginEmail').val() != '' && $('#loginPassword').val() != '' ){
//login the user
dbRef.authWithPassword({
email : $('#loginEmail').val(),
password : $('#loginPassword').val()
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
$('#messageModalLabel').html('<span class="text-danger">ERROR: '+ error.code + '</span>')
} else {
console.log("Authenticated successfully with payload:", authData);
auth = authData;
$('#messageModalLabel').html('<span class="text-center text-success">Success!</span>')
setTimeout( function () {
$('#messageModal').modal('hide');
$('.unauthenticated, .userAuth').toggleClass('unauthenticated').toggleClass('authenticated');
contactsRef
.child(auth.uid)
.on("child_added", function(snap) {
console.log("added", snap.key(), snap.val());
$('#contacts').append(contactHtmlFromObject(snap.val()));
});
})
}
});
}
});
//save contact
$('.addValue').on("click", function( event ) {
event.preventDefault();
if( auth != null ){
if( $('#name').val() != '' || $('#email').val() != '' ){
contactsRef
.child(auth.uid)
.push({
name: $('#name').val(),
email: $('#email').val(),
location: {
city: $('#city').val(),
state: $('#state').val(),
zip: $('#zip').val()
}
})
document.contactForm.reset();
} else {
alert('Please fill atlease name or email!');
}
} else {
//inform user to login
}
});
//save contact
$('.displayChart').on("click", function( event ) {
event.preventDefault();
if( auth != null ){
$.ajax({url: "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=X1-ZWz195iegi5q17_93aho&zpid=48749425", success: function(result){
var json = convertXml2Json(result);
}});
drawChart();
} else {
//inform user to login
}
});
//prepare conatct object's HTML
function contactHtmlFromObject(contact){
console.log( contact );
var html = '';
html += '<li class="list-group-item contact">';
html += '<div>';
html += '<p class="lead">'+contact.name+'</p>';
html += '<p>'+contact.email+'</p>';
html += '<p><small title="'
+contact.location.zip+'">'
+contact.location.city
+', '
+contact.location.state
+'</small></p>';
html += '</div>';
html += '</li>';
return html;
}