-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
310 lines (272 loc) · 11.9 KB
/
Copy pathscript.js
File metadata and controls
310 lines (272 loc) · 11.9 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Code is written by CHIRAG RAJ
"use strict";
// Get HTML element
let search_box = document.getElementById("search_box");
let main = document.getElementById("main");
let container = document.getElementById("container");
let favourite_btn = document.getElementById("favourite_btn");
let fav_exit = document.getElementById("EXIT");
let fav_body = document.getElementById("fav_body");
let heart = []; // it store dom of heart icon
let view = 0;
let btn_array = [];
//set array in local storage for store id of meal
if (localStorage.getItem("meals_id_array") === null) {
let meals_id = [];
localStorage.setItem("meals_id_array", JSON.stringify(meals_id));
}
//Create Array
let object_array = [];
//show alert
function show_alert(text) {
alert(text);
}
//add event listener to the search box
search_box.addEventListener("keyup", find_Recipes);
// fetch the food data from API
function find_Recipes() {
let search_value = search_box.value;
// Get request
fetch("https://www.themealdb.com/api/json/v1/1/search.php?s=" + search_value)
.then(function (response) {
return response.json();
})
.then(function (data) {
// copy the fetch data to array
object_array = data;
// call render_cards functions and pass object_array
render_cards(object_array);
})
.catch(function () {
main.innerHTML = `<div id="error1">
<p id="sad">😢</p>
<p>There is no recipe matching your search</p>
</div>`;
})
}
// Run a Loop and call append_cards functions
function render_cards(object_array) {
//clear cards in each char next search
main.innerHTML = "";
let length = object_array.meals.length;
for (let i = 0; i < length; i++) {
append_cards(object_array.meals[i]);
}
}
// set card in HTML
let card_btn_array = [];
let index = 0;
function append_cards(object) {
let mealCard = document.createElement("div");
mealCard.classList.add("food_card");
mealCard.innerHTML = `
<div class="card_img_div">
<img class="card_img" src = "${object.strMealThumb}"/>
</div >
<p class="card_text_para">${object.strMeal}</p>
<div class="card_lower_div">
<button id="${object.idMeal}" class="btn">View Recipe</button>
<span id="${object.idMeal}1" class="material-symbols-sharp"> favorite </span>
</div>`;
//add 1 in id = ${object.idMeal} becuse i want to make all id unique for heart
main.append(mealCard);
//set heart as red color if it is in fav section
// search in local storage for card is in fav section or not
let mls_id = JSON.parse(localStorage.getItem("meals_id_array"));
for (let i = 0; i < mls_id.length; i++) {
if (mls_id[i].idMeal === object.idMeal) {
let HEART_ID = `${object.idMeal}1`;
let element = document.getElementById(HEART_ID);
element.style.color = "red";
}
}
// for card view btn
card_btn_array[index] = document.getElementById(`${object.idMeal}`);
// add event listner for evry card button
card_btn_array[index].addEventListener("click", Recipe_container);
// for card heart
heart[index] = document.getElementById(`${object.idMeal}1`);
// add event listner for evry heart or fav button
heart[index].addEventListener("click", add_to_fav);
index++;
}
// Add recipes card to fav section
function add_to_fav(event) {
// search in local storage for card is in fav section or not
let mls_id = JSON.parse(localStorage.getItem("meals_id_array"));
for (let i = 0; i < mls_id.length; i++) {
if ((event.target.id).slice(0, -1) === mls_id[i].idMeal) {
show_alert("🧐 Your Meal Recipe already exists in your favourites list");
return;
// dont want to go downside of code if it return
}
}
// find card whaich have to add in fav section
mls_id = mls_id.concat(object_array.meals.filter(function (object) {
return object.idMeal === (event.target.id).slice(0, -1);
}));
localStorage.setItem("meals_id_array", JSON.stringify(mls_id));
localStorage_fetch();
show_alert("😋 Your Meal Recipe has been added to your favourites list");
// Add red color to heart
(event.target).style.color = "red";
}
// work on View Recipe
function Recipe_container(event) {
if (btn_array.includes(event.target.id) === true) {
show_alert("🔒 Your Meal Recipe is already open\nOR\nClose the Recipes INSTRUCTIONS Page for reaching to your desired page");
}
else {
btn_array.push(event.target.id);
view++;
// main.innerHTML = ""; // option1
main.style.visibility = "hidden"; // alternate option 2
// filter_array have 1 object whose meal id match with button target id
let filter_array = object_array.meals.filter(function (object) {
return object.idMeal === event.target.id;
});
let Recipe_div = document.createElement("div");
Recipe_div.classList.add("Recipe_card");
Recipe_div.innerHTML = `
<div id="left">
<div id="left_upper">
<img id="left_upper_img"src="${filter_array[0].strMealThumb}" alt="error">
<p id="left_upper_p1">${filter_array[0].strMeal}</p>
<p id="left_upper_p2">Cuisine : ${filter_array[0].strArea}</p>
</div>
<div id="left_lower">
<a href="${filter_array[0].strYoutube}" target="_blank"><button id="left_lower_btn">Watch Video</button></a>
</div>
</div>
<div id="right">
<span id="${(event.target.id)}5" class="cross material-symbols-outlined">cancel</span>
<h3 id="right_inst">INSTRUCTIONS</h3>
<p id="right_p">${filter_array[0].strInstructions}</p>
</div>`;
container.append(Recipe_div);
let cross = document.getElementsByClassName("cross");
cross[0].addEventListener("click", exit_page);
}
}
function exit_page(event) {
//delete div on click of cross
const index = btn_array.indexOf(event.target.id.slice(0, -1));
btn_array.splice(index, 1);
view--;
let recipes_container_div = document.getElementsByClassName("Recipe_card");
recipes_container_div[recipes_container_div.length - 1].remove();
if (view === 0) {
main.style.visibility = "visible";
}
}
// work on favourite_section
favourite_btn.addEventListener("click", fav_page);
function fav_page() {
container.style.filter = "brightness(50%)";
let favourite_container = document.getElementById("favourite_container");
favourite_container.style.right = "0vw";
fav_exit.addEventListener("click", EXIT);
function EXIT() {
favourite_container.style.right = "-360px";
container.style.filter = "brightness(100%)";
}
localStorage_fetch();
}
// fetch local storage for render page
function localStorage_fetch() {
let localStorage_length = JSON.parse(localStorage.getItem("meals_id_array")).length;
let meals_id_array = JSON.parse(localStorage.getItem("meals_id_array"));
if (localStorage_length === 0) {
fav_body.innerHTML = "<h2>No recipes added in your favourites list.</h2>";
}
else {
fav_body.innerHTML = "";
for (let i = 0; i < localStorage_length; i++) {
//set card in fav div
let mealCard = document.createElement("div");
mealCard.classList.add("food_card");
mealCard.innerHTML = `
<div class="card_img_div">
<img class="card_img" src = "${meals_id_array[i].strMealThumb}"/>
</div>
<p class="card_text_para">${meals_id_array[i].strMeal}</p>
<div class="card_lower_div_fav">
<button id="${meals_id_array[i].idMeal}2" class="btn1">View</button>
<button id="${meals_id_array[i].idMeal}3" class="btn1">Remove</button>
</div>`;
//add 1 in id = ${object.idMeal} becuse i want to make all id unique for heart
fav_body.append(mealCard);
// for card view btn
card_btn_array[index] = document.getElementById(`${meals_id_array[i].idMeal}2`);
// add event listner for evry card button
card_btn_array[index].addEventListener("click", Recipe_container1);
// for card remove
heart[index] = document.getElementById(`${meals_id_array[i].idMeal}3`);
// add event listner for evry remove button
heart[index].addEventListener("click", remove_from_fav);
index++;
}
}
}
// work on View Recipe
function Recipe_container1(event) {
if (btn_array.includes((event.target.id).slice(0, -1)) === true) {
show_alert("🔒 Your Meal Recipe is already open\nOR\nClose the Recipes INSTRUCTIONS Page for reaching to your desired page");
}
else {
btn_array.push((event.target.id).slice(0, -1));
view++;
// main.innerHTML = ""; // option1
main.style.visibility = "hidden"; // alternate option 2
// filter_array have 1 object whose meal id match with button target id
let filter_array = JSON.parse(localStorage.getItem("meals_id_array")).filter(function (object) {
return object.idMeal === (event.target.id).slice(0, -1);
});
let Recipe_div = document.createElement("div");
Recipe_div.classList.add("Recipe_card");
Recipe_div.innerHTML = `
<div id="left">
<div id="left_upper">
<img id="left_upper_img"src="${filter_array[0].strMealThumb}" alt="error">
<p id="left_upper_p1">${filter_array[0].strMeal}</p>
<p id="left_upper_p2">Cuisine : ${filter_array[0].strArea}</p>
</div>
<div id="left_lower">
<a href="${filter_array[0].strYoutube}" target="_blank"><button id="left_lower_btn">Watch Video</button></a>
</div>
</div>
<div id="right">
<span id="${(event.target.id).slice(0, -1)}4" class="cross material-symbols-outlined">cancel</span>
<h3 id="right_inst">INSTRUCTIONS</h3>
<p id="right_p">${filter_array[0].strInstructions}</p>
</div>`;
container.append(Recipe_div);
let cross = document.getElementsByClassName("cross");
for (let i = cross.length - 1; i >= 0; i--) {
cross[i].addEventListener("click", exit_page);
}
}
}
// remove recipes card from_fav section
function remove_from_fav(event) {
// search in local storage for card is in fav section or not
let mls_id = JSON.parse(localStorage.getItem("meals_id_array"));
for (let i = 0; i < mls_id.length; i++) {
if (mls_id[i].idMeal === event.target.id.slice(0, -1)) {
mls_id.splice(i, 1);
}
}
localStorage.setItem("meals_id_array", JSON.stringify(mls_id));
localStorage_fetch();
show_alert("🤪 Your Meal Recipe has been removed from your favourites list");
// Remove red color from heart
//fetch or find hert element by their id
let HEART_ID = event.target.id.slice(0, -1) + 1; // add 1 because heart id is same as target id + 1.
let element = document.getElementById(HEART_ID);
// checking here because if browser is refresh and main div doesn't have any card so, it not set a id in heart so it through error in console that in element there is null.
if (element !== null) {
element.style.color = "black";
}
// else we normaly remove card from fav div and it dirctly set black color on heart. it is because when main container render card on serach btn id is not present in local storage
}
// End code