-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
209 lines (177 loc) · 7.03 KB
/
script2.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
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
function getNextKey() {
let key = keyArray[keyArrayIndex];
keyArrayIndex = (keyArrayIndex + 1) % keyArray.length;
return key;
}
const inputEl = document.getElementById("user-input");
const badgesEl = document.getElementById("badges");
badgesEl.style = "display: flex";
//var allIngredients = [];
var ingredientsArray = [];
document.getElementById("add").addEventListener("click", function () {
var userIngredient = inputEl.value;
// ingredientsArray = JSON.parse(localStorage.getItem('ingredients'))
if (ingredientsArray.length >= 3) {
document.getElementById("warning").textContent = "up to 3!!";
} else {
ingredientsArray.push(userIngredient);
//ingredientsArray.push(ingredient);
localStorage.setItem("ingredients", JSON.stringify(ingredientsArray));
}
loadIngredients();
inputEl.value = "";
});
// loadIngredients();
document.getElementById("wand-btn").addEventListener("click", function () {
// call api with all ingredients.
event.preventDefault();
fetchRecipe();
});
function loadIngredients() {
console.log(ingredientsArray);
if (localStorage.getItem("ingredients")) {
ingredientsArray = JSON.parse(localStorage.getItem("ingredients"));
}
badgesEl.innerHTML = "";
for (var i = 0; i < ingredientsArray.length; i++) {
var divEl = document.createElement("div");
var buttonEl = document.createElement("button");
divEl.textContent = " " + ingredientsArray[i] + " ";
divEl.style = "border: 1px solid gold; border-radius: 10px; padding: 3px 5px; margin: 5px";
divEl.setAttribute("id", ingredientsArray[i]);
buttonEl.innerHTML = "<span style='color:red'> X </span>";
buttonEl.classList.add("delete");
buttonEl.setAttribute("id", ingredientsArray[i]);
buttonEl.setAttribute("data-text", ingredientsArray[i]);
divEl.appendChild(buttonEl);
badgesEl.appendChild(divEl);
}
var deleteEl = document.querySelectorAll(".delete");
deleteEl.forEach((button) => {
button.addEventListener("click", function () {
var deleteIngredient = button.parentNode;
deleteIngredient.remove();
var existingArray = JSON.parse(localStorage.getItem("ingredients"));
var ingredientIndex = existingArray.indexOf(`${button.id}`);
if (ingredientIndex > -1) {
existingArray.splice(ingredientIndex, 1);
}
localStorage.setItem("ingredients", JSON.stringify(existingArray));
ingredientsArray = existingArray;
console.log(button.id);
});
});
}
var scroll = document.querySelector('#recipes');
function fetchRecipe() {
// ingredientsArray = JSON.parse(localStorage.getItem('ingredients'))
const stringIngredients = ingredientsArray.join(",");
const options = {
method: "GET",
headers: {
"X-RapidAPI-Key": getNextKey(),
"X-RapidAPI-Host": "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com",
},
};
console.log(stringIngredients);
fetch(
"https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?ingredients=" +
stringIngredients +
"&number=3&ignorePantry=true&ranking=1",
options
)
.then((response) => response.json())
.then((response) => {
recipeToPage(response);
// render page using response variable
scroll.scrollIntoView({ behavior: 'smooth' });
})
.catch((err) => console.error(err));
}
// This function displays 3 recipes on the DOM
function recipeToPage(recipes) {
for (let i = 0; i < recipes.length; i++) {
const recipe = recipes[i];
const recipeDiv = $(`#recipe-${i + 1} .recipe-content`);
recipeDiv.empty();
const contentWrapper = $('<div></div>').addClass('flex flex-col items-center justify-center');
const recipeName = $('<h2></h2>')
.text(recipe.title)
.css({ 'font-size': '1.3rem', 'font-weight': 'bold', 'word-wrap': 'break-word'});
contentWrapper.append(recipeName);
const recipeImage = $('<img></img>')
.attr('src', recipe.image)
.attr('alt', recipe.title)
.addClass('recipe-image');
contentWrapper.append(recipeImage);
const usedIngredientsTitle = $('<h4></h4>')
.text('Dream ingredients:')
.css({ 'font-size': '1.1rem', 'font-weight': 'bold' });
contentWrapper.append(usedIngredientsTitle);
const usedIngredientsList = $('<ul></ul>');
recipe.usedIngredients.forEach(ingredient => {
let usedIngredientName = $('<li></li>').text(ingredient.name);
usedIngredientsList.append(usedIngredientName);
});
contentWrapper.append(usedIngredientsList);
const missedIngredientsTitle = $('<h4></h4>')
.text('Additional ingredients:')
.css({ 'font-size': '1.1rem', 'font-weight': 'bold' });
contentWrapper.append(missedIngredientsTitle)
const missedIngredientsList = $('<ul></ul>');
recipe.missedIngredients.forEach(ingredient => {
let missedIngredientName = $('<li></li>').text(ingredient.name);
missedIngredientsList.append(missedIngredientName);
});
contentWrapper.append(missedIngredientsList);
recipeDiv.append(contentWrapper);
}
}
// This function displays random cocktail on DOM
const Drinkies = {
method: "GET",
headers: {
"X-RapidAPI-Key": getNextKey(),
"X-RapidAPI-Host": "the-cocktail-db.p.rapidapi.com",
},
};
function getRandomCocktail() {
fetch("https://thecocktaildb.p.rapidapi.com/random.php", Drinkies)
.then((response) => response.json())
.then((data) => displayCocktail(data.drinks[0]))
.catch((error) => console.error("Error:", error));
}
// Used jquery for function below
// This function will makes sure that the DOM is fully loaded before the getRandomCocktail function can be called
$(document).ready(function () {
$(".getRandomCocktail").click(getRandomCocktail);
$("#closeModal").click(closeModal);
});
function displayCocktail(cocktail) {
$("#cocktailName").text(cocktail.strDrink);
$("#instructions").text(cocktail.strInstructions);
const ingredientsList = $("#ingredientsList");
ingredientsList.empty();
for (let i = 1; i <= 15; i++) {
const ingredient = cocktail[`strIngredient${i}`];
const measure = cocktail[`strMeasure${i}`];
if (ingredient && measure) {
const li = $("<li></li>").text(`${ingredient} - ${measure}`);
ingredientsList.append(li);
}
}
$("#cocktailModal").removeClass("hidden");
}
function closeModal() {
$("#cocktailModal").addClass("hidden");
}
//Wand Button Bounce :D
const button = document.getElementById("wand-btn");
button.addEventListener("click", function () {
button.classList.add("animate__animated", "animate__bounce");
console.log("Wand clicked!");
});
setTimeout(function () {
button.classList.remove("animate__animated", "animate__bounce");
console.log("Wand clicked!");
}, 1000);