-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry.js
479 lines (427 loc) · 21.8 KB
/
country.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//API DATA HANDLING
//list of areas data
const areaURL = 'https://www.themealdb.com/api/json/v1/1/list.php?a=list';
//all meals from areas
const areaMealsBaseURL = 'https://www.themealdb.com/api/json/v1/1/filter.php?a=';
const idBaseURL = 'https://www.themealdb.com/api/json/v1/1/lookup.php?i=';
//fetch list of areas
async function fetchAllAreas() {
try {
const res = await fetch(areaURL)
const data = await res.json()
UI.insertAreas(data)
} catch (err) {
console.log('There has been an error', err);
}
}
//fetch a meal by its area
async function fetchMealsByArea(area) {
try {
const res = await fetch(areaMealsBaseURL + area)
const data = await res.json()
//call the getmealsbyid()
data.meals.forEach((meal) => {
fetchMealById(meal.idMeal)
})
} catch (err) {
console.log('There has been an error', err);
}
}
//fetch a meal by its id
async function fetchMealById(id) {
try {
const res = await fetch(idBaseURL + id)
const data = await res.json()
//find a way to return an array of objects
//console.log(data);
UI.createMeals(data)
} catch (err) {
console.log('There has been an error', err);
}
}
//get recipe with id
async function fetchRecipe(id) {
try {
const res = await fetch(idBaseURL + id)
const data = await res.json()
//find a way to return an array of objects
//console.log(data);
UI.createRecipe(data)
return data
} catch (err) {
console.log(err);
}
}
//LOAD FUNCTIONS ON WINDOW LOAD
document.addEventListener('DOMContentLoaded', () => {
//fetch all areas
fetchAllAreas()
//show default american area
fetchMealsByArea('american')
})
//////////////////////////////////////////////
//variables
let randomMealSection = document.querySelector('.random-meal-section');
let mealCategoriesSection = document.querySelector('.meal-categories-section')
let recipeSection = document.querySelector('.recipe-section')
let countrySection = document.querySelector('.country-section')
let closeBtn = document.querySelector('.close-btn')
let categories = document.querySelectorAll('.category');
let grid = document.querySelector('.grid-country')
let container = document.querySelector('.container')
let countryContainer = document.querySelector('.country-container')
let body = document.querySelector('body')
let gridItems = document.querySelectorAll('.grid-item')
let categoryValue;
let idsArr = []
let meals = []
//REPRESENTS A MEAL
class MealCard {
constructor(data) {
this.name = data.meals[0].strMeal
this.type = data.meals[0].strCategory
this.area = data.meals[0].strArea
this.thumb = data.meals[0].strMealThumb
this.id = data.meals[0].idMeal
}
}
//HANDLES UI TASKS
class UI {
//create recipe
static createRecipe(data) {
const meals = Storage.getMealFromLS();
if (meals.includes(data.meals[0].idMeal)) {
recipeSection.innerHTML = `
<div class="close-btn"><i class="fas fa-times"></i></div>
<div class="recipe-image-container">
<img src="${ data.meals[0].strMealThumb }" alt="${ data.meals[0].strMeal }">
</div>
<div class="recipe-info-container">
<h3 class="recipe-title active">
${ data.meals[0].strMeal }
</h3>
<div class="recipe-category-container">
<p class="recipe-category active">Category:</p>
<p class="recipe-category-result">${ data.meals[0].strCategory }</p>
</div>
<div class="recipe-area-container">
<p class="recipe-area active">Area:</p>
<p class="recipe-area-result">${ data.meals[0].strArea }</p>
</div>
<div class="heart-container" id="${ data.meals[0].idMeal }">
<i class="fas fa-heart heart-2 pink"></i>
</div>
<div class="ingredients-container">
<h4 class="ingredients-title active">Ingredients</h4>
<ul class="ingredients-list">
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient1 }</span><span class="ingr-measure">${ data.meals[0].strMeasure1 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient2 }</span><span class="ingr-measure">${ data.meals[0].strMeasure2 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient3 }</span><span class="ingr-measure">${ data.meals[0].strMeasure3 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient4 }</span><span class="ingr-measure">${ data.meals[0].strMeasure4 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient5 }</span><span class="ingr-measure">${ data.meals[0].strMeasure5 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient6 }</span><span class="ingr-measure">${ data.meals[0].strMeasure6 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient7 }</span><span class="ingr-measure">${ data.meals[0].strMeasure7 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient8 }</span><span class="ingr-measure">${ data.meals[0].strMeasure8 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient9 }</span><span class="ingr-measure">${ data.meals[0].strMeasure9 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient10 }</span><span class="ingr-measure">${ data.meals[0].strMeasure10 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient11 }</span><span class="ingr-measure">${ data.meals[0].strMeasure11 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient12 }</span><span class="ingr-measure">${ data.meals[0].strMeasure12 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient13 }</span><span class="ingr-measure">${ data.meals[0].strMeasure13 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient14 }</span><span class="ingr-measure">${ data.meals[0].strMeasure14 }</span></li><li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient15 }</span><span class="ingr-measure">${ data.meals[0].strMeasure15 }</span></li><li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient16 }</span><span class="ingr-measure">${ data.meals[0].strMeasure16 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient17 }</span><span class="ingr-measure">${ data.meals[0].strMeasure17 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient18 }</span><span class="ingr-measure">${ data.meals[0].strMeasure18 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient19 }</span><span class="ingr-measure">${ data.meals[0].strMeasure19 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient20 }</span><span class="ingr-measure">${ data.meals[0].strMeasure20 }</span></li>
</ul>
</div>
<div class="instructions-container">
<div class="instructions-title active">Instructions</div>
<p class="instructions">
"${ data.meals[0].strInstructions }"
</p>
<p class="note active">Buon Appetito!</p>
<div class="video">
<iframe class="video" id="player" type="text/html" width="640" height="390" src="https://www.youtube.com/embed/${ data.meals[0].strYoutube.slice(32) }" frameborder="0"></iframe>
</div>
</div>
</div>
`
} else {
recipeSection.innerHTML = `
<div class="close-btn"><i class="fas fa-times"></i></div>
<div class="recipe-image-container">
<img src="${ data.meals[0].strMealThumb }" alt="${ data.meals[0].strMeal }">
</div>
<div class="recipe-info-container">
<h3 class="recipe-title active">
${ data.meals[0].strMeal }
</h3>
<div class="recipe-category-container">
<p class="recipe-category active">Category:</p>
<p class="recipe-category-result">${ data.meals[0].strCategory }</p>
</div>
<div class="recipe-area-container">
<p class="recipe-area active">Area:</p>
<p class="recipe-area-result">${ data.meals[0].strArea }</p>
</div>
<div class="heart-container" id="${ data.meals[0].idMeal }">
<i class="fas fa-heart heart-2"></i>
</div>
<div class="ingredients-container">
<h4 class="ingredients-title active">Ingredients</h4>
<ul class="ingredients-list">
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient1 }</span><span class="ingr-measure">${ data.meals[0].strMeasure1 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient2 }</span><span class="ingr-measure">${ data.meals[0].strMeasure2 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient3 }</span><span class="ingr-measure">${ data.meals[0].strMeasure3 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient4 }</span><span class="ingr-measure">${ data.meals[0].strMeasure4 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient5 }</span><span class="ingr-measure">${ data.meals[0].strMeasure5 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient6 }</span><span class="ingr-measure">${ data.meals[0].strMeasure6 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient7 }</span><span class="ingr-measure">${ data.meals[0].strMeasure7 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient8 }</span><span class="ingr-measure">${ data.meals[0].strMeasure8 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient9 }</span><span class="ingr-measure">${ data.meals[0].strMeasure9 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient10 }</span><span class="ingr-measure">${ data.meals[0].strMeasure10 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient11 }</span><span class="ingr-measure">${ data.meals[0].strMeasure11 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient12 }</span><span class="ingr-measure">${ data.meals[0].strMeasure12 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient13 }</span><span class="ingr-measure">${ data.meals[0].strMeasure13 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient14 }</span><span class="ingr-measure">${ data.meals[0].strMeasure14 }</span></li><li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient15 }</span><span class="ingr-measure">${ data.meals[0].strMeasure15 }</span></li><li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient16 }</span><span class="ingr-measure">${ data.meals[0].strMeasure16 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient17 }</span><span class="ingr-measure">${ data.meals[0].strMeasure17 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient18 }</span><span class="ingr-measure">${ data.meals[0].strMeasure18 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient19 }</span><span class="ingr-measure">${ data.meals[0].strMeasure19 }</span></li>
<li class="ingredient"><span class="ingr-title">${ data.meals[0].strIngredient20 }</span><span class="ingr-measure">${ data.meals[0].strMeasure20 }</span></li>
</ul>
</div>
<div class="instructions-container">
<div class="instructions-title active">Instructions</div>
<p class="instructions">
"${ data.meals[0].strInstructions }"
</p>
<p class="note active">Buon Appetito!</p>
<div class="video">
<iframe class="video" id="player" type="text/html" width="640" height="390" src="https://www.youtube.com/embed/${ data.meals[0].strYoutube.slice(32) }" frameborder="0"></iframe>
</div>
</div>
</div>
`
}
}
//changes category background color on click
static changeAreaColor() {
let countries = document.querySelectorAll('.country')
let america = countries[0]
america.classList.add('active-background')
countries.forEach((country) => {
country.addEventListener('click', () => {
countries.forEach((country) => {
country.classList.remove('active-background')
})
country.classList.add('active-background')
})
})
}
//create meal card
static createMeals(data) {
//create a new meal obj
const meal = new MealCard(data)
let gridItem = document.createElement('div')
gridItem.classList.add('grid-item')
gridItem.classList.add('meal')
gridItem.id = `${ meal.id }`
gridItem.innerHTML = `
<div class="meal-photo">
<img src="${ meal.thumb }" alt="meal">
</div>
<div class="meal-info">
<h3 class="meal-title active">${ meal.name }</h3>
<p class=meal-category><i class="fas fa-boxes active"></i>${ meal.type }</p>
<p class="meal-area"><i class="fas fa-flag active"></i>${ meal.area }</p>
</div>
<div class="heart-container">
<div>
<i class="fas fa-heart heart-full"></i>
</div>
</div>
`
grid.appendChild(gridItem)
//if id is in LS, add pink to heart
const meals = Storage.getMealFromLS();
if (meals.includes(gridItem.id)) {
gridItem.innerHTML = `
<div class="meal-photo">
<img src="${ meal.thumb }" alt="meal">
</div>
<div class="meal-info">
<h3 class="meal-title active">${ meal.name }</h3>
<p class=meal-category><i class="fas fa-boxes active"></i>${ meal.type }</p>
<p class="meal-area"><i class="fas fa-flag active"></i>${ meal.area }</p>
</div>
<div class="heart-container">
<div>
<i class="fas fa-heart heart-full pink"></i>
</div>
</div>
`
}
}
//insert areas list
static insertAreas(areas) {
areas.meals.forEach((meal) => {
let country = document.createElement('div')
country.classList.add('country')
country.id = meal.strArea
country.innerHTML = `
<p class="country-text">${ meal.strArea }</p>
`
countryContainer.appendChild(country)
})
UI.changeAreaColor()
//add event listener on each country
let countries = document.querySelectorAll('.country')
countries.forEach((c) => {
c.addEventListener('click', () => {
grid.innerHTML = ''
fetchMealsByArea(c.id)
})
})
}
}
//HANDLES STORAGE
class Storage {
static getMealFromLS() {
let meals;
if (localStorage.getItem('meals') === null) {
meals = [];
} else {
meals = JSON.parse(localStorage.getItem('meals'));
}
return meals;
}
static addMealToLS(meal) {
const meals = Storage.getMealFromLS();
//check if id is already in LS
if (meals.includes(meal)) {
return
} else {
meals.push(meal);
localStorage.setItem('meals', JSON.stringify(meals));
}
}
static removeMealFromLS(id) {
const meals = Storage.getMealFromLS();
meals.forEach((meal, index) => {
if (meal === id) {
meals.splice(index, 1);
}
});
localStorage.setItem('meals', JSON.stringify(meals));
}
}
// EVENT LISTENERS
//get meal id on grid card click
grid.addEventListener('click', (e) => {
if (e.target.classList.contains('grid-item')) {
//get recipe
fetchRecipe(e.target.id)
//remove none class from recipe section
recipeSection.classList.remove('none')
//add none class to country section
countrySection.classList.add('none')
//add none class to grid
grid.classList.add('none')
//scroll to top of recipe
container.scrollTo(0, 0)
} else if (e.target.parentElement.parentElement.classList.contains('grid-item')) {
//get recipe
fetchRecipe(e.target.parentElement.parentElement.id)
//remove none class from recipe section
recipeSection.classList.remove('none')
//add none class to country section
countrySection.classList.add('none')
//add none class to grid
grid.classList.add('none')
//scroll to top of recipe
container.scrollTo(0, 0)
}
})
//close button listener
recipeSection.addEventListener('click', (e) => {
if (e.target.classList.contains('fa-times')) {
//add none class from recipe section
recipeSection.classList.add('none')
//remove none class to country section
countrySection.classList.remove('none')
//remove none class to grid
grid.classList.remove('none')
}
})
//heart listener
body.addEventListener('click', (e) => {
if (e.target.classList.contains('fa-heart') && e.target.classList.contains('fas') && e.target.parentElement.parentElement.parentElement.classList.contains('grid-item') === true) { // HERE **********
//change heart color and animate it
e.target.classList.add('pink')
e.target.classList.toggle('animate-heart')
//add/remove favourite meal to/from storage
let id = e.target.parentElement.parentElement.parentElement.id
if (Storage.getMealFromLS().includes(id)) {
e.target.classList.remove('pink')
Storage.removeMealFromLS(id)
} else {
Storage.addMealToLS(id)
}
}
})
//heart listener on RECIPE page
body.addEventListener('click', (e) => {
if (e.target.classList.contains('fa-heart') && e.target.classList.contains('fas') && e.target.parentElement.parentElement.parentElement.classList.contains('grid-item') === false) { // HERE **********
//change heart color and animate it
e.target.classList.add('pink')
e.target.classList.toggle('animate-heart')
//add/remove favourite meal to/from storage
let id = e.target.parentElement.id
if (Storage.getMealFromLS().includes(id)) { // HERE **********
e.target.classList.remove('pink')
Storage.removeMealFromLS(id)
recipeSection.addEventListener('click', (e) => {
if (e.target.classList.contains('fa-times')) {
location.reload() // HERE **********
}
})
} else {
Storage.addMealToLS(id)
recipeSection.addEventListener('click', (e) => {
if (e.target.classList.contains('fa-times')) {
location.reload() // HERE **********
}
})
}
}
})
//favourite button listener
body.addEventListener('click', (e) => {
if (e.target.classList.contains('fa-heart') && e.target.classList.contains('far')) {
console.log('hi');
}
})
//countries scroll x on mouse hold
let isDown = false;
let startX;
let scrollLeft;
countryContainer.addEventListener('mousedown', (e) => {
isDown = true;
startX = e.pageX - countryContainer.offsetLeft;
scrollLeft = countryContainer.scrollLeft;
});
countryContainer.addEventListener('mouseleave', () => {
isDown = false;
});
countryContainer.addEventListener('mouseup', () => {
isDown = false;
});
countryContainer.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - countryContainer.offsetLeft;
const walk = (x - startX) * 3; //scroll-fast
countryContainer.scrollLeft = scrollLeft - walk;
});
//TO DO:
//1. swap \r\ in recipe for <br> using regex?