-
Notifications
You must be signed in to change notification settings - Fork 0
/
parts.js
121 lines (105 loc) · 3.74 KB
/
parts.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
var urlParam = function(name, w){
w = w || window;
var rx = new RegExp('[\&|\?]'+name+'=([^\&\#]+)'),
val = w.location.search.match(rx);
return !val ? '':val[1];
}
window.onload = function() {
console.log("loaded");
firebase.auth().onAuthStateChanged(function(user) {
if (user.email == "[email protected]") {
var part = decodeURI(urlParam('part'))
console.log(part)
console.log(user);
var db = firebase.firestore()
db.collection("users").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
if (doc.data()["partsUsing"].includes(part)){
console.log(doc.id)
console.log(doc.data()["partsUsing"])
console.log(part)
addToList(doc.id, getOccurrence(doc.data()["partsUsing"], part))
}
});
});
var docRef = db.collection("components").doc(part);
docRef.get().then(function(doc) {
if (doc.exists) {
// console.log("Document data:", doc.data().status);
document.getElementById("name").value = doc.data()["name"];
document.getElementById("description").value = doc.data()["description"];
document.getElementById("resources").value = doc.data()["resources"];
document.getElementById("image").value = doc.data()["image"];
document.getElementById("quantity").value = doc.data()["quantity"];
document.getElementById("location").value = doc.data()["location"];
} else {
// doc.data() will be undefined in this case
// document.getElementById("status").innerHTML = "Have not applied";
// console.log("has not applied")
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
console.log(urlParam('part'))
} else {
console.log("notsignedin")
document.location.href = "index.html";
}
});
}
function getOccurrence(array, value) {
var count = 0;
array.forEach((v) => (v === value && count++));
return count;
}
function addToList(email, quantity){
console.log(email, "has", quantity)
var ul = document.getElementById("peopleWithPart")
var li = document.createElement("li")
li.className = "list-group-item d-flex justify-content-between align-items-center"
li.appendChild(document.createTextNode(email))
var span = document.createElement("span")
span.className ="badge badge-primary badge-pill"
span.innerHTML = quantity
li.appendChild(span)
ul.appendChild(li)
}
function editPart(){
var db = firebase.firestore()
name = document.getElementById('name').value
description = document.getElementById('description').value
resources = document.getElementById('resources').value
image = document.getElementById('image').value
quantity = document.getElementById('quantity').value
where = document.getElementById('location').value
if (name == "" || description == "" || resources == "" || image == "" || quantity == "" || location == ""){
alert("We need the name of the part")
} else {
console.log(name)
console.log(description)
console.log(resources)
console.log(image)
console.log(quantity)
console.log(where)
db.collection("components").doc(name).update({
name: name,
description: description,
quantity: quantity,
resources: resources,
image: image,
location: where
}).then(function(){
console.log("written")
}).catch(function(){
console.log("error", error)
})
console.log("wtf")
}
}
function signOutAccount() {
firebase.auth().signOut().then(function() {
console.log("signing out")
}).catch(function(error) {
// An error happened.
});
};