-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (102 loc) · 3.49 KB
/
script.js
File metadata and controls
112 lines (102 loc) · 3.49 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
const searchInput = document.querySelector("#searchInput");
const searchButton = document.querySelector("#searchButton");
const mealList = document.querySelector("#mealList");
const modalContainer = document.querySelector(".modal-container");
const mealDetailsContent = document.querySelector(".meal-details-content");
const recipeCloseBtn = document.querySelector("#recipeCloseBtn");
// Event listeners
searchButton.addEventListener('click', async () => {
const ingredient = searchInput.value.trim();
if (ingredient) {
const meals = await searchMealsByIngredient(ingredient);
displayMeals(meals);
}
});
mealList.addEventListener('click', async (e) => {
const card = e.target.closest('.meal-item');
if (card) {
const mealId = card.dataset.id;
const meal = await getMealDetails(mealId);
if (meal) {
showMealDetailsPopup(meal);
}
}
});
// Function to fetch meals by ingredient
async function searchMealsByIngredient(ingredient) {
try {
const response = await fetch(`https://www.themealdb.com/api/json/v1/1/filter.php?i=${ingredient}`);
const data = await response.json();
return data.meals;
} catch (error) {
// Show error in console
console.error('Error fetching data:', error);
}
}
// Function to fetch meal details by ID
async function getMealDetails(mealId) {
try {
const response = await fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealId}`);
const data = await response.json();
return data.meals[0];
} catch (error) {
console.error('Error fetching meal details:', error);
}
}
// Function to display meals in list
function displayMeals(meals) {
mealList.innerHTML = '';
if (meals) {
meals.forEach((meal) => {
const mealItem = document.createElement('div');
mealItem.classList.add('meal-item');
mealItem.dataset.id = meal.idMeal;
mealItem.innerHTML = `
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
<h3>${meal.strMeal}</h3>
`;
mealList.appendChild(mealItem);
});
} else {
mealList.innerHTML = '<p>No meals found. Try another ingredient.</p>';
}
}
// Function to create and display meal details on popup
function showMealDetailsPopup(meal) {
mealDetailsContent.innerHTML = `
<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-img">
<img src="${meal.strMealThumb}" alt="${meal.strMeal}">
</div>
<div class="recipe-video">
<a href="${meal.strYoutube}" target="_blank">Video Tutorial</a>
</div>
`;
modalContainer.style.display = "block";
}
// Event listener for popup close button
recipeCloseBtn.addEventListener('click', () => {
modalContainer.style.display = 'none';
});
searchInput.addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
performSearch();
}
});
async function performSearch() {
const ingredient = searchInput.value.trim();
if (ingredient) {
const meals = await searchMealsByIngredient(ingredient);
displayMeals(meals);
}
}
// Perform a chicken search on page load
window.addEventListener('load', () => {
searchInput.value = 'chicken';
performSearch();
});