-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (77 loc) · 2.81 KB
/
server.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
// Function to create a product list item
function createProductItem() {
console.log("Creating")
const productList = document.getElementById('product-list-items');
fetch('../assets/product.json')
.then(response => response.json())
.then(products => {
products.forEach(product => {
const productItem = document.createElement('li');
productItem.innerHTML = `
<h3>${product.name}</h3>
<img src="${product.image}"/>
<h3>Price: ${product.price}$</h3>
<button onclick="loadDetailsPage(${product.id})">View Details</button>
`
productList.appendChild(productItem);
});
});
}
function loadDetailsPage(id){
window.location.href = `product-details.html?productId=${id}`;
}
//Function to display the details of a product
function showProductDetails() {
const urlParams = new URLSearchParams(window.location.search);
const productId = urlParams.get('productId');
const productDetail = document.getElementById('product-details');
fetch(`../assets/product.json`)
.then(response => response.json())
.then(products => products.find(product => product.id === Number(productId)))
.then(product => {
productDetail.innerHTML = `
<h3>${product.name}</h3>
<img src="${product.image}"/>
<h2>Price: ${product.price}$</h2>
<p>${product.description}</p>
<button onclick="addProductToCart(${product.id},'${product.name}','${product.price}')">Add to Cart</button>
`
})
}
// Function to add a product to the cart
function addProductToCart(id, name, price) {
// Get the cart data from localStorage (or create an empty object if it doesn't exist)
let cart = JSON.parse(localStorage.getItem('cart')) || {};
// Check if the product is already in the cart
const existingProduct = cart[id];
if (existingProduct) {
existingProduct.quantity++;
} else {
cart[id] = { id, name, price, quantity: 1 }; // Create new entry with id as key
}
// Update the cart data in localStorage
localStorage.setItem('cart', JSON.stringify(cart));
alert('Product added to cart');
}
//function to show the cart
function showCart() {
const cart = JSON.parse(localStorage.getItem('cart')) || {};
const cartItems = document.getElementById('cart-items');
let totalPrice = 0;
Object.values(cart).forEach(item => {
cartItems.innerHTML += `
<div>
<h3>${item.name}</h3>
<p>Quantity: ${item.quantity}</p>
<p>Price: ${item.quantity * item.price}$</p>
</div>
`;
totalPrice += item.quantity * item.price;
});
cartItems.innerHTML += `
<div>
<h3>Total Price:</h3>
<p>${totalPrice}$</p>
</div>
`;
}