-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (91 loc) · 3.79 KB
/
script.js
File metadata and controls
107 lines (91 loc) · 3.79 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
(function eventHandler() {
const searchBar = document.getElementById("search-bar");
const modalBackground = document.querySelector(".modal-background");
const modal = document.querySelector(".modal");
const errorModal = document.querySelector(".error-modal");
const recipeTitle = document.getElementById("recipe-title");
const servings = document.getElementById("servings");
const ingredientList = document.getElementById("ingredient-list");
const instructionsList = document.getElementById("instructions-list"); // Changed from instruction-list
searchBar.addEventListener("keydown", async (e) => {
if (e.key === "Enter") {
e.preventDefault();
const data = await fetchRecipe(searchBar.value);
if (data) {
openModal(modalBackground, modal, errorModal);
recipeTitle.textContent = data.title;
servings.textContent = data.servings;
// Clear existing ingredients
ingredientList.innerHTML = '';
// Add new ingredients
if (data.ingredients && Array.isArray(data.ingredients)) {
data.ingredients.forEach(ingredient => {
let listItem = document.createElement("li");
listItem.textContent = ingredient;
ingredientList.appendChild(listItem);
});
}
instructionsList.innerHTML = '';
if (data.instructions && typeof data.instructions === 'string') {
const instructions = data.instructions
.split('.')
.filter(instruction => instruction.trim().length > 0);
instructions.forEach(instruction => {
let instructionItem = document.createElement("li");
instructionItem.textContent = instruction.trim();
instructionsList.appendChild(instructionItem);
});
}
}
else {
modalBackground.classList.remove("hidden");
errorModal.classList.remove("hidden");
}
}
});
modalBackground.addEventListener("click", () => {
closeModal(modalBackground, modal, errorModal);
});
})();
async function fetchRecipe(searchInput) {
if (searchInput.trim() === "") {
console.log("empty input!");
return null;
}
const recipeTitle = 'https://api.api-ninjas.com/v2/recipe?ingredients='+searchInput;
const myAPIKey = 'INSERT API KEY HERE';
try {
const response = await fetch(recipeTitle, {
headers: { 'X-Api-Key': myAPIKey }
});
if (!response.ok) {
console.log("Bad response! ", response.status);
document.getElementById("search-bar").value = "";
return null;
}
const data = await response.json();
if (data.length === 0) {
console.log("Something went wrong here.");
document.getElementById("search-bar").value = "";
return null;
}
document.getElementById("search-bar").value = "";
return data[0];
} catch (error) {
console.error("Error fetching recipe:", error);
return null;
}
}
function openModal(modalBackground, modal) {
if (modalBackground && modal) {
modalBackground.classList.remove("hidden");
modal.classList.remove("hidden");
}
}
function closeModal(modalBackground, modal, errorModal) {
if (modalBackground) {
modalBackground.classList.add("hidden");
if (modal) modal.classList.add("hidden");
if (errorModal) errorModal.classList.add("hidden");
}
}