-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmcb.js
60 lines (55 loc) · 1.92 KB
/
mcb.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
function toggleComplete(path,selector){
firebase.auth().onAuthStateChanged( function(user) {
if (user) {
var docRef = db.doc('users/'+user.uid+'/progress/'+path);
docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
if(eval("doc.data()." + selector) == null){
elem.innerHTML = "Mark as complete";
elem.className = "btn btn-dark";
}
else{
elem.innerHTML = "Mark as incomplete";
elem.className = "btn btn-outline-dark";
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
elem.innerHTML = "Mark as complete";
elem.className = "btn btn-dark";
});
//that was for setting up
//this is for changing it live
var elem = document.querySelector("#mcb");
elem.addEventListener("click", (e) => {
e.preventDefault();
var user = firebase.auth().currentUser;
var docRef = db.doc('users/'+user.uid+'/progress/'+path);
docRef.get().then(function(doc) {
if (doc.exists) {
//console.log("Document data:", doc.data());
if(eval("doc.data()." + selector) == null){
eval("docRef.update({'"+selector+"': firebase.firestore.Timestamp.now()})");
elem.innerHTML = "Mark as incomplete";
elem.className = "btn btn-outline-dark";
}
else if (window.confirm("Are you sure you want to mark this lesson as incomplete? You'll permanently lose the timestamp associated with your completion.")){
eval("docRef.update({'"+selector+"': null})");
elem.innerHTML = "Mark as complete";
elem.className = "btn btn-dark";
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
});
}
});
}