-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (100 loc) · 3.97 KB
/
index.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
document.querySelector('#searchBtn').addEventListener('click', function(e) {
e.preventDefault();
getData();
});
let getData = async () => {
let searchInputTxt = document.querySelector('#search').value;
try {
let response = await fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?i=${searchInputTxt}`);
// let response = await fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?a=${searchInputTxt}`);
let data = await response.json();
// console.log(data.meals);
append(data.meals);
} catch (error) {
console.log(error);
alert('No Data Found');
}
}
let getData2 = async () => {
try {
let response = await fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?a=indian`);
let data = await response.json();
// console.log(data.meals);
append(data.meals);
} catch (error) {
console.log(error);
}
}
getData2()
// strMeal
// strMealThumb
let foodArr = JSON.parse(localStorage.getItem('food')) || [];
let likeArr = JSON.parse(localStorage.getItem('like')) || [];
const append =(data)=>{
let sup = document.querySelector('sup');
sup.innerHTML = foodArr.length;
let notif = document.querySelector('.notification');
let notifd = document.querySelector('.notifDiv');
document.getElementById('show').innerHTML = '';
let price = Math.floor(Math.random() * 500);
data.forEach(el => {
let div = document.createElement('div');
let img = document.createElement('img');
img.src = el.strMealThumb;
let div2 = document.createElement('div');
div2.classList.add('text')
let p = document.createElement('h3');
p.innerHTML = el.strMeal;
let p2 = document.createElement('p');
p2.innerHTML =`<i class="fa-solid fa-heart"></i>`
p2.classList.add('heart')
p2.addEventListener('click', function(e) {
let timer = setInterval(() => {
notif.innerHTML =el.strMeal+ ' 👍';
notifd.style.display = 'block';
}, 10);
setTimeout(() => {
notifd.style.display = 'none';
clearInterval(timer);
}, 2000);
p2.style.color = 'oranged';
let foodObj = {
name: el.strMeal,
price: parseInt(price.innerHTML),
img: el.strMealThumb
}
likeArr.push(foodObj);
localStorage.setItem('like', JSON.stringify(likeArr));
});
div2.append(p,p2);
let div3 = document.createElement('div');
div3.classList.add('price');
let price = document.createElement('p');
// <i class="fa-solid fa-rupee-sign"></i>.
price.innerHTML = `${Math.floor(Math.random() * 500)}`;
let cart = document.createElement('button');
// cart.classList.add('cssbuttons-io-button');
cart.innerHTML = `Add to Cart`;
cart.addEventListener('click', function(e) {
let timer = setInterval(() => {
notif.innerHTML =el.strMeal+ ' <b>Added to Cart</b>';
notifd.style.display = 'block';
}, 300);
setTimeout(() => {
notifd.style.display = 'none';
clearInterval(timer);
}, 2000);
let foodObj = {
name: el.strMeal,
price: parseInt(price.innerHTML),
img: el.strMealThumb
}
foodArr.push(foodObj);
localStorage.setItem('food', JSON.stringify(foodArr));
});
div3.append(price,cart);
div.append(img,div2,div3);
document.getElementById('show').append(div);
});
}
// -------------------- other section