-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (89 loc) · 3.35 KB
/
app.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
// Creating necessary variables for the functions using Query Selector
const searchInput = document.querySelector(".search-input");
const searchBtn = document.querySelector(".search-btn");
const randomMealSection = document.querySelector(".random-meal");
const searchedMealsSection = document.querySelector(".searched-meals");
const ingredientModal = document.querySelector(".ingredient-modal");
const searchResult = document.querySelector("#search-results");
// Function for creating a meal-div (template)
const createMealElement = (meal) => {
const mealEl = document.createElement("div");
mealEl.classList.add("meal");
mealEl.innerHTML = `
<img src="${meal.strMealThumb}" alt="meal-image">
<h3>${meal.strMeal}</h3>
`;
// Setting up event listener for the modal
mealEl.addEventListener("click", () => {
showIngredientsModal(meal.idMeal);
});
return mealEl;
};
// Function for getting the ingredient list using the meal id through Fetch API
const showIngredientsModal = async (mealId) => {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealId}`
);
const data = await response.json();
const mealDetails = data.meals[0];
const ingredients = Object.entries(mealDetails)
.filter(([key, value]) => key.startsWith("strIngredient") && value)
.map(([, value]) => `<li>${value}</li>`)
.join("");
// Appending the fetched ingredient list inside the ingredient modal section
ingredientModal.innerHTML = `
<h3>Ingredients</h3>
<ul> ${ingredients} </ul>
`;
ingredientModal.style.display = "block";
};
// Function to fetch a random meal and display it as the "Meal of the Day"
async function fetchRandomMeal() {
const response = await fetch(
"https://www.themealdb.com/api/json/v1/1/random.php"
);
const data = await response.json();
const meal = data.meals[0];
randomMealSection.innerHTML = "";
randomMealSection.appendChild(createMealElement(meal));
}
// Function to fetch meals by catergoies
async function fetchMealsByCategory(category) {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/filter.php?c=${category}`
);
const data = await response.json();
const meals = data.meals;
searchedMealsSection.innerHTML = "";
if (meals) {
meals.forEach((meal) => {
searchedMealsSection.appendChild(createMealElement(meal));
});
} else {
console.log("No meals found for the specified category");
}
}
// Setting Up all the Eventlisteners => (^_^) => {
// Onclick event for the Search Button to fetch meals by categories:
searchBtn.addEventListener("click", () => {
const category = searchInput.value;
searchResult.innerHTML = `Search Results for "${category}"`;
fetchMealsByCategory(category);
});
// Using keyboard events to use the enter key for fetching meals by categories:
searchInput.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
const category = searchInput.value;
searchResult.innerHTML = `Search Results for "${category}"`;
fetchMealsByCategory(category);
}
});
// onclick event for closing the Ingredient modal:
window.onclick = (e) => {
if (e.target == ingredientModal || ingredientModal.contains(e.target)) {
ingredientModal.style.display = "none";
}
};
// }
// Calling the Function
fetchRandomMeal();