-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
140 lines (128 loc) · 4.97 KB
/
cart.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
const firebaseConfig = {
apiKey: "AIzaSyATzuEEeKwr9tht1-VZhWzjPcMue6hT3Us",
authDomain: "santa-clara-swap.firebaseapp.com",
projectId: "santa-clara-swap",
storageBucket: "santa-clara-swap.appspot.com",
messagingSenderId: "765275523808",
appId: "1:765275523808:web:6ffa04584f7f877853de85",
storageBucket: 'santa-clara-swap.appspot.com'
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
db = firebase.firestore();
storage = firebase.storage()
var storageRef = firebase.storage().ref();
var email = "";
var array = [];
var k = 0;
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log("logged in");
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/v8/firebase.User
var uid = user.uid;
email = user.email;
document.getElementById("login").style.display = 'none';
document.getElementById("signup").style.display = 'none';
document.getElementById("navbar").innerHTML = `<li class="newsreader-400" style="float:right; padding-top: 10px;" onclick="firebase.auth().signOut();"><a href="" class="last-button">Sign Out</a></li> ` + document.getElementById("navbar").innerHTML;
} else {
// User is signed out
// ...
}
getCartItems();
});
function getCartItems() {
console.log("user email", email);
var d = 0;
db.collection("carts").where("email", "==", email)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
items = doc.data().items;
var len = items.length;
console.log(items, len);
arr = items;
console.log(len);
for (i = 0; i < len; i++) {
makeCards(items[i]);
// console.log(i, items[i]);
// var imageName = items[i];
// var spaceRef = storageRef.child(imageName);
// spaceRef.getDownloadURL().then(function(url) {
// // Handle the download URL (e.g., display the image in an <img> tag)
// console.log('Download URL:', url);
// console.log("IMAGE ID", imageName);
// makeCards(arr, url, d);
// d++;
// }).catch(function(error) {
// // Handle any errors
// console.error('Error retrieving image:', error);
// });
}
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
}
function deleteFromCart(imageName) {
var docRef = db.collection("carts").doc(email);
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
items=doc.data().items;
console.log("OLD ARRAY", items, imageName, array, array[imageName]);
const index = items.indexOf(array[imageName]);
console.log(index);
if (index > -1) {
items.splice(index, 1); // Remove one item at the specified index
}
console.log("NEW ARRAY", items, array, imageName)
return docRef.update({
items: items
})
.then(() => {
console.log("Document successfully updated!");
alert("Successfully deleted from cart!");
window.location.href="cart.html";
})
.catch((error) => {
// The document probably doesn't exist.
console.error("Error updating document: ", error);
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
}
function makeCards(name) {
// name = arr[d];
// console.log(name, url);
db.collection("items").doc(name).get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
var cards = document.getElementById("cards");
array.push(name);
cards.innerHTML +=
`<div class="card">
<div class="card__content">
<img class="card__img" style="height: 16rem;" src="${doc.data().url}">
<h1 class="card__header newsreader-800">${doc.data().title} - $${doc.data().price}</h1>
<p class="card__text newsreader-400" >${doc.data().description}</p>
<button class="login-button" onclick="deleteFromCart(${k})">Remove</button>
</div>
</div>`;
k++;
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
}