-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (124 loc) · 4.42 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
let iconCart = document.querySelector('.iconCart');
let cart = document.querySelector('.cart');
let container = document.querySelector('.container');
let close = document.querySelector('.close');
iconCart.addEventListener('click', function(){
if(cart.style.right == '-100%'){
cart.style.right = '0';
container.style.transform = 'translateX(-400px)';
}else{
cart.style.right = '-100%';
container.style.transform = 'translateX(0)';
}
})
close.addEventListener('click', function (){
cart.style.right = '-100%';
container.style.transform = 'translateX(0)';
})
let products = null;
// get data from file json
fetch('product.json')
.then(response => response.json())
.then(data => {
products = data;
addDataToHTML();
})
//show datas product in list
function addDataToHTML(){
// remove datas default from HTML
let listProductHTML = document.querySelector('.listProduct');
listProductHTML.innerHTML = '';
// add new datas
if(products != null) // if has data
{
products.forEach(product => {
let newProduct = document.createElement('div');
newProduct.classList.add('item');
newProduct.innerHTML =
`<img src="${product.image}" alt="">
<h2>${product.name}</h2>
<div class="price">Rs${product.price}</div>
<button onclick="addCart(${product.id})">Add To Cart</button>`;
listProductHTML.appendChild(newProduct);
});
}
}
//use cookie so the cart doesn't get lost on refresh page
let listCart = [];
function checkCart(){
var cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('listCart='));
if(cookieValue){
listCart = JSON.parse(cookieValue.split('=')[1]);
}else{
listCart = [];
}
}
checkCart();
function addCart($idProduct){
let productsCopy = JSON.parse(JSON.stringify(products));
//// If this product is not in the cart
if(!listCart[$idProduct])
{
listCart[$idProduct] = productsCopy.filter(product => product.id == $idProduct)[0];
listCart[$idProduct].quantity = 1;
}else{
//If this product is already in the cart.
//I just increased the quantity
listCart[$idProduct].quantity++;
}
document.cookie = "listCart=" + JSON.stringify(listCart) + "; expires=Thu, 31 Dec 2025 23:59:59 UTC; path=/;";
addCartToHTML();
}
addCartToHTML();
function addCartToHTML(){
// clear data default
let listCartHTML = document.querySelector('.listCart');
listCartHTML.innerHTML = '';
let totalHTML = document.querySelector('.totalQuantity');
let totalQuantity = 0;
// if has product in Cart
if(listCart){
listCart.forEach(product => {
if(product){
let newCart = document.createElement('div');
newCart.classList.add('item');
newCart.innerHTML =
`<img src="${product.image}">
<div class="content">
<div class="name">${product.name}</div>
<div class="price">Rs${product.price} / 1 product</div>
</div>
<div class="quantity">
<button onclick="changeQuantity(${product.id}, '-')">-</button>
<span class="value">${product.quantity}</span>
<button onclick="changeQuantity(${product.id}, '+')">+</button>
</div>`;
listCartHTML.appendChild(newCart);
totalQuantity = totalQuantity + product.quantity;
}
})
}
totalHTML.innerText = totalQuantity;
}
function changeQuantity($idProduct, $type){
switch ($type) {
case '+':
listCart[$idProduct].quantity++;
break;
case '-':
listCart[$idProduct].quantity--;
// if quantity <= 0 then remove product in cart
if(listCart[$idProduct].quantity <= 0){
delete listCart[$idProduct];
}
break;
default:
break;
}
// save new data in cookie
document.cookie = "listCart=" + JSON.stringify(listCart) + "; expires=Thu, 31 Dec 2025 23:59:59 UTC; path=/;";
// reload html view cart
addCartToHTML();
}