This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
134 lines (117 loc) · 3.71 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
// APIs Site: https://www.themealdb.com/api.php
// Search meal by name: https://www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata
// Lookup full meal details by id: https://www.themealdb.com/api/json/v1/1/lookup.php?i=52772
// Lookup a single random meal: https://www.themealdb.com/api/json/v1/1/random.php
const searchInput = document.getElementById('search'),
formSubmit = document.getElementById('form-submit'),
randomBtn = document.getElementById('random-btn'),
searchKeyword = document.getElementById('search-keyword'),
meals = document.getElementById('meals'),
singleMeal = document.getElementById('single-meal');
const searchMeal = async (e) => {
e.preventDefault();
const input = searchInput.value;
searchKeyword.innerHTML = '';
singleMeal.innerHTML = '';
if (input.trim()) {
try {
const data = await fetch(
`https://www.themealdb.com/api/json/v1/1/search.php?s=${input}`
).then((res) => res.json());
if (data.meals === null) {
searchKeyword.innerHTML = `There are no search results. Try again!`;
} else {
searchKeyword.innerHTML = `<h2>Showing result for: ${input}</h2>`;
meals.innerHTML = data.meals
.map(
(meal, index) => `
<div class='meal' key='${index}'>
<img src='${meal.strMealThumb}' alt='${meal.strMeal}'/>
<div class='meal-info' data-mealID='${meal.idMeal}'>
<h3>${meal.strMeal}</h3>
</div>
</div>
`
)
.join('');
searchInput.value = '';
}
} catch (err) {
console.error(err);
}
} else {
alert('Please enter a valid term.');
}
};
const displayMeal = (mealDetails) => {
const ingredient1 = [];
for (let i = 1; i <= 20; i++) {
if (mealDetails[`strIngredient${i}`].length > 0) {
ingredient1.push(
`${mealDetails[`strIngredient${i}`]} - ${mealDetails[`strMeasure${i}`]}`
);
} else {
break;
}
}
singleMeal.innerHTML = `<div class='single-meal'>
<h1>${mealDetails.strMeal}</h1>
<img src='${mealDetails.strMealThumb}' alt='${mealDetails.strMeal}'/>
<div class='single-meal-info'>
${mealDetails.strCategory ? `<p>${mealDetails.strCategory}</p>` : ''}
${mealDetails.strArea ? `<p>${mealDetails.strArea}</p>` : ''}
</div>
<div class='single-meal-details'>
${
mealDetails.strInstructions
? `<p>${mealDetails.strInstructions}</p>`
: ''
}
<h2>Ingrediants</h2>
<ul>
${ingredient1.map((ing) => `<li>${ing}</li>`).join('')}
</ul>
</div>
</div>`;
};
const getMealById = async (mealID) => {
try {
const mealData = await fetch(
`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealID}`
).then((res) => res.json());
displayMeal(mealData.meals[0]);
document.querySelector('#single-meal').scrollIntoView({
behavior: 'smooth',
});
} catch (err) {
console.error(err);
}
};
const getMealDetails = (e) => {
const mealInfo = e.path.find((item) => {
if (item.classList) {
return item.classList.contains('meal-info');
} else {
return false;
}
});
if (mealInfo) {
const mealID = mealInfo.getAttribute('data-mealID');
getMealById(mealID);
}
};
const getRandomMealDetails = async () => {
searchKeyword.innerHTML = '';
meals.innerHTML = '';
try {
const mealData = await fetch(
'https://www.themealdb.com/api/json/v1/1/random.php'
).then((res) => res.json());
displayMeal(mealData.meals[0]);
} catch (err) {
console.error(err);
}
};
formSubmit.addEventListener('submit', searchMeal);
meals.addEventListener('click', getMealDetails);
randomBtn.addEventListener('click', getRandomMealDetails);