-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProfile.js
356 lines (322 loc) · 10.7 KB
/
Profile.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
var myPassword = "*******";
var storage = firebase.storage().ref();
var root = firebase.database().ref("Users");
var userId;
var currentUser;
var adminId = null;
var currentTeam = null;
window.onload = function () {
firebase.auth().onAuthStateChanged(function (user) {
// User is signed in: set userId and teamId
if (user) {
userId = user.uid;
currentUser = user;
changeView();
// Check values
console.log("userId has been set: " + userId);
updateName();
updatePhone();
updateEmail();
updatePicture();
document.getElementById("Password").innerHTML = myPassword.toString();
}
// No user is signed in.
else {
console.log("User is not logged in. !!");
}
});
};
function nameChange() {
document.getElementById("NameText").innerHTML =
'<input id="newName" type="text" placeholder="Insert Your Name" >';
document.getElementById("nameEdit").hidden = true;
document.getElementById("nameSave").hidden = false;
document.getElementById("nameCancel").hidden = false;
}
function passwordChange() {
var temp = document.getElementById("PasswordRow");
temp.style = "display: none";
document.getElementById("curPassword").style = "";
document.getElementById("newPassword").style = "";
document.getElementById("re-enterPW").style = "";
document.getElementById("passwordSave").hidden = false;
document.getElementById("passwordCancel").hidden = false;
document.getElementById("currentPW").value = "";
document.getElementById("newPW").value = "";
document.getElementById("reenterPW").value = "";
}
function emailChange() {
document.getElementById("Email").innerHTML =
'<input id="newEmail" type="email" placeholder="Insert Your Email" >';
document.getElementById("emailEdit").hidden = true;
document.getElementById("emailSave").hidden = false;
document.getElementById("emailCancel").hidden = false;
}
function phoneChange() {
document.getElementById("Phone").innerHTML =
'<input id="newPhone" type="tel" , placeholder="Insert Your Phone">';
document.getElementById("phoneEdit").hidden = true;
document.getElementById("phoneSave").hidden = false;
document.getElementById("phoneCancel").hidden = false;
}
function nameSave() {
var otherName = document.getElementById("newName").value;
if (/^\s+$/.test(otherName) || otherName == "") {
alert("Name Field Is Missing !!");
} else {
document.getElementById("NameText").innerHTML = otherName.toString();
doneNameEdit();
root
.child(userId)
.child("Name")
.set(otherName.toString());
var adminRef = firebase.database().ref("Users/" + userId + "/Teams/adminOf");
var memRef = firebase.database().ref("Users/" + userId + "/Teams/memberOf");
adminRef.once('child_added').then(function (snapshot) {
var team = snapshot.key;
console.log(team);
firebase.database().ref("Team/" + team + "/Members/" + userId + "/0").set(otherName);
console.log(firebase.database().ref("Team/" + team + "/Members/" + userId + "/0"));
});
memRef.once('child_added').then(function (snapshot) {
var team = snapshot.key;
console.log(team);
firebase.database().ref("Team/" + team + "/Members/" + userId + "/0").set(otherName);
console.log(firebase.database().ref("Team/" + team + "/Members/" + userId + "/0"));
});
}
}
function nameCancel() {
var databaseName = root.child(userId).child("Name");
databaseName.once("value", function (snapshot) {
var temp = snapshot.val();
document.getElementById("NameText").innerHTML = temp.toString();
});
doneNameEdit();
}
function passwordSave() {
var curPassword = document.getElementById("currentPW").value;
var newPassword1 = document.getElementById("newPW").value;
var newPassword2 = document.getElementById("reenterPW").value;
var databasePassword = root.child(userId).child("Password");
databasePassword.once("value", function (snapshot) {
databasePassword = snapshot.val();
if (curPassword == "" || newPassword1 == "" || newPassword1 == "") {
alert("Missing password field(s).");
return false;
} else if (newPassword1.length < "6") {
alert(
"Your new password is not long enough. Please re-enter a new password."
);
return false;
} else if (databasePassword !== curPassword) {
alert("Your current password is incorrect. Please try again.");
return false;
} else if (newPassword2 !== newPassword1) {
alert(
"The confirmation password does not match the new password. Please try again."
);
return false;
} else {
currentUser
.updatePassword(newPassword1)
.then(function () {
alert("You have successfully changed your password");
root
.child(userId)
.child("Password")
.set(newPassword1.toString());
donePasswordEdit();
})
.catch(function (error) {
alert("An error has occurred. Please try again.");
return false;
});
}
});
}
function passwordCancel() {
document.getElementById("Password").innerHTML = myPassword.toString();
donePasswordEdit();
}
function emailSave() {
var otherEmail = document.getElementById("newEmail").value;
if (otherEmail == "" || /^\s+$/.test(otherEmail)) {
alert("Name Field Is Missing !!");
} else if (!(otherEmail.includes("@") && otherEmail.includes("."))) {
alert("Your email is invalid !!");
} else {
document.getElementById("Email").innerHTML = otherEmail.toString();
doneEmailEdit();
root
.child(userId)
.child("Email")
.set(otherEmail.toString());
}
}
function emailCancel() {
var databaseEmail = root.child(userId).child("Email");
databaseEmail.once("value", function (snapshot) {
var temp = snapshot.val();
document.getElementById("Email").innerHTML = temp.toString();
});
doneEmailEdit();
}
function phoneSave() {
var otherPhone = document.getElementById("newPhone").value;
if (otherPhone == "" || /^\s+$/.test(otherPhone)) {
alert("Phone Field Is Missing !!");
} else if (
!/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/.test(
otherPhone
)
) {
alert("The phone number is invalid !!");
} else {
document.getElementById("Phone").innerHTML = otherPhone.toString();
donePhoneEdit();
root
.child(userId)
.child("Phone")
.set(otherPhone.toString());
}
}
function phoneCancel() {
var databasePhone = root.child(userId).child("Phone");
databasePhone.once("value", function (snapshot) {
var temp = snapshot.val();
document.getElementById("Phone").innerHTML = temp.toString();
});
donePhoneEdit();
}
function doneNameEdit() {
document.getElementById("nameEdit").hidden = false;
document.getElementById("nameSave").hidden = true;
document.getElementById("nameCancel").hidden = true;
}
function doneEmailEdit() {
document.getElementById("emailEdit").hidden = false;
document.getElementById("emailSave").hidden = true;
document.getElementById("emailCancel").hidden = true;
}
function donePhoneEdit() {
document.getElementById("phoneEdit").hidden = false;
document.getElementById("phoneSave").hidden = true;
document.getElementById("phoneCancel").hidden = true;
}
function donePasswordEdit() {
document.getElementById("passwordEdit").hidden = false;
document.getElementById("passwordSave").hidden = true;
document.getElementById("passwordCancel").hidden = true;
document.getElementById("curPassword").style = "display : none";
document.getElementById("newPassword").style = "display : none";
document.getElementById("re-enterPW").style = "display : none";
document.getElementById("PasswordRow").style = "";
}
var fileUpload = document.getElementById("upload");
fileUpload.addEventListener("change", function (e) {
var file = e.target.files[0];
var databaseStorage = firebase
.storage()
.ref()
.child(userId);
databaseStorage.put(file);
var reader = new FileReader();
reader.onload = function () {
var image = reader.result;
document.getElementById("Pic").src = image;
//document.getElementById('icon').src = image;
};
reader.readAsDataURL(file);
});
async function updatePicture() {
var databaseStorage = firebase
.storage()
.ref()
.child(userId);
databaseStorage
.getDownloadURL()
.then(function (url) {
document.getElementById("Pic").src = url;
// document.getElementById('icon').src = url;
})
.catch(function (error) {
document.getElementById("Pic").src = "img/sample.png";
});
}
async function updateName() {
var databaseName = root.child(userId);
databaseName.once("value", function (snapshot) {
if (!snapshot.hasChild("Name")) {
databaseName.child("Name").set("");
} else {
databaseName.child("Name").once("value", function (snapshot) {
var temp = snapshot.val();
document.getElementById("NameText").innerHTML = temp.toString();
});
}
});
}
async function updatePhone() {
console.log("TEST PASSED");
var databasePhone = root.child(userId);
databasePhone.once("value", function (snapshot) {
if (!snapshot.hasChild("Phone")) {
databasePhone.child("Phone").set("");
} else {
databasePhone.child("Phone").once("value", function (snapshot) {
var temp = snapshot.val();
document.getElementById("Phone").innerHTML = temp.toString();
});
}
});
}
async function updateEmail() {
console.log("TEST PASSED");
var databaseEmail = root.child(userId);
databaseEmail.once("value", function (snapshot) {
if (!snapshot.hasChild("Email")) {
databaseEmail.child("Email").set("");
} else {
databaseEmail.child("Email").once("value", function (snapshot) {
var temp = snapshot.val();
document.getElementById("Email").innerHTML = temp.toString();
});
}
});
}
function signOut() {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log("Signing out");
firebase.auth().signOut();
}
});
}
async function getCurrTeam(ref) {
return ref.once('value').then(function (snapshot) {
currentTeam = snapshot.val();
console.log(currentTeam);
});
}
async function getAdminID(ref) {
return ref.once('value').then(function (snapshot) {
adminId = snapshot.val();
console.log(adminId);
})
}
async function changeView() {
var item = document.getElementById("move");
var ref = firebase.database().ref("Users/" + userId + "/currTeam");
await getCurrTeam(ref);
var aRef = firebase.database().ref("Team/" + currentTeam + "/admin");
await getAdminID(aRef);
if (adminId == userId) {
console.log("Should not be printed");
item.href = "HomePage.html";
}
else {
item.href = "HomePageMem.html";
console.log("Should be printed");
}
}