Skip to content

Commit

Permalink
Done! Finished the ability to remove items from cart
Browse files Browse the repository at this point in the history
  • Loading branch information
RosalieWessels committed Feb 18, 2024
1 parent 08ccc9b commit a2c52e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
62 changes: 53 additions & 9 deletions cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ storage = firebase.storage()
var storageRef = firebase.storage().ref();

var email = "";
var array = [];
var k = 0;

firebase.auth().onAuthStateChanged((user) => {
if (user) {
Expand Down Expand Up @@ -67,25 +69,67 @@ function getCartItems() {

}

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, url) {
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");
cards.innerHTML +=
`<div class="card">
<div class="card__content">
<img class="card__img" style="height: 16rem;" src="${url}">
<h1 class="card__header newsreader-800">${doc.data().title} - $${doc.data().price}</h1>
<p class="card__text newsreader-400" >${doc.data().description}</p>
</div>
</div>`
array.push(name);
cards.innerHTML +=
`<div class="card">
<div class="card__content">
<img class="card__img" style="height: 16rem;" src="${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);
});
}
}

3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function addToCart(id) {
})
.then(() => {
console.log("Document successfully updated!");
alert("Successfully added to cart!");
})
.catch((error) => {
// The document probably doesn't exist.
Expand All @@ -81,6 +82,7 @@ function addToCart(id) {
})
.then(() => {
console.log("Document successfully written!");
alert("Successfully added to cart!");
})
.catch((error) => {
console.error("Error writing document: ", error);
Expand All @@ -94,6 +96,7 @@ function addToCart(id) {
})
.then(() => {
console.log("Document successfully written!");
alert("Successfully added to cart!");
})
.catch((error) => {
console.error("Error writing document: ", error);
Expand Down

0 comments on commit a2c52e7

Please sign in to comment.