-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (81 loc) · 2.55 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
const searchBtn= document.getElementById('searchBtn');
const mealList= document.getElementById('meal');
const mealDetailsContent= document.querySelector('.meal-details-content');
const recipeCloseBtn= document.getElementById('recipeCloseBtn');
searchBtn.addEventListener('click', getMealList);
mealList.addEventListener('click', getMealRecipe);
recipeCloseBtn.addEventListener('click', ()=>{
mealDetailsContent.parentElement.classList.remove('showRecipe');
});
function getMealList(){
let searchInputText= document.getElementById('searchInput').value.trim();
// console.log(searchInputText);
let url=`https://www.themealdb.com/api/json/v1/1/filter.php?i=${searchInputText}`
fetch(url)
.then((response)=>{
return response.json()
}).then((data)=> {
console.log(data);
showList(data);
});
}
function showList(data){
if(data.meals){
let template="";
data.meals.forEach(element=>{
template += `
<div class="meal-item" data-id="${element.idMeal}">
<div class="meal-img">
<img src="${element.strMealThumb}" alt="${element.strMeal}">
</div>
<div class="meal-name">
<h3>${element.strMeal}</h3>
<a href="#" class="recipe-btn")">Get Recipe</a>
</div>
</div>
`;
mealList.innerHTML= template;
});
meal.classList.remove('notfound');
}
else{
template= "Sorry, We didn't find any meal! Try Again";
mealList.innerHTML= template;
mealList.classList.add('notfound');
}
}
function getMealRecipe(e){
e.preventDefault();
if(e.target.classList.contains('recipe-btn')){
let mealItem= e.target.parentElement.parentElement;
console.log(mealItem);
const url= `https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealItem.dataset.id}`;
fetch(url)
.then((response)=>{
return response.json();
}).then((data)=>{
console.log(data);
mealRecipeModal(data.meals);
});
}
}
function mealRecipeModal(meal){
console.log(meal);
meal= meal[0];
let template= `
<h2 class="recipe-title">${meal.strMeal}</h2>
<p class="recipe-category">${meal.strCategory}</p>
<div class="recipe-instructions">
<h3>Instructions:</h3>
<p>${meal.strInstructions}</p>
</div>
<div class="recipe-meal-img">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
</div>
<div class="recipe-link">
<a href="${meal.strYoutube}" target="_blank">Watch Video Tutorial</a>
</div>
`;
mealDetailsContent.innerHTML= template;
mealDetailsContent.parentElement.classList.add('showRecipe');
}