-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
402 lines (354 loc) · 14.3 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
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
const EMPTY_HEART = '♡'
const FULL_HEART = '♥'
// Number of input elements to search meals
let condCnt = 0;
// It is to store lists of options for input elements to search meals.
// index 0: Meal Category, 1: Area, 2: ingredients
const arrConditionDL = [];
// It is to save list of favorite meals so that don't need to fetch every time this list is needed.
const arrFavoriteMeals = [];
fetchSearchConditions();
addSearchElements();
fetchFavoriteMeals();
document.getElementById('search-meals').addEventListener('submit', e => {
e.preventDefault();
const arrSearchConditions = [];
for (let i = 0; i < condCnt; i++) {
const sltTag = document.getElementById(`select${i}`);
const inputTag = document.getElementById(`input${i}`);
arrSearchConditions.push({select: sltTag.value, input: inputTag.value});
}
// console.log(arrSearchConditions);
searchMeals(arrSearchConditions, [], true);
// initialzing search html elements
document.getElementById('search-conditions').innerHTML = '';
condCnt = 0;
addSearchElements();
});
function fetchSearchConditions() {
// fetching meal category data list
fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
.then(resp => resp.json())
.then(mealCategory => {
// console.log(mealCategory.meals);
arrConditionDL[0] = mealCategory.meals.map(objCategory => objCategory.strCategory);
});
// fetching area data list
fetch('https://www.themealdb.com/api/json/v1/1/list.php?a=list')
.then(resp => resp.json())
.then(area => {
// console.log(area.meals);
arrConditionDL[1] = area.meals.map(objArea => objArea.strArea);
});
// fetching ingredients data list
fetch('https://www.themealdb.com/api/json/v1/1/list.php?i=list')
.then(resp => resp.json())
.then(ingredients => {
// console.log(ingredients.meals);
arrConditionDL[2] = ingredients.meals.map(objIngredient => objIngredient.strIngredient);
});
// console.log('arrConditionDL', arrConditionDL);
}
function addSearchElements() {
const sltTag = document.createElement('select');
sltTag.id = `select${condCnt}`;
const optTag1 = document.createElement('option');
optTag1.value = '0';
optTag1.textContent = 'Choose one';
const optTag2 = document.createElement('option');
optTag2.value = '1';
optTag2.textContent = 'Name';
const optTag3 = document.createElement('option');
optTag3.value = '2';
optTag3.textContent = 'Meal Category';
const optTag4 = document.createElement('option');
optTag4.value = '3';
optTag4.textContent = 'Area';
const optTag5 = document.createElement('option');
optTag5.value = '4';
optTag5.textContent = 'Ingredient';
sltTag.append(optTag1, optTag2, optTag3, optTag4, optTag5);
sltTag.value = '0';
sltTag.addEventListener('change', mkDataList);
const dlTag = document.createElement('datalist');
dlTag.id = `datalist${condCnt}`;
const inputTag = document.createElement('input');
inputTag.type = 'text';
inputTag.name = `condition${condCnt}`;
inputTag.id = `input${condCnt}`;
inputTag.setAttribute('list', `datalist${condCnt}`);
const btnTag = document.createElement('button');
btnTag.type = 'button';
btnTag.textContent = '+';
btnTag.addEventListener('click', addSearchElements);
const brTag = document.createElement('br');
const divTag = document.getElementById('search-conditions');
divTag.append(sltTag, inputTag, dlTag, btnTag, brTag);
condCnt++;
}
function mkDataList(e) {
const condNum = e.target.id.slice(6);
//console.log('condNum', condNum);
const dlTag = document.getElementById(`datalist${condNum}`);
//console.log('dlTag', dlTag);
dlTag.innerHTML = '';
const sltTagValue = e.target.value;
if (sltTagValue >= 2) {
arrConditionDL[e.target.value - 2].forEach(elem => {
const opTag = document.createElement('option');
opTag.value = elem;
dlTag.appendChild(opTag);
});
}
}
function searchMeals(arrSearchConditions, arrSearchedMeals, bFirst) {
if (arrSearchConditions.length <= 0) {
if (arrSearchedMeals.length === 0) {
alert("Can't find any matching result.");
} else {
displaySearchedMeals(arrSearchedMeals);
}
return;
}
const objSearchCondition = arrSearchConditions.pop();
let srchCond = '';
let srchUrl = 'https://www.themealdb.com/api/json/v1/1/';
switch(objSearchCondition.select) {
case '1':
srchUrl = `${srchUrl}search.php?s=${objSearchCondition.input}`;
srchCond = 'Name';
break;
case '2':
srchUrl = `${srchUrl}filter.php?c=${objSearchCondition.input}`;
srchCond = 'Meal Category';
break;
case '3':
srchUrl = `${srchUrl}filter.php?a=${objSearchCondition.input}`;
srchCond = 'Area';
break;
case '4':
srchUrl = `${srchUrl}filter.php?i=${objSearchCondition.input}`;
srchCond = 'Ingredient';
break;
default:
alert('Please, add condition to get a search result.');
return;
}
fetch(srchUrl)
.then(resp => resp.json())
.then(meals => {
// console.log('meals.meals', meals.meals);
if (meals.meals !== null) {
if (bFirst) {
arrSearchedMeals.splice(0, 0, ...meals.meals);
} else {
findIntersection(arrSearchedMeals, meals.meals);
}
} else {
alert(`"${srchCond}": "${objSearchCondition.input}" is not valid!`)
}
// console.log(arrSearchedMeals);
searchMeals(arrSearchConditions, arrSearchedMeals, false);
})
.catch(error => console.log(error));
}
function findIntersection(arr1, arr2) {
let arrIds1 = arr1.map(elem => elem.idMeal);
let arrIds2 = arr2.map(elem => elem.idMeal);
let arrIdsIter, arrIdsSet, arrIter;
if (arrIds1.length <= arrIds2.length) {
arrIdsIter = arrIds1;
arrIter = arr1;
arrIdsSet = new Set(arrIds2);
} else {
arrIdsIter = arrIds2;
arrIter = arr2;
arrIdsSet = new Set(arrIds1);
}
const arrIdsIntersection = arrIdsIter.filter(id => arrIdsSet.has(id));
// console.log('arrIdsIntersection', arrIdsIntersection)
const arrRes = arrIdsIntersection.map(id => arrIter.find(obj => obj.idMeal === id));
// console.log('arrRes', arrRes);
arr1.splice(0, arr1.length, ...arrRes);
}
function displaySearchedMeals(arrSearchedMeals) {
const ulMeals = document.getElementById('searched-meal-list');
ulMeals.innerHTML = '';
arrSearchedMeals.forEach(objMeal => {
const imgMeal = document.createElement('img');
imgMeal.src = objMeal.strMealThumb;
imgMeal.alt = objMeal.strMeal;
imgMeal.width = '30';
imgMeal.height = '30';
imgMeal.classList.add('img-thumbnail');
const liMeal = document.createElement('li');
liMeal.appendChild(imgMeal);
// console.log('liMeal', liMeal, liMeal.innerHTML);
liMeal.innerHTML = `${liMeal.innerHTML} ${objMeal.strMeal}`;
liMeal.classList.add('pointer');
liMeal.addEventListener('click', e => displayMealDesc(objMeal.idMeal, false));
ulMeals.appendChild(liMeal);
});
}
function displayMealDesc(id, bFromFavorites) {
fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${id}`)
.then(resp => resp.json())
.then(mealDesc => {
const objMealDesc = mealDesc.meals[0];
// console.log(objMealDesc);
const imgMeal = document.createElement('img');
imgMeal.src = objMealDesc.strMealThumb;
imgMeal.alt = objMealDesc.strMeal;
imgMeal.width = '300';
imgMeal.height = '300';
imgMeal.style.display = 'block';
imgMeal.style.marginLeft = 'auto';
imgMeal.style.marginRight = 'auto';
const ulMealDesc = document.createElement('ul');
const liStrMeal = document.createElement('li');
liStrMeal.textContent = `Name: ${objMealDesc.strMeal}`;
liStrMeal.id = 'display-meal-desc-strmeal';
const liCategory = document.createElement('li');
liCategory.textContent = `Category: ${objMealDesc.strCategory}`;
const liArea = document.createElement('li');
liArea.textContent = `Origin: ${objMealDesc.strArea}`;
const liInstructions = document.createElement('li');
liInstructions.textContent = `Instructions: ${objMealDesc.strInstructions}`;
const ulIngredients = document.createElement('ul');
for (let i = 1; i <= 20 && objMealDesc[`strIngredient${i}`] !== null && objMealDesc[`strIngredient${i}`] !== ''; i++) {
// console.log(`strIngredient${i}: `, objMealDesc[`strIngredient${i}`], objMealDesc[`strIngredient${i}`] !== '');
const liIngredient = document.createElement('li');
liIngredient.textContent = objMealDesc[`strIngredient${i}`];
ulIngredients.appendChild(liIngredient);
}
const liIngredients = document.createElement('li');
liIngredients.textContent = "Ingredients: ";
ulMealDesc.append(liStrMeal, liCategory, liArea, liInstructions, liIngredients, ulIngredients);
const ftFavorite = document.createElement('footer');
const ulFavorite = document.createElement('ul');
const liFavorite = document.createElement('li');
const spHeart = document.createElement('span');
spHeart.id = 'heart';
spHeart.classList.add('pointer');
if (bFromFavorites) {
spHeart.textContent = FULL_HEART;
spHeart.classList.add('activated-heart');
} else {
const stFavoriteMealsId = new Set(arrFavoriteMeals.map(objMeal => objMeal.id));
if (stFavoriteMealsId.has(id)) {
spHeart.textContent = FULL_HEART;
spHeart.classList.add('activated-heart');
} else {
spHeart.textContent = EMPTY_HEART;
}
}
spHeart.addEventListener('click', e => {
//console.log(e);
if (e.target.textContent === EMPTY_HEART) {
e.target.textContent = FULL_HEART;
e.target.classList.toggle('activated-heart');
addFavoriteMeal(objMealDesc);
} else {
e.target.textContent = EMPTY_HEART;
e.target.classList.toggle('activated-heart');
deleteFavoriteMeal(objMealDesc.idMeal);
}
});
liFavorite.innerHTML = 'Add to favorite: ';
liFavorite.append(spHeart);
ulFavorite.append(liFavorite);
ftFavorite.append(ulFavorite);
const divMealDesc = document.getElementById('display-meal-desc');
divMealDesc.innerHTML = '';
divMealDesc.append(imgMeal, ulMealDesc, ftFavorite);
})
.catch(error => console.log(error));
}
function addFavoriteMeal(objMealDesc) {
fetch(`http://localhost:3000/favoriteMeals`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
'id': objMealDesc.idMeal,
'strMeal': objMealDesc.strMeal,
'strMealThumb' : objMealDesc.strMealThumb,
}),
})
.then(resp => resp.json())
.then(objMeal => {
// console.log('POST: ', objMeal);
arrFavoriteMeals.push(objMeal);
// console.log('addFavoriteMeal', arrFavoriteMeals);
displayFavoriteMeals();
})
.catch(error => console.log(error));
}
function deleteFavoriteMeal(idMeal) {
fetch(`http://localhost:3000/favoriteMeals/${idMeal}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
})
.then(resp => resp.json())
.then(objMeal => {
//console.log('DELETE: ', objMeal);
for (let i = 0; i < arrFavoriteMeals.length; i++) {
if (arrFavoriteMeals[i].id === idMeal) {
arrFavoriteMeals.splice(i, 1);
break;
}
}
// console.log('deleteFavoriteMeal', arrFavoriteMeals);
displayFavoriteMeals();
})
.catch(error => console.log(error));
}
function fetchFavoriteMeals() {
fetch('http://localhost:3000/favoriteMeals')
.then(resp => resp.json())
.then(favoriteMeals => {
// console.log('fetchFavoriteMeals: ', favoriteMeals);
arrFavoriteMeals.splice(0, arrFavoriteMeals.length, ...favoriteMeals);
displayFavoriteMeals();
})
.catch(error => console.log(error));
}
function displayFavoriteMeals() {
const ulMeals = document.getElementById('favorite-meals');
ulMeals.innerHTML = '';
arrFavoriteMeals.forEach((objMeal, i) => {
const imgMeal = document.createElement('img');
imgMeal.src = objMeal.strMealThumb;
imgMeal.alt = objMeal.strMeal;
imgMeal.width = '30';
imgMeal.height = '30';
imgMeal.classList.add('img-thumbnail');
imgMeal.addEventListener('click', e => displayMealDesc(objMeal.id, true));
const spStrMeal = document.createElement('span');
spStrMeal.textContent = objMeal.strMeal;
spStrMeal.addEventListener('click', e => displayMealDesc(objMeal.id, true));
const btnDelete = document.createElement('button');
btnDelete.type = 'button'
btnDelete.textContent = 'X';
btnDelete.classList.add('btn-delete-favorite');
btnDelete.addEventListener('click', e => {
// If the meal description displays a meal deleted from favorite meals, update its heart in the meal description
const liStrMeal = document.getElementById('display-meal-desc-strmeal');
if (liStrMeal !== null && liStrMeal.textContent.slice(6) === objMeal.strMeal) {
const spHeart = document.getElementById('heart');
spHeart.textContent = EMPTY_HEART;
spHeart.classList.remove('activated-heart');
}
deleteFavoriteMeal(objMeal.id);
});
const liMeal = document.createElement('li');
liMeal.classList.add('pointer');
liMeal.append(imgMeal, spStrMeal, btnDelete);
ulMeals.appendChild(liMeal);
});
}