forked from cbragg9/Recipe-Master
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
284 lines (251 loc) · 9.21 KB
/
Copy pathscript.js
File metadata and controls
284 lines (251 loc) · 9.21 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
$(document).ready(function () {
// U.S. DEPARTMENT OF AGRICULTURE
// FoodData Central API
// When a food ingredient button is clicked
$(document).on("click", ".ingredient-button", function () {
// The button title will be the query food
var ingredientQuery = $(this).text();
// Settings for AJAX call
var fdc_api_key = "RlTgUNeWpu2FIFPDd0AmRrHssiC7e96O5TKQSGEc";
var dataType = "Branded";
var settings = {
async: true,
crossDomain: true,
url:
"https://api.nal.usda.gov/fdc/v1/foods/search?api_key=" +
fdc_api_key +
"&query=" +
ingredientQuery +
"&dataType=" +
dataType,
method: "GET",
};
// Ajax call
$.ajax(settings).done(function (response) {
// Update HTML to display what was searched
$("#ingredient-name").text("Nutrition info for: " + ingredientQuery);
var nutrientValue = "";
// Information for nutrients that will be displayed
var nutrientsArray = [
{
nutrientName: "Energy",
nutrientHTML: "#calories-tag",
nutrientDisplay: "Calories: ",
nutrientExists: "false",
},
{
nutrientName: "Protein",
nutrientHTML: "#protein-tag",
nutrientDisplay: "Protein: ",
nutrientExists: "false",
},
{
nutrientName: "Carbohydrate, by difference",
nutrientHTML: "#carbs-tag",
nutrientDisplay: "Carbs: ",
nutrientExists: "false",
},
{
nutrientName: "Sugars, total including NLEA",
nutrientHTML: "#sugars-tag",
nutrientDisplay: "Sugar: ",
nutrientExists: "false",
},
{
nutrientName: "Total lipid (fat)",
nutrientHTML: "#fat-tag",
nutrientDisplay: "Fat: ",
nutrientExists: "false",
},
{
nutrientName: "Fiber, total dietary",
nutrientHTML: "#fiber-tag",
nutrientDisplay: "Fiber: ",
nutrientExists: "false",
},
{
nutrientName: "Cholesterol",
nutrientHTML: "#cholesterol-tag",
nutrientDisplay: "Cholesterol: ",
nutrientExists: "false",
},
];
// Loop through food nutrients array for the first food item from the response
for (var i = 0; i < response.foods[0].foodNutrients.length; i++) {
updateNutrient();
}
// Updates the HTML tags for each nutrient
function updateNutrient() {
for (var j = 0; j < nutrientsArray.length; j++) {
var currentNutrientName = nutrientsArray[j].nutrientName;
var currentNutrientDisplay = nutrientsArray[j].nutrientDisplay;
var currentNutrientHTML = nutrientsArray[j].nutrientHTML;
// Update the corresponding HTML with the current nutrient info
if (
response.foods[0].foodNutrients[i].nutrientName ===
currentNutrientName
) {
nutrientValue = response.foods[0].foodNutrients[i].value;
nutrientsArray[j].nutrientExists = "true";
// Add units if the nutrient is not calories
if (currentNutrientName === "Energy") {
$(currentNutrientHTML).text(
currentNutrientDisplay + nutrientValue + " kcal"
);
} else if (currentNutrientName === "Cholesterol") {
$(currentNutrientHTML).text(
currentNutrientDisplay + nutrientValue + " mg"
);
} else {
$(currentNutrientHTML).text(
currentNutrientDisplay + nutrientValue + " g"
);
}
// Display N/A if nutrient is not found in API response
} else if (nutrientsArray[j].nutrientExists === "false") {
$(currentNutrientHTML).text(currentNutrientDisplay + "N/A");
}
}
}
});
});
// Created button that will display into pantry list box when a user submits
// an ingredient.
$("#pantrySearchBtn").on("click", function createBtn() {
var userInput = document.getElementById("pantryText");
$("#pantry-search-form").submit(function () {
createBtn();
return false;
});
if (userInput.value == "" || userInput.value == null) {
return false;
} else {
var pantryInput = document.createElement("button");
pantryInput.setAttribute(
"class",
"button new-ingredient-button is-danger is-light is-rounded"
);
pantryInput.setAttribute("id", "pantryListBtn");
pantryInput.textContent = userInput.value;
document
.getElementById("prepend-ingredients-here")
.appendChild(pantryInput);
$("#pantryText").val("");
}
});
// Created event handler for clear all button that will clear all ingredients buttons
// from pantry list
$("#clearAllBtn").on("click", function () {
var clearBtn = document.querySelectorAll(".new-ingredient-button");
for (var f = 0; f < clearBtn.length; f++) {
clearBtn[f].remove();
}
});
var userIngredients = "";
// Set Click to Search button
$("#search").on("click", function () {
//Remove all content before rendering
$(".is-3").remove();
userIngredients = "";
var userButtons = $(".new-ingredient-button");
for (i = 0; i < userButtons.length; i++) {
var button = userButtons[i];
var str = button.innerHTML;
userIngredients += str;
if (i < userButtons.length - 1) {
userIngredients += ",";
}
}
var pageRandom = Math.floor(Math.random() * 10) + 1;
var pageNumber = "&p=" + pageRandom;
var settings = {
url:
"https://recipe-puppy.p.rapidapi.com/?i=" +
userIngredients +
pageNumber,
method: "GET",
headers: {
"x-rapidapi-host": "recipe-puppy.p.rapidapi.com",
"x-rapidapi-key": "d8b37011d5mshfdc852418e9e300p167785jsn0d94302f0b12",
},
};
// AJAX Recipe Puppy
$.ajax(settings).then(function (response) {
var recipePuppyResponse = JSON.parse(response);
console.log(recipePuppyResponse);
// List all ingredients
for (i = 0; i < 4; i++) {
// Get title and remove carriage returns
var recipeTitle = recipePuppyResponse.results[i].title.replace(
/[\n\r]/g,
""
);
var recipeImage = recipePuppyResponse.results[i].thumbnail;
if (recipePuppyResponse.results[i].thumbnail === "") {
recipeImage = "./docs/loading-image.png";
}
var recipeLink = recipePuppyResponse.results[i].href;
renderRecipe();
}
function renderRecipe() {
//Create Column div for Card
var recipeCardColumn = $("<div>");
recipeCardColumn.addClass("column is-3");
//Create anchor and assign recipe link to it
var recipeCardAnchor = $("<a target=_blank>");
recipeCardAnchor.attr("href", recipeLink);
//Create Card to place recipe image and name into it
var recipeCard = $("<div>");
recipeCard.addClass("card");
var recipeCardImage = $("<div>");
recipeCardImage.addClass("card-image");
var recipeCardFigure = $("<figure>");
recipeCardFigure.addClass("image is-4by3");
var recipeFigureImage = $("<img>");
recipeFigureImage.attr("src", recipeImage);
//Append child class into parent
recipeCardFigure.append(recipeFigureImage);
recipeCardImage.append(recipeCardFigure);
var recipeCardTitle = $("<div>");
recipeCardTitle.addClass("card-content");
recipeCardTitle.attr("data-name", recipeTitle);
recipeCardTitle.attr(
"data-ingredients",
recipePuppyResponse.results[i].ingredients
);
recipeCardTitle.text(recipeTitle);
//Append child class into parent and into HTML
recipeCard.append(recipeCardImage, recipeCardTitle);
recipeCardAnchor.append(recipeCard);
recipeCardColumn.append(recipeCardAnchor);
$("#append-three-cards-here-1").append(recipeCardColumn);
// Change the padding after you "search" is clicked
$("#recipe-cards-section").css("padding", "5%");
}
});
// Onclick handler to display ingredients
$(document).on("click", ".card", function () {
$("#append-recipe-ingredients-here").html("");
var ingredientList = $(this).children("div.card-content");
console.log(ingredientList);
ingredientList = ingredientList.data("ingredients");
var ingredientListArray = ingredientList.split(", ");
for (var i = 0; i < ingredientListArray.length; i++) {
var ingredientName = ingredientListArray[i];
var newButtonEl = $("<button>");
newButtonEl.addClass("button ingredient-button");
if (userIngredients.toLowerCase().includes(ingredientName)) {
newButtonEl.addClass("is-primary");
} else {
newButtonEl.addClass("is-danger");
}
ingredientName = capitalizeFirstLetter(ingredientName);
newButtonEl.text(ingredientName);
$("#append-recipe-ingredients-here").append(newButtonEl);
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
});
});
});