-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
130 lines (112 loc) · 2.39 KB
/
cart.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
let api1="https://63c63b14d307b7696735016b.mockapi.io/product"
let api2="https://63c63b14d307b7696735016b.mockapi.io/product?sortBy=price&order=desc"
api3="https://63c63b14d307b7696735016b.mockapi.io/product?sortBy=price&order=asc"
let dis=document.getElementById("main_cart")
let arr=[]
let filter=document.getElementById("filter")
filter.addEventListener("change",()=>{
if(filter.value==""){
fapi1()
}else{
if(filter.value=="high"){
fapi3()
}else{
fapi2()
}
}
})
fapi1()
function fapi1(){
fetch(api1)
.then((request)=>{
return request.json()
})
.then((data)=>{
console.log(data)
arr=data
check(data)
})
.catch((err)=>{
console.log(err)
})
}
function fapi2(){
fetch(api2)
.then((request)=>{
return request.json()
})
.then((data)=>{
console.log(data)
arr=data
check(data)
})
.catch((err)=>{
console.log(err)
})
}
function fapi3(){
fetch(api3)
.then((request)=>{
return request.json()
})
.then((data)=>{
console.log(data)
arr=data
check(data)
})
.catch((err)=>{
console.log(err)
})
}
let inp=document.querySelector("form")
inp.addEventListener("submit",(e)=>{
e.preventDefault()
let search=document.getElementById("scarch").value
let f=arr.filter(element=>{
if(element.name.toUpperCase().includes(search.toUpperCase())==true){
return true
}else{
return false
}
})
check(f)
console.log(f)
})
let count1=document.querySelector(".count1")
let arr5=JSON.parse(localStorage.getItem("cart"))||[]
function check(data){
dis.innerHTML=null
data.forEach(element => {
let div=document.createElement("div")
let img=document.createElement("img")
img.src=element.avatar
let h4=document.createElement("h4")
h4.innerText="RaxCart"
let p=document.createElement("p")
p.innerText=element.name
let p1=document.createElement("h1")
p1.innerText="₹"+element.price
let button =document.createElement("button")
button.innerText="Add To Cart"
count1.innerText=data.length
button.addEventListener("click",()=>{
if(checkduplicate(element)){
alert("Product already in cart")
}else{
arr5.push({...element,quentity:1})
localStorage.setItem("cart",JSON.stringify(arr5))
alert("Product add to cart")
}
})
div.append(img,h4,p,p1,button)
dis.append(div)
});
}
function checkduplicate(element){
for(let i=0;i<arr5.length;i++){
if(arr5[i].id==element.id){
return true
}
}
return false
}