forked from Rajat24111994/flower.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wish.js
119 lines (94 loc) · 2.87 KB
/
wish.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
// Localstorage
let cart=JSON.parse(localStorage.getItem('cart'))||[]
let wish=JSON.parse(localStorage.getItem('wish'))||[]
let Cart = JSON.parse(localStorage.getItem("cart")) || [];
// Fetching the data
let container=document.getElementById("container")
display(wish)
function display(wish){
container.innerHTML=""
wish.forEach((el,ind) => {
// Creating
let box=document.createElement("div")
let box2=document.createElement("div")
let box3=document.createElement("div")
let img=document.createElement("img")
let title=document.createElement("h2")
let price=document.createElement("h3")
let btn=document.createElement("button")
let btn2=document.createElement("button")
// Assigning Data
img.src=el.image
title.innerText=el.title
price.innerText=`₹ ${el.price}`
btn.innerText="Add to Cart"
btn2.innerText="Remove"
// Clssess
box.className="box";
box3.id="wish";
//Event Listner
btn.addEventListener("click",()=>{
if(checkdub(el)){
Swal.fire({
position: 'center',
icon: 'error',
title: 'Product Already in Cart',
showConfirmButton: false,
timer: 1500
})
}else{
Swal.fire({
position: 'center',
icon: 'success',
title: 'Product Added To Cart',
showConfirmButton: false,
timer: 1500
})
cart.push({...el,quantity:1})
localStorage.setItem("cart",JSON.stringify(cart))
wish.splice(ind,1)
localStorage.setItem("wish",JSON.stringify(wish))
display(wish)
}
})
btn2.addEventListener("click",()=>{
Swal.fire({
position: 'center',
icon: 'success',
title: 'Removed',
showConfirmButton: false,
timer: 1500
})
wish.splice(ind,1)
localStorage.setItem("wish",JSON.stringify(wish))
display(wish)
})
// Appending to Main
box2.append(img)
box3.append(btn,btn2)
box.append(box2,title,price,box3)
container.append(box)
});
}
let Total=document.getElementById("cartTotal")
Total.innerText=cart.length
let user = document.querySelector(".signin");
user.onclick = () => {
location.href = "./login.html";
};
let order = document.querySelector(".orders");
order.onclick = () => {
location.href = "./cart.html";
};
let bag = document.querySelector(".bag");
bag.onclick = () => {
location.href = "./cart.html";
};
function checkdub(data){
for(i=0;i<cart.length;i++){
if(cart[i].id==data.id){
return true
}
}
return false
}