-
Notifications
You must be signed in to change notification settings - Fork 0
/
app qr.js
109 lines (106 loc) · 3.01 KB
/
app qr.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
let openShopping = document.querySelector('.shopping');
let closeShopping = document.querySelector('.closeShopping');
let list = document.querySelector('.list');
let listCard = document.querySelector('.listCard');
let body = document.querySelector('body');
let total = document.querySelector('.total');
let quantity = document.querySelector('.quantity');
openShopping.addEventListener('click', ()=>{
body.classList.add('active');
})
closeShopping.addEventListener('click', ()=>{
body.classList.remove('active');
})
let products = [
{
id: 1,
name: 'PRODUCT NAME 1',
image: 'QR 1.jpg',
price: 2.00,
},
{
id: 2,
name: 'PRODUCT NAME 2',
image: 'QR 1.jpg',
price: 3.00
},
{
id: 3,
name: 'PRODUCT NAME 3',
image: 'QR 2.jpg',
price: 2.00
},
{
id: 4,
name: 'PRODUCT NAME 4',
image: 'QR 3.jpg',
price: 1.00
},
{
id: 5,
name: 'PRODUCT NAME 5',
image: 'QR 4.jpg',
price: 1.
},
{
id: 6,
name: 'PRODUCT NAME 6',
image: 'QR 3.jpg',
price: 1
}
];
let listCards = [];
function initApp(){
products.forEach((value, key) =>{
let newDiv = document.createElement('div');
newDiv.classList.add('item');
newDiv.innerHTML = `
<img src="image/${value.image}">
<div class="title">${value.name}</div>
<div class="price">${value.price.toLocaleString()}</div>
<button onclick="addToCard(${key})">Add To Card</button>`;
list.appendChild(newDiv);
})
}
initApp();
function addToCard(key){
if(listCards[key] == null){
// copy product form list to list card
listCards[key] = JSON.parse(JSON.stringify(products[key]));
listCards[key].quantity = 1;
}
reloadCard();
}
function reloadCard(){
listCard.innerHTML = '';
let count = 0;
let totalPrice = 0;
listCards.forEach((value, key)=>{
totalPrice = totalPrice + value.price;
count = count + value.quantity;
if(value != null){
let newDiv = document.createElement('li');
newDiv.innerHTML = `
<div><img src="image/${value.image}"/></div>
<div>${value.name}</div>
<div>${value.price.toLocaleString()}</div>
<div>
<button onclick="changeQuantity(${key}, ${value.quantity - 1})">-</button>
<div class="count">${value.quantity}</div>
<button onclick="changeQuantity(${key}, ${value.quantity + 1})">+</button>
</div>`;
listCard.appendChild(newDiv);
}
})
total.innerText = totalPrice.toLocaleString();
quantity.innerText = count;
}
function changeQuantity(key, quantity){
if(quantity == 0){
delete listCards[key];
}else{
listCards[key].quantity = quantity;
listCards[key].price = quantity * products[key].price;
}
reloadCard();
}