forked from gauravkrs/Blinkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.html
163 lines (138 loc) · 4.96 KB
/
new.html
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="icon" href="https://blinkit.com/images/apple-touch-icon-change-60x60-1be1e81.png">
<style>
#cont{
display: flex;
}
#cartPartAppendingM{
position: absolute;
height: 100vh;
width: 25vw;
border: 1px solid black;
right:0;
top: 0;
overflow: scroll;
}
</style>
</head>
<body>
<div id="main"></div>
</body>
</html>
<script>
let cartDB1 = JSON.parse(localStorage.getItem("cartDB")) || [];
console.log(cartDB1)
// <-Html to append to lacation
let main = document.getElementById("cartPartAppendingM");
let mycarttop = document.createElement("div")
mycarttop.innerText="My Cart";
mycarttop.setAttribute("id","mycarttop")
let mycartbottom = document.createElement("div")
mycartbottom.setAttribute("id","mycartbottom")
const display = (data) => {
mycartbottom.innerHTML = null;
if(data.length==0){
mycartbottom.innerHTML= ` <div id="emptycart">
<div class="emptycartimage">
<img src="https://cdn.grofers.com/assets/ui/empty_states/emp_empty_cart.png" alt="emptycart"/>
</div>
<h3>you don't have any items in your cart</h3>
<p>your favourite items are just a click away</p>
<button id="emptycartbutton" onclick="cartvisibilityhidden()">start shopping</button>
</div>`
}
data.forEach((el,index,array) => {
let div = document.createElement("div");
div.setAttribute("id", "contentttG")
let imgDiv = document.createElement("div");
let img = document.createElement("img");
// img.setAttribute("class","pimg")
img.src = el.img;
// let add = document.createElement("button");
// add.setAttribute("id","addcart")
imgDiv.append(img)
let nameDiv = document.createElement("div");
nameDiv.setAttribute("class", "info")
let name = document.createElement("p");
name.innerText = el.name
let qty = document.createElement("p");
qty.innerText = el.quantity
let priceDiv = document.createElement("div");
priceDiv.setAttribute("class", "price")
let price = document.createElement("span")
price.innerText = `₹${el.price}`;
let strikePrice = document.createElement("span");
strikePrice.innerText = `₹${el.sPrice}`;
let btnDiv = document.createElement("div")
btnDiv.setAttribute("id",'addremove')
let lBtn = document.createElement("button");
lBtn.innerText="-"
lBtn.addEventListener("click",()=>{
// console.log(data[index])
decreaseQty(data,index)
})
let qtyL = document.createElement("span");
qtyL.setAttribute("class","nummberOfElement")
qtyL.innerText=el.quantity;
let rBtn = document.createElement("button");
rBtn.innerText="+"
rBtn.addEventListener("click",()=>{
increaseQty(data,index)
})
btnDiv.append(lBtn,qtyL,rBtn)
imgDiv.append(img)
priceDiv.append(price, strikePrice);
nameDiv.append(name,priceDiv);
div.append(imgDiv, nameDiv,btnDiv)
mycartbottom.append(div);
})
main.append(mycarttop,mycartbottom)
}
display(cartDB1)
function decreaseQty(data,index){
if(data[index].quantity>0){
data[index].quantity--;
if(data[index].quantity==0){
data.splice([index],1)
}
localStorage.setItem("cartDB",JSON.stringify(data))
display(data)
}
}
function increaseQty(data,index){
if(data[index].quantity<3){
data[index].quantity++;
localStorage.setItem("cartDB",JSON.stringify(data))
display(data)
}else{
alert("Sorry! you can't add more of this item")
}
}
// <<<<<<-------function cart value--->
totalStrikedP();
totalPrice()
function totalStrikedP(){
let str=data.reduce(function(element,acc){
return acc+element.sPrice*element.quqntity
},0)
let striked = document.querySelector(".sTRIKED");
striked.innerText="";
striked.innerText=str
}
function totalPrice(){
let str=data.reduce(function(element,acc){
return acc+element.price*element.quqntity
},0)
let striked = document.querySelector(".pRICE");
striked.innerText=""
striked.innerText=str;
}
function cartvisibilityhidden(){
}
</script>