-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (78 loc) · 3.05 KB
/
script.js
File metadata and controls
80 lines (78 loc) · 3.05 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
const apiKey = "YOUR_SPOONACULAR_API_KEY";
const ingredientInput = document.getElementById("ingredientInput");
const recipeContainer = document.getElementById("recipes");
const nutritionContainer = document.getElementById("nutritionInfo");
const toggleNutritionBtn = document.getElementById("toggleNutrition");
const searchBtn = document.getElementById("searchBtn");
ingredientInput.addEventListener("focus", function () {
this.placeholder = "";
});
ingredientInput.addEventListener("blur", function () {
this.placeholder = "Enter ingredients (comma-separated)";
});
async function fetchRecipes(ingredients) {
const url = `https://api.spoonacular.com/recipes/findByIngredients?ingredients=${ingredients}&number=10&apiKey=${apiKey}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const recipes = await response.json();
displayRecipes(recipes);
} catch (error) {
console.error("Error fetching recipes:", error);
recipeContainer.innerHTML = "<p>Failed to fetch recipes. Please try again later.</p>";
}
}
async function fetchRecipeInfo(recipeId) {
const url = `https://api.spoonacular.com/recipes/${recipeId}/nutritionWidget.json?apiKey=${apiKey}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Error fetching nutrition info:", error);
return null;
}
}
async function displayRecipes(recipes) {
recipeContainer.innerHTML = "";
nutritionContainer.innerHTML = "";
if (recipes.length === 0) {
recipeContainer.innerHTML = "<p>No recipes found. Try different ingredients.</p>";
return;
}
for (const recipe of recipes) {
const recipeInfo = await fetchRecipeInfo(recipe.id);
if (!recipeInfo) continue;
const recipeDiv = document.createElement("div");
recipeDiv.classList.add("recipe");
recipeDiv.innerHTML = `
<h2>${recipe.title}</h2>
<img src="${recipe.image}" alt="${recipe.title}">
`;
recipeContainer.appendChild(recipeDiv);
const nutritionDiv = document.createElement("div");
nutritionDiv.classList.add("nutrition-data");
nutritionDiv.innerHTML = `
<p><strong>${recipe.title}</strong></p>
<p>Calories: ${recipeInfo.calories} kcal</p>
<p>Carbs: ${recipeInfo.carbs}</p>
<p>Protein: ${recipeInfo.protein}</p>
<hr>
`;
nutritionContainer.appendChild(nutritionDiv);
}
gsap.from(".recipe", { opacity: 0, y: 50, stagger: 0.2, duration: 0.6 });
}
toggleNutritionBtn.addEventListener("click", function () {
nutritionContainer.classList.toggle("show");
});
searchBtn.addEventListener("click", function () {
const ingredients = ingredientInput.value.trim();
if (ingredients) {
fetchRecipes(ingredients);
}
});