-
Notifications
You must be signed in to change notification settings - Fork 1
/
food.js
167 lines (123 loc) · 5.31 KB
/
food.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
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
164
165
166
167
//let data = JSON.parse(localStorage.getItem("cardData")) || [];
// console.log(data)
let mainSection = document.querySelector("#container");
fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?a=indian`)
.then((res) => {
return res.json();
})
.then((res) => {
data = res;
console.log(data);
//localStorage.setItem('data', JSON.stringify(data));
let reData = data.meals.map(item => {
return {
name: item.strMeal,
img: item.strMealThumb
}
})
// console.log(reData)
displayData(reData);
// console.log(reData);
});
let cardData = JSON.parse(localStorage.getItem("cardData"))||[];
console.log(cardData)
mainSection = document.querySelector("#container");
fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?a=indian`)
.then((res) => {
return res.json();
})
.then((res) => {
data = res;
console.log(data);
//localStorage.setItem('data', JSON.stringify(data));
let reData = data.meals.map(item => {
return {
name: item.strMeal,
img: item.strMealThumb
}
})
// console.log(reData)
displayData(reData);
// console.log(reData);
});
function displayData(reData) {
let cardlist = `<div class = "card-list">${reData.map(item => getCard(item.name, item.img)).join(" ")}</div>`
let cart = document.querySelector("#addtocartBtn");
mainSection.innerHTML = cardlist;
let a = document.querySelectorAll(".addBtn");
let c = document.querySelectorAll(".images")
let b = document.querySelectorAll(".card-item");
let d = document.querySelectorAll(".card-price");
for(let i = 0; i < a.length; i++){
a[i].addEventListener("click",()=>{
// console.log(c[i].src)
//console.log(d[i].textContent);
// console.log(b[i].textContent)
let obj = {
name : b[i].textContent,
img : c[i].src,
price : d[i].textContent
}
cardData = JSON.parse(localStorage.getItem("cardData"))||[];
console.log("carddata-",cardData)
let id = JSON.parse(localStorage.getItem("id"));
if(id == undefined){
alert("Login First");
return;
}
console.log("id is",id)
let flag = true;
cardData.forEach(element => {
// console.log(element.name, element.price)
if(element.name == obj.name){
flag = false;
}
});
if(flag == false){
alert("Already in cart");
return;
}
console.log(obj.name)
alert("Added to Cart")
cardData.push(obj)
console.log("cardData before setting-",cardData)
localStorage.setItem('cardData', JSON.stringify(cardData));
//BELOW THIS OUR CODE
fetch(`https://63ca391fd0ab64be2b4e8c76.mockapi.io/LoginCredientials/${id}/`).then((res)=>res.json()).then((a)=>{
// console.log(a);
fetch(`https://63ca391fd0ab64be2b4e8c76.mockapi.io/LoginCredientials/${id}/`,{ method: 'PUT',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({AddtoCart:cardData})
}); })
// console.log(a.AddtoCart)})
// ABOVE THIS YOUR CODE
// window.location.reload();
})
// cardData.push(obj)
}
// a.forEach((ele)=>{
// ele.addEventListener("click",()=>{
// alert("hello")
// console.log(ele)
// })
// })
console.log(a);
console.log(cart);
}
function getCard(name, img) {
let card = `
<div class = "card">
<div class = "card-img">
<img class="images" src = ${img} alt= "123" />
</div>
<div class = "card-body">
<h2 class="card-item">${name} </h2>
<p class="card-price"> ${Math.floor(Math.random() * 500)}</p>
<button id="addtocardBtn" class="addBtn">Add to Cart</button >
</div >
</div >
`
return card;
}