-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (98 loc) · 5.73 KB
/
Copy pathscript.js
File metadata and controls
197 lines (98 loc) · 5.73 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
// $(document).ready() makes the function available once the document is loaded.
$(document).ready(function () {
console.log("I'm ready..");
// id information and API key declared as global variable (required for use by developer.edamam.com)
var edamamId = "9f4f3487"
var edamamKey = "8e2d662eda8d8f1307f21875dbd2373b"
// this is the search button on.("click") feature that triggers mealSearchResults function
// when the user clicks search after inputing their query, this kicks it all off..
$("#meal-search-button").on("click", mealSearchResults);
// mealSearchResults is triggered by the search button click
// this takes the users search input and passes it along..
// .. in the form of mealSearchInput variable and gives it to the API call.
// this also calls on the randomDrinkOption API call function to generate
// there is also a scrollIntoView being executed so after clicking the page..
//.. will auto scroll into view.
function mealSearchResults(event) {
event.preventDefault();
var mealSearchInput = $("#mealSearchInput").val();
console.log(mealSearchInput);
getMealRecipe(mealSearchInput);
randomDrinkOption();
document.querySelector("#bottom").scrollIntoView();
}
// checkMealRecipe function has been given the users search criteria..
// ..by the searchResults function, and it will now call on the edamam api..
// HTML elements are created by pre-set id's within DIV's on index.html
// information paths within the JSON information displays the text we want.
// edamam is layed out pretty nicely and it is fairly cut and dry to obtain this.
function getMealRecipe(mealSearchInput) {
var queryURL = `https://api.edamam.com/search?q=${mealSearchInput}&app_id=${edamamId}&app_key=${edamamKey}`
$.ajax({
url: queryURL,
method: "GET"
})
.then(function (data) {
console.log(data);
$("#recipeCardHeader").html("<h3 " + "class='uk-card-title'" + ">" + "HERE'S A RECIPE:" + "</h3>");
$("#recipeTitle").html("<h4>" + data.hits[0].recipe.label + "</h4>");
$("#recipeImage").html("<img " + "style= 'width: 700px' " + "src=" + data.hits[0].recipe.image + ">");
$("#recipeIngredientsLabel").html("<p>" + "INGREDIENTS:" + "</p>");
$("#recipeIngredients").html("<p>" + data.hits[0].recipe.ingredientLines + "</p>");
$("#recipeLinkLabel").html("<p>" + "RECIPE LINK:" + "</p>");
$("#recipeLink").html("<p>" + "<a href=" + data.hits[0].recipe.url + " target=_'blank' " + ">" + "CLICK HERE TO VIEW THE ENTIRE RECIPE" + "</a>" + "</p>");
console.log(data.hits[0].recipe.url);
});
};
// a random cocktail selection will be provided along side of the users generated recipe.
// the cockatailDB random cocktail api is being called on below.
// HTML elements are created by pre-set id's within DIV's on index.html
// information paths within the JSON information displays the text we want.
// unfortunately this API is a personal project and has many blank fields..
// .. that display results of "null".. we don't want this printed on the page..
// .. so a .forEach() work around was implemented to check each entry in the api..
// .. if null is displayed, it is left alone and the next entry is checked.
// we then use .empty() to clear this information upon the next search, and so on..
function randomDrinkOption() {
$("#drinkIngredients span").empty();
$("#drinkInstructions span").empty();
var queryURL = `https://www.thecocktaildb.com/api/json/v1/1/random.php`
$.ajax({
url: queryURL,
method: "GET"
})
.then(function (response) {
console.log(response);
$("#drinkCardHeader").html("<h3 " + "class='uk-card-title'" + ">" + "+ A RANDOM DRINK:" + "</h3>");
$("#drinkIngredientsLabel").html("<p>" + "INGREDIENTS:" + "</p>");
$("#drinkInstructionsLabel").html("<p>" + "INSTRUCTIONS:" + "</p>");
let drink_obj = { ingredients: [] }
let chosen = response.drinks[0];
drink_obj.instructions = chosen.strInstructions.split(".")
drink_obj.title = chosen.strDrink
drink_obj.thumb = chosen.strDrinkThumb
Object.keys(chosen).forEach((key, index) => {
if (chosen[key] != null) {
let new_obj = {}
// check to see if key contains the word ingredient..
if (key.includes("Ingredient")) {
new_obj["name"] = chosen[key]
new_obj["measure"] = chosen[`strMeasure${key.charAt(key.length - 1)}`] || ""
drink_obj.ingredients.push(new_obj)
}
}
})
console.log(drink_obj)
$("#drinkTitle").html("<h4>" + drink_obj.title + "</h4>");
$("#drinkImage").html("<img src=" + drink_obj.thumb + ">");
drink_obj.ingredients.forEach(item => {
$("#drinkIngredients span").append(` ${item.name} ${item.measure}`);
})
drink_obj.instructions.forEach(inst => {
$("#drinkInstructions span").append(`<p>${inst}</p>`);
});
});
};
});
// option that we moved on past for drink search input
// var queryURL = `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${drinkSearchInput}`