-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
374 lines (339 loc) · 18.6 KB
/
search.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
// API DATA FUNCTIONS
const nameBaseURL = 'https://www.themealdb.com/api/json/v1/1/search.php?s=';
const idBaseURL = 'https://www.themealdb.com/api/json/v1/1/lookup.php?i=';
//fetch a meal by name
async function fetchMealByName(letters) {
try {
const res = await fetch(nameBaseURL + letters)
const data = await res.json()
UI.createMeals(data)
return data
} catch (err) {
console.log(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);
}
}
/////////////////////////////////////////////////////
let grid = document.querySelector('.grid-favourite')
let body = document.querySelector('body')
let container = document.querySelector('.container')
let gridItems = document.querySelectorAll('.grid-item')
let searchInput = document.getElementById('search-input')
let mealCategoriesSection = document.querySelector('.meal-categories-section')
let recipeSection = document.querySelector('.recipe-section')
let searchSection = document.querySelector('.search-section')
let closeBtn = document.querySelector('.close-btn')
//REPRESENTS A MEAL
class MealCard {
constructor(data) {
this.name = data.strMeal
this.type = data.strCategory
this.area = data.strArea
this.thumb = data.strMealThumb
this.id = data.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">
<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>
`
}
}
//create meal card
static createMeals(data) {
if (data.meals === null) {
return
} else {
//create a new meal obj
data.meals.forEach((m) => {
const meal = new MealCard(m)
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>
`
}
})
}
}
}
//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
//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) {
//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');
}
})
//call fetchMealByName() if search input has value
searchInput.addEventListener('input', () => {
let result = searchInput.value
if (result.length === 0) {
grid.innerHTML = ''
} else {
grid.innerHTML = ''
fetchMealByName(result)
}
})
//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 meal categories section
searchSection.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 meal categories section
searchSection.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')) {
recipeSection.classList.add('none')
//remove none class to search section
searchSection.classList.remove('none')
}
})
//TO DO:
//1. swap \r\ in recipe for <br> using regex?