-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (108 loc) · 4.24 KB
/
script.js
File metadata and controls
123 lines (108 loc) · 4.24 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
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
// dom elements
const searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-btn");
const mealsContainer = document.getElementById("meals");
const resultHeading = document.getElementById("result-heading");
const errorContainer = document.getElementById("error-container");
const mealDetails = document.getElementById("meal-details");
const mealDetailsContent = document.querySelector(".meal-details-content");
const backBtn = document.getElementById("back-btn");
// api urls
const BASE_URL = "https://www.themealdb.com/api/json/v1/1/";
const SEARCH_URL = `${BASE_URL}search.php?s=`;
const LOOKUP_URL = `${BASE_URL}lookup.php?i=`;
// event listeners
searchBtn.addEventListener("click", searchMeals);
mealsContainer.addEventListener("click", handleMealClick);
backBtn.addEventListener("click", () => mealDetails.classList.add("hidden"));
searchInput.addEventListener("keypress", (e) => e.key === "Enter" && searchMeals());
// search meals
async function searchMeals() {
const searchTerm = searchInput.value.trim();
if (!searchTerm) {
errorContainer.textContent = "Please enter a search term";
errorContainer.classList.remove("hidden");
return;
}
try {
resultHeading.textContent = `Searching for "${searchTerm}"...`;
mealsContainer.innerHTML = "";
errorContainer.classList.add("hidden");
const response = await fetch(`${SEARCH_URL}${searchTerm}`);
const data = await response.json();
data.meals === null ? (
resultHeading.textContent = "",
mealsContainer.innerHTML = "",
errorContainer.textContent = `No recipes found for "${searchTerm}". Try another search term!`,
errorContainer.classList.remove("hidden")
) : (
resultHeading.textContent = `Search results for "${searchTerm}":`,
displayMeals(data.meals),
searchInput.value = ""
);
} catch (error) {
errorContainer.textContent = "Something went wrong. Please try again later.";
errorContainer.classList.remove("hidden");
}
}
// display meals
const displayMeals = (meals) => {
mealsContainer.innerHTML = meals.map(meal => `
<div class="meal" data-meal-id="${meal.idMeal}">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
<div class="meal-info">
<h3 class="meal-title">${meal.strMeal}</h3>
${meal.strCategory ? `<div class="meal-category">${meal.strCategory}</div>` : ""}
</div>
</div>
`).join("");
};
// handle meal click
async function handleMealClick(e) {
const mealEl = e.target.closest(".meal");
if (!mealEl) return;
const mealId = mealEl.getAttribute("data-meal-id");
try {
const response = await fetch(`${LOOKUP_URL}${mealId}`);
const data = await response.json();
if (data.meals && data.meals[0]) {
const meal = data.meals[0];
const ingredients = [];
for (let i = 1; i <= 20; i++) {
meal[`strIngredient${i}`] && meal[`strIngredient${i}`].trim() !== "" && ingredients.push({
ingredient: meal[`strIngredient${i}`],
measure: meal[`strMeasure${i}`]
});
}
mealDetailsContent.innerHTML = `
<img src="${meal.strMealThumb}" alt="${meal.strMeal}" class="meal-details-img">
<h2 class="meal-details-title">${meal.strMeal}</h2>
<div class="meal-details-category">
<span>${meal.strCategory || "Uncategorized"}</span>
</div>
<div class="meal-details-instructions">
<h3>Instructions</h3>
<p>${meal.strInstructions}</p>
</div>
<div class="meal-details-ingredients">
<h3>Ingredients</h3>
<ul class="ingredients-list">
${ingredients.map(item => `
<li><i class="fas fa-check-circle"></i> ${item.measure} ${item.ingredient}</li>
`).join("")}
</ul>
</div>
${meal.strYoutube ? `
<a href="${meal.strYoutube}" target="_blank" class="youtube-link">
<i class="fab fa-youtube"></i> Watch Video
</a>
` : ""}
`;
mealDetails.classList.remove("hidden");
mealDetails.scrollIntoView({ behavior: "smooth" });
}
} catch (error) {
errorContainer.textContent = "Could not load recipe details. Please try again later.";
errorContainer.classList.remove("hidden");
}
}