-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
88 lines (70 loc) · 2.67 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
const apiKey = '3447159022774804a3c62660e0902c52';
//const apiKey = 'YOUR_SPOONACULAR_API_KEY';
async function searchRecipe() {
const searchInput = document.getElementById('searchInput').value;
const url = `https://api.spoonacular.com/recipes/complexSearch?query=${searchInput}&apiKey=${apiKey}&addRecipeInformation=true`;
try {
const response = await fetch(url);
const data = await response.json();
displayRecipes(data.results);
} catch (error) {
console.log('Error fetching data: ', error);
}
}
async function getRecipeDetails(recipeId) {
const url = `https://api.spoonacular.com/recipes/${recipeId}/information?apiKey=${apiKey}`;
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.log('Error fetching recipe details: ', error);
}
}
function displayRecipes(recipes) {
const recipeContainer = document.getElementById('recipeContainer');
recipeContainer.innerHTML = '';
recipes.forEach(async recipe => {
const recipeDetails = await getRecipeDetails(recipe.id);
const card = createRecipeCard(recipeDetails);
recipeContainer.appendChild(card);
});
}
function createRecipeCard(recipe) {
const card = document.createElement('div');
card.classList.add('card');
const title = document.createElement('h2');
title.textContent = recipe.title;
const ingredientsTitle = document.createElement('h3');
ingredientsTitle.textContent = 'Ingredients';
const ingredientsList = document.createElement('ul');
recipe.extendedIngredients.forEach(ingredient => {
const li = document.createElement('li');
li.textContent = ingredient.original;
ingredientsList.appendChild(li);
});
const instructionsTitle = document.createElement('h3');
instructionsTitle.textContent = 'Instructions';
const instructionsList = document.createElement('ol');
recipe.analyzedInstructions[0].steps.forEach(step => {
const li = document.createElement('li');
li.textContent = step.step;
instructionsList.appendChild(li);
});
const saveButton = document.createElement('button');
saveButton.textContent = 'Save Recipe';
saveButton.addEventListener('click', () => {
saveRecipe(recipe);
});
card.appendChild(title);
card.appendChild(ingredientsTitle);
card.appendChild(ingredientsList);
card.appendChild(instructionsTitle);
card.appendChild(instructionsList);
card.appendChild(saveButton);
return card;
}
function saveRecipe(recipe) {
// Implement saving logic here
alert('recipe Saved: ', recipe);
}