-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (69 loc) · 2.59 KB
/
Copy pathscript.js
File metadata and controls
97 lines (69 loc) · 2.59 KB
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
const searchBox = document.querySelector('.searchBox');
const searchBtn = document.querySelector('.searchBtn');
const recipeContainer = document.querySelector('.recipe-container');
const recipeDetailsContent = document.querySelector('.recipe-details-content');
const recipeCloseBtn = document.querySelector('.recipe-close-btn');
//function to get recipes
const fetchRecipes = async(query) =>{
recipeContainer.innerHTML = "<h2>Fetching Recipes...</h2>";
const data = await fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`);
const response = await data.json();
recipeContainer.innerHTML = "";
response.meals.forEach(meal =>{
// console.log(meal);
const recipeDiv = document.createElement('div');
recipeDiv.classList.add('recipe');
recipeDiv.innerHTML = `
<img src = "${meal.strMealThumb}">
<h3>${meal.strMeal}</h3>
<p>${meal.strArea}</p>
<p>${meal.strCategory}</p>
`
const button = document.createElement('button');
button.textContent = "View Recipe";
recipeDiv.appendChild(button);
// Adding addEventListener to recipe button
button.addEventListener('click',() =>{
openRecipePopup(meal);
})
recipeContainer.appendChild(recipeDiv);
});
}
//function to fetch ingredients and measurements;
const fetchIngredients = (meal) =>{
// console.log(meal);
let ingredientsList = "";
for(let i = 1;i<20;i++){
const ingredient = meal[`strIngredient${i}`];
if(ingredient){
const measure = meal[`strMeasure${i}`];
ingredientsList += `<li>${measure}${ingredient}</li>`
}
else{
break;
}
}
return ingredientsList;
}
//button click it open recipe ....
const openRecipePopup = (meal) =>{
recipeDetailsContent.innerHTML = `
<h2 class = "recipeName">${meal.strMeal}</h2>
<h3>Ingredients:</h3>
<ul class = "ingredientList">${fetchIngredients(meal)}</ul>
<div class = "recipeInstructions">
<h3>Instructions:</h3>
<p >${meal.strInstructions}</p>
</div>
`
recipeDetailsContent.parentElement.style.display = "block";
}
recipeCloseBtn.addEventListener('click',() =>{
recipeDetailsContent.parentElement.style.display = "none";
});
searchBtn.addEventListener('click',(e) =>{
e.preventDefault();
const searchInput = searchBox.value.trim();
fetchRecipes(searchInput);
// console.log("Button clicked");
});