From 9f73b94c96892f57e9c7eb2495a20aaba9c5c3a6 Mon Sep 17 00:00:00 2001 From: Kumar Priyansh Date: Mon, 3 Jun 2019 07:40:55 +0530 Subject: Updated Cart JS --- shared/cart.js | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/shared/cart.js b/shared/cart.js index 4d7b8ea..a81e331 100644 --- a/shared/cart.js +++ b/shared/cart.js @@ -2,6 +2,7 @@ $(document).ready(function(){ $('#cart').click(function(){ window.location.replace('../') }) + function showAlert(message) { // Get the snackbar DIV var x = document.getElementById("snackbar"); @@ -12,6 +13,7 @@ $(document).ready(function(){ // After 3 seconds, remove the show class from DIV setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000); } + $('#signout').click(function(){ firebase.auth().signOut().then(function() { window.location.replace('../') @@ -20,34 +22,47 @@ $(document).ready(function(){ }); }) - var user; - firebase.auth().onAuthStateChanged(function(user) { if (!user) { window.location.replace('../') } - user = firebase.auth().currentUser; - console.log(user) + loadCart(user); - //Checking if Cart is Empty - + }); + + function loadCart(user) { + var user = user var cartRef = firebase.database().ref('userCart/' + user.uid); cartRef.on('value', function(snapshot) { if(snapshot.val() === null) { showEmptyCart(); } - }); + else { + var totalPrice = 0; + $('#loader').empty(); + $("#loader").append('ItemPriceOptions') + for(i = 0; i < Object.keys(snapshot.val()).length; i++) { + // console.log(Object.values(snapshot.val())[i]); + totalPrice += parseFloat(Object.values(snapshot.val())[i].price); + $("#loader").append(''+Object.values(snapshot.val())[i].name+'$'+Object.values(snapshot.val())[i].price+'') + } + $("#loader").append('Total Price$'+totalPrice+'') + } + $('.delClick').on('click', function(event){ + event.preventDefault(); + var id = ($(this).attr('id')) + var cartRef = firebase.database().ref('userCart/' + user.uid + '/' + id) + cartRef.remove(); + loadCart(user); + showAlert("Product Deleted") + }) + }) + } - function showEmptyCart() { - $('#loader').html('

Your Cart is Empty

Click here to shop for some products.

'); - showAlert('Your cart is empty. Go add something.') - } + function showEmptyCart() { + $('#loader').html('

Your Cart is Empty

Click here to shop for some products.

'); + showAlert('Your cart is empty. Go add something.') + } - }); - - - - - }) \ No newline at end of file -- cgit v1.2.3