forked from Anjaliavv51/Retro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3b990f
commit 25d4ea8
Showing
10 changed files
with
2,909 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
function addItemToCart() { | ||
var itemName = localStorage.getItem('itemName'); | ||
var itemPrice = localStorage.getItem('itemPrice'); | ||
addToCart(itemName, itemPrice); | ||
} | ||
|
||
const addToCart = function(name, price){ | ||
let cartItems = localStorage.getItem('cartItems'); | ||
cartItems = cartItems ? JSON.parse(cartItems) : []; | ||
if(name==null && price==null) return; | ||
const existingItem = cartItems.find(item => item.name === name); | ||
if (!existingItem) { | ||
cartItems.push({ name, price }); | ||
localStorage.setItem('cartItems', JSON.stringify(cartItems)); | ||
console.log(cartItems); | ||
} | ||
|
||
updateCartDisplay(); | ||
calculateBill(); | ||
|
||
} | ||
|
||
const updateCartDisplay = function() { | ||
const cartBody = document.querySelector(".items"); | ||
cartBody.innerHTML = ''; | ||
let cartItems = localStorage.getItem('cartItems'); | ||
cartItems = cartItems ? JSON.parse(cartItems) : []; | ||
|
||
cartItems.forEach(item => { | ||
const cartRow = document.createElement("tr"); | ||
const cartItemName = document.createElement("td"); | ||
const cartItemPrice = document.createElement("td"); | ||
cartItemName.innerText = item.name; | ||
cartItemPrice.innerText = item.price; | ||
cartItemPrice.classList.add("price"); | ||
cartRow.appendChild(cartItemName); | ||
cartRow.appendChild(cartItemPrice); | ||
cartBody.appendChild(cartRow); | ||
}); | ||
} | ||
|
||
|
||
// calculate total bill amount | ||
let total = 0; | ||
const calculateBill = ()=>{ | ||
itemPrices = document.querySelectorAll(".price"); | ||
for (p of itemPrices){ | ||
if (p!=null){ | ||
console.log(p.innerText); | ||
total += parseFloat(p.innerText.replace('$','')); | ||
} | ||
} | ||
|
||
console.log(total); | ||
if(total!=0 && !isNaN(total)){ | ||
document.getElementById("bill").innerText = "$" + total.toFixed(2) | ||
} | ||
|
||
} | ||
|
||
document.addEventListener('DOMContentLoaded', function () { | ||
addItemToCart(); | ||
}); | ||
|
||
let orderBtn = document.querySelector(".butt"); | ||
orderBtn.addEventListener("click", ()=>{ | ||
if(total==0){ | ||
alert("Please add something in the cart to place the order"); | ||
} | ||
else{ | ||
|
||
alert("Order placed!"); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// adding items from menu to cart | ||
document.addEventListener('DOMContentLoaded', function () { | ||
var menuContainers = document.querySelectorAll('.menu_container'); | ||
menuContainers.forEach(function (container) { | ||
container.addEventListener('click', function (event) { | ||
if (event.target.classList.contains('butt')) { | ||
var item = event.target.closest('.items'); | ||
var itemName = item.querySelector('h3').textContent; | ||
var itemPrice = item.querySelector('p').textContent; | ||
alert("Item added to cart successfully"); | ||
|
||
localStorage.setItem('itemName', itemName); | ||
localStorage.setItem('itemPrice', itemPrice); | ||
|
||
window.location.href = "cart.html"; | ||
} | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.