-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
414 lines (337 loc) · 13.1 KB
/
script.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
document.addEventListener('load', ()=>{
updateTask();
})
/**
* Get the DOM elements for toggle button, sidebar, flex-box, searchbar, dbObjectFavList, and dbLastInput
*/
const toggleButton = document.getElementById("toggle-sidebar");
const sidebar = document.getElementById("sidebar");
const flexBox = document.getElementById('flex-box');
const searchbar = document.getElementById('search-bar');
/**
* Check and initialize the local storage items for favorite list and last input
*/
const dbObjectFavList = "favouritesList";
if (localStorage.getItem(dbObjectFavList) == null) {
localStorage.setItem(dbObjectFavList, JSON.stringify([]));
}
/**
* Update the task counter with the current number of items in the favorite list.
*/
function updateTask() {
const favCounter = document.getElementById('total-counter');
const db = JSON.parse(localStorage.getItem(dbObjectFavList));
if (favCounter.innerText != null) {
favCounter.innerText = db.length;
}
}
/**
* Check if an ID is in a list of favorites
*
* @param list The list of favorites
* @param id The ID to check
* @return true if the ID is in the list, false otherwise
*/
function isFav(list, id) {
let res = false;
for (let i = 0; i < list.length; i++) {
if (id == list[i]) {
res = true;
}
}
return res;
}
/**************************** Some Usefull Utility Function***************************** */
/**
* It return truncated string greater then 50
* @param {*} str
* @param {*} n
* @returns
*/
function truncate(str, n) {
return str?.length > n ? str.substr(0, n - 1) + "..." : str;
}
/**
* Generates a random character string starting
* @returns {string} The generated string
*/
function generateOneCharString() {
var possible = "abcdefghijklmnopqrstuvwxyz";
return possible.charAt(Math.floor(Math.random() * possible.length));
}
/**
* Function to toggle the sidebar and display the list of favorite meals.
* When the toggle button is clicked, the sidebar is shown or hidden and the list of favorite meals is displayed.
* The flexBox class is also toggled to adjust the layout of the page.
*
*/
toggleButton.addEventListener("click", function () {
showFavMealList();
sidebar.classList.toggle("show");
flexBox.classList.toggle('shrink');
});
/**
*
* This function adds an event listener to the toggle button that when clicked, it calls the showFavMealList function and adds or removes the "show" class to the sidebar element and "shrink" class to the flexBox element, respectively.
* @event toggleButton - The button element that when clicked, triggers the event listener.
* @function showFavMealList - The function that is called when the toggle button is clicked. It populates the fav element with the list of favorite meals.
* @element sidebar - The sidebar element that has the "show" class added or removed.
* @element flexBox - The flexbox element that has the "shrink" class added or removed.
*/
flexBox.onscroll = function () {
if (flexBox.scrollTop > searchbar.offsetTop) {
searchbar.classList.add("fixed");
} else {
searchbar.classList.remove("fixed");
}
};
/**
* Fetch meals from API
*
* @param {string} url - The base URL for the API
* @param {string} value - The value to append to the URL for filtering the results
*
* @returns {Promise} A promise that resolves to the JSON data of the meals
*/
const fetchMealsFromApi = async (url, value) => {
const response = await fetch(`${url + value}`);
const meals = await response.json();
return meals;
}
/**
* showMealList - function to show meal list based on search input
*
* @returns {void}
*
* This function first retrieves the data from local storage and then it fetches the meals data from API
* using the fetchMealsFromApi function. It then maps over the meals data and creates the HTML template
* for each meal. This HTML template is then added to the DOM.
*/
async function showMealList() {
const list = JSON.parse(localStorage.getItem(dbObjectFavList));
const inputValue = document.getElementById("search-input").value;
const url = "https://www.themealdb.com/api/json/v1/1/search.php?s=";
const mealsData = await fetchMealsFromApi(url, inputValue);
let html = '';
if (mealsData.meals) {
html = mealsData.meals.map(element => {
return `
<div class="card">
<div class="card-top" onclick="showMealDetails(${element.idMeal}, '${inputValue}')">
<div class="dish-photo" >
<img src="${element.strMealThumb}" alt="">
</div>
<div class="dish-name">
${element.strMeal}
</div>
<div class="dish-details">
${truncate(element.strInstructions, 50)}
<span class="button" onclick="showMealDetails(${element.idMeal}, '${inputValue}')">Know More</span>
</div>
</div>
<div class="card-bottom">
<div class="like">
<i class="fa-solid fa-heart ${isFav(list, element.idMeal) ? 'active' : ''} " onclick="addRemoveToFavList(${element.idMeal})"></i>
</div>
<div class="play">
<a href="${element.strYoutube}">
<i class="fa-brands fa-youtube"></i>
</a>
</div>
</div>
</div>
`
}).join('');
document.getElementById('cards-holder').innerHTML = html;
}
}
/**
* addRemoveToFavList - function to add or remove a meal from the favorite list
*
* @param {string} id - The id of the meal to be added or removed
*
* This function first retrieves the data from local storage and then it checks if the provided meal id already exist in the favorite list.
* If it exists, it removes it from the list, otherwise it adds it to the list. It then updates the local storage and updates the UI.
*/
function addRemoveToFavList(id) {
const detailsPageLikeBtn = document.getElementById('like-button');
let db = JSON.parse(localStorage.getItem(dbObjectFavList));
let ifExist = false;
for (let i = 0; i < db.length; i++) {
if (id == db[i]) {
ifExist = true;
}
} if (ifExist) {
db.splice(db.indexOf(id), 1);
} else {
db.push(id);
}
localStorage.setItem(dbObjectFavList, JSON.stringify(db));
if (detailsPageLikeBtn != null) {
detailsPageLikeBtn.innerHTML = isFav(db, id) ? 'Remove From Favourite' : 'Add To Favourite';
}
showMealList();
showFavMealList();
updateTask();
}
/**
* Show details for a specific meal
* @async
* @function
* @param {string} itemId - The ID of the meal to show details for
* @param {string} searchInput - The search input used to fetch the related meals
*/
async function showMealDetails(itemId, searchInput) {
console.log("searchInput:...............", searchInput);
const list = JSON.parse(localStorage.getItem(dbObjectFavList));
flexBox.scrollTo({ top: 0, behavior: "smooth" });
const url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=";
const searchUrl = "https://www.themealdb.com/api/json/v1/1/search.php?s=";
const mealList = await fetchMealsFromApi(searchUrl,searchInput);
console.log('mealslist:..........',mealList);
let html = ''
const mealDetails = await fetchMealsFromApi(url, itemId);
if (mealDetails.meals) {
html = `
<div class="container remove-top-margin">
<div class="header hide">
<div class="title">
Let's Eat Something New
</div>
</div>
<div class="fixed" id="search-bar">
<div class="icon">
<i class="fa-solid fa-search "></i>
</div>
<div class="new-search-input">
<form onkeyup="showMealList()">
<input id="search-input" type="text" placeholder="Search food, receipe" />
</form>
</div>
</div>
</div>
<div class="item-details">
<div class="item-details-left">
<img src=" ${mealDetails.meals[0].strMealThumb}" alt="">
</div>
<div class="item-details-right">
<div class="item-name">
<strong>Name: </strong>
<span class="item-text">
${mealDetails.meals[0].strMeal}
</span>
</div>
<div class="item-category">
<strong>Category: </strong>
<span class="item-text">
${mealDetails.meals[0].strCategory}
</span>
</div>
<div class="item-ingrident">
<strong>Ingrident: </strong>
<span class="item-text">
${mealDetails.meals[0].strIngredient1},${mealDetails.meals[0].strIngredient2},
${mealDetails.meals[0].strIngredient3},${mealDetails.meals[0].strIngredient4}
</span>
</div>
<div class="item-instruction">
<strong>Instructions: </strong>
<span class="item-text">
${mealDetails.meals[0].strInstructions}
</span>
</div>
<div class="item-video">
<strong>Video Link:</strong>
<span class="item-text">
<a href="${mealDetails.meals[0].strYoutube}">Watch Here</a>
</span>
<div id="like-button" onclick="addRemoveToFavList(${mealDetails.meals[0].idMeal})">
${isFav(list, mealDetails.meals[0].idMeal) ? 'Remove From Favourite' : 'Add To Favourite'} </div>
</div>
</div>
</div>
<div class="card-name">
Related Items
</div>
<div id="cards-holder" class=" remove-top-margin ">`
}
if( mealList.meals!=null){
html += mealList.meals.map(element => {
return `
<div class="card">
<div class="card-top" onclick="showMealDetails(${element.idMeal}, '${searchInput}')">
<div class="dish-photo" >
<img src="${element.strMealThumb}" alt="">
</div>
<div class="dish-name">
${element.strMeal}
</div>
<div class="dish-details">
${truncate(element.strInstructions, 50)}
<span class="button" onclick="showMealDetails(${element.idMeal}, '${searchInput}')">Know More</span>
</div>
</div>
<div class="card-bottom">
<div class="like">
<i class="fa-solid fa-heart ${isFav(list, element.idMeal) ? 'active' : ''} "
onclick="addRemoveToFavList(${element.idMeal})"></i>
</div>
<div class="play">
<a href="${element.strYoutube}">
<i class="fa-brands fa-youtube"></i>
</a>
</div>
</div>
</div>
`
}).join('');
}
html = html + '</div>';
document.getElementById('flex-box').innerHTML = html;
}
/**
This function is used to show all the meals which are added to the favourite list.
@function
@async
@returns {string} html - This returns html which is used to show the favourite meals.
@throws {Error} If there is no favourite meal then it will show "Nothing To Show....."
@example
showFavMealList()
*/
async function showFavMealList() {
let favList = JSON.parse(localStorage.getItem(dbObjectFavList));
let url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=";
let html = "";
if (favList.length == 0) {
html = `<div class="fav-item nothing"> <h1>
Nothing To Show.....</h1> </div>`
} else {
for (let i = 0; i < favList.length; i++) {
const favMealList = await fetchMealsFromApi(url, favList[i]);
if (favMealList.meals[0]) {
let element = favMealList.meals[0];
html += `
<div class="fav-item" onclick="showMealDetails(${element.idMeal},'${generateOneCharString()}')">
<div class="fav-item-photo">
<img src="${element.strMealThumb}" alt="">
</div>
<div class="fav-item-details">
<div class="fav-item-name">
<strong>Name: </strong>
<span class="fav-item-text">
${element.strMeal}
</span>
</div>
<div id="fav-like-button" onclick="addRemoveToFavList(${element.idMeal})">
Remove
</div>
</div>
</div>
`
}
}
}
document.getElementById('fav').innerHTML = html;
}
updateTask();
/************************************** End Of the Code ****************************************** */