-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
74 lines (69 loc) · 2.57 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
const searchBtn = document.getElementById('search-btn');
const mealList = document.getElementById('meal');
const mealDetailsContent = document.querySelector('.meal-details-content');
const recipeCloseBtn = document.getElementById('recipe-close-btn');
//event listeners
searchBtn.addEventListener('click',getMealList);
mealList.addEventListener('click', getMealRecipe);
recipeCloseBtn.addEventListener('click', () => {
mealDetailsContent.parentElement.classList.remove('showRecipe');
});
// get meal list that matches with the ingredient
function getMealList(){
let searchInputTxt = document.getElementById('search-input').value.trim();
fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?i=${searchInputTxt}`)
.then(response => response.json())
.then(data => {
let html = "";
if(data.meals){
data.meals.forEach(meal => {
html+=`
<div class = "meal-item" data-id = "${meal.idMeal}">
<div class = "meal-img">
<img src = "${meal.strMealThumb}" alt = "food">
</div>
<div class = "meal-name">
<h3>${meal.strMeal}</h3>
<a href = "#" class = "recipe-btn">Get Recipe</a>
</div>
</div>
`;
});
mealList.classList.remove('notFound');
}
else{
html = "Sorry, we didnt find any meal";
mealList.classList.add('notFound');
}
mealList.innerHTML = html;
});
}
function getMealRecipe(e){
e.preventDefault();
if(e.target.classList.contains('recipe-btn')){
let mealItem = e.target.parentElement.parentElement;
fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealItem.dataset.id}`)
.then(response => response.json())
.then(data => mealRecipeModal(data.meals));
}
}
function mealRecipeModal(meal){
console.log(meal);
meal = meal[0];
let html = `
<h2 class = "recipe-title">${meal.strMeal}</h2>
<p class = "recipe-category">${meal.strCategory}</p>
<div class = "recipe-instruct">
<h3>Instructions:</h3>
<p>${meal.strInstructions}</p>
</div>
<div class = "recipe-meal-img">
<img src = "${meal.strMealThumb}" alt = "">
</div>
<div class = "recipe-link">
<a href = "${meal.strYoutube}" target = "_blank">Watch Video</a>
</div>
`;
mealDetailsContent.innerHTML = html;
mealDetailsContent.parentElement.classList.add('showRecipe');
}