-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (166 loc) · 6.97 KB
/
script.js
File metadata and controls
214 lines (166 loc) · 6.97 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Select DOM elements
const searchBox = document.querySelector('.searchBox');
const searchBtn = document.querySelector('.searchBtn');
const recipeContainer = document.querySelector('.recipe-container');
const recipeDetailsContents = document.querySelector('.recipe-details-contents')
const recipeCloseBtn = document.querySelector('.recipe-close-btn')
// Fetch recipes from API
const fetchRecipe = async (query) => {
recipeContainer.innerHTML = "<h2>Fetching Recipe</h2>";
try {
const data = await fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`);
const respones = await data.json();
// console.log(respones.meals[0]);
recipeContainer.innerHTML = "";
respones.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><span>${meal.strArea}</span>Dish</p>
<p>Belong to <span>${meal.strCategory} </span>Category</p>
`
// Add "View Recipe" button and create
const button = document.createElement('button');
button.textContent = "View Recire";
recipeDiv.appendChild(button);
button.addEventListener('click', () => {
openRecipePopup(meal);
})
recipeContainer.appendChild(recipeDiv);
})
} catch (error) {
recipeContainer.innerHTML = "<h2>Error In Fetching Recipe</h2>";
}
}
// function to fetch Ingredents
const fetchIngredents = (meal) => {
let ingredentsList = "";
for (let i=1; i<=20; i++){
const ingredient = meal[`strIngredient${i}`];
if(ingredient){
const measure = meal[`strMeasure${i}`];
ingredentsList += `<li>${ingredient},${measure}</li>`
}else{
break;
}
}
return ingredentsList;
}
// Open recipe popup
const openRecipePopup = (meal) => {
// console.log(meal);
recipeDetailsContents.innerHTML = `
<h2 class="recipeName">${meal.strMeal}</h2>
<h3>Ingredents</h3>
<ulclass="ingredentsList">${fetchIngredents(meal)}</ul>
<div class="recipeInstructions">
<h3>Instructions:</h3>
<p>${meal.strInstructions}</p>
</div>
`
recipeDetailsContents.parentElement.style.display = "block";
}
// close popup button view recipe
recipeCloseBtn.addEventListener('click', () => {
recipeDetailsContents.parentElement.style.display = "none";
})
// Handle search button click
searchBtn.addEventListener('click',(e)=>{
e.preventDefault();
const searchInput = searchBox.value.trim();
if(!searchInput){
recipeContainer.innerHTML = `<h2>Type the meal in the search box</h2>`
return;
}
fetchRecipe(searchInput);
// console.log('clicked');
});
// // feat: setup DOM element selectors for search, container, and popup
// // 🔹 Selecting input, button, and container elements from HTML
// const searchBox = document.querySelector('.searchBox');
// const searchBtn = document.querySelector('.searchBtn');
// const recipeContainer = document.querySelector('.recipe-container');
// const recipeDetailsContents = document.querySelector('.recipe-details-contents');
// const recipeCloseBtn = document.querySelector('.recipe-close-btn');
// // feat: add fetchRecipe function to get meals from TheMealDB API
// // 🔹 Function that fetches recipes based on user query
// const fetchRecipe = async (query) => {
// recipeContainer.innerHTML = "<h2>Fetching Recipe...</h2>"; // show loading message
// try {
// // feat: fetch data from TheMealDB API
// const data = await fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`);
// const respones = await data.json(); // parse JSON response
// recipeContainer.innerHTML = ""; // clear old results
// // feat: display recipe cards with image, name, area, and category
// respones.meals.forEach(meal => {
// const recipeDiv = document.createElement('div'); // create recipe card
// recipeDiv.classList.add('recipe');
// // card content
// recipeDiv.innerHTML = `
// <img src="${meal.strMealThumb}">
// <h3>${meal.strMeal}</h3>
// <p><span>${meal.strArea}</span> Dish</p>
// <p>Belongs to <span>${meal.strCategory}</span> Category</p>
// `;
// // feat: add "View Recipe" button to each recipe card
// const button = document.createElement('button');
// button.textContent = "View Recipe";
// recipeDiv.appendChild(button);
// // feat: add click handler to open popup with details
// button.addEventListener('click', () => {
// openRecipePopup(meal);
// });
// recipeContainer.appendChild(recipeDiv); // add card to container
// });
// } catch (error) {
// // fix: show error if API fails
// recipeContainer.innerHTML = "<h2>Error In Fetching Recipe</h2>";
// }
// };
// // feat: implement fetchIngredents function to list ingredients and measures
// // 🔹 Extract ingredients + measures from API response
// const fetchIngredents = (meal) => {
// let ingredentsList = "";
// for (let i = 1; i <= 20; i++) { // loop through possible 20 ingredients
// const ingredient = meal[`strIngredient${i}`];
// if (ingredient) {
// const measure = meal[`strMeasure${i}`];
// ingredentsList += `<li>${ingredient} - ${measure}</li>`; // add list item
// } else {
// break; // stop if ingredient is empty
// }
// }
// return ingredentsList; // return built list
// };
// // feat: add openRecipePopup function to display recipe details in popup
// const openRecipePopup = (meal) => {
// // fill popup content
// recipeDetailsContents.innerHTML = `
// <h2 class="recipeName">${meal.strMeal}</h2>
// <h3>Ingredients</h3>
// <ul class="ingredentsList">${fetchIngredents(meal)}</ul>
// <div class="recipeInstructions">
// <h3>Instructions:</h3>
// <p>${meal.strInstructions}</p>
// </div>
// `;
// recipeDetailsContents.parentElement.style.display = "block"; // show popup
// };
// // feat: add close button functionality to hide recipe popup
// recipeCloseBtn.addEventListener('click', () => {
// recipeDetailsContents.parentElement.style.display = "none"; // hide popup
// });
// // feat: handle search button click
// searchBtn.addEventListener('click', (e) => {
// e.preventDefault(); // prevent form submit
// const searchInput = searchBox.value.trim(); // get user input
// // fix: handle empty search input with proper message
// if (!searchInput) {
// recipeContainer.innerHTML = `<h2>Type the meal in the search box</h2>`;
// return;
// }
// fetchRecipe(searchInput); // call API
// });