-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfood.js
143 lines (115 loc) · 4.52 KB
/
food.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
var mainIng = "";
$(document).ready(function () {
$(".card").click(function () {
//set main ingredient
mainIng = $(this).attr("id");
console.log("Main Ingredient: " + mainIng);
//set spirit for cocktail based on main ingredient
spirit = getSpirit(mainIng);
console.log("Spirit: " + spirit);
//clear #features div and create row to be populated
$("#features").empty();
var row = $("<div>");
row.addClass("row justify-content-center");
row.attr("id", "choices");
$("#features").append(row);
//Preparing query for food dishes based on mainIng
var queryURL = "https://api.edamam.com/search?q=" + mainIng + "&app_id=a9502a10&app_key=38e9596cea1782797a3e09245c9370fb&from=0&to=100";
console.log("FOOD list query: " + queryURL);
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
console.log(response);
//Generate random numbers to use as indexes from query result
var randomNo = [];
for (var i = 0; i < 6; i++) {
var random = Math.floor(Math.random() * 100);
var genNumber = randomNo.indexOf(random);
if (genNumber === -1) {
randomNo.push(random);
}
else {
i--;
}
}
console.log("Random numbers: " + randomNo);
//Get dishes using random number and get their information
for (var i = 0; randomNo.length; i++) {
var foodImg = response.hits[randomNo[i]].recipe.image;
var foodName = response.hits[randomNo[i]].recipe.label;
// var ingredients = response.hits[randomNo[i]].recipe.ingredientLines;
var ingredients = "";
renderFood(foodImg, foodName, ingredients, randomNo[i]);
}
});
});
});
//Event listener to when a dish selection has been made
$(document).on("click", ".choices", function () {
//Clear row
$("#choices").empty();
//Dish number chosen from list
var number = $(this).attr("data-number");
// --------------------------------------ADD FOOD CARD---------------------
var queryURL = "https://api.edamam.com/search?q=" + mainIng + "&app_id=a9502a10&app_key=38e9596cea1782797a3e09245c9370fb&from=0&to=100";
console.log("Dish query: " + queryURL);
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
console.log(response);
var foodImg = response.hits[number].recipe.image;
var foodName = response.hits[number].recipe.label;
var ingredients = response.hits[number].recipe.ingredientLines;
renderFood(foodImg, foodName, ingredients, number);
});
//Call to cocktail.js to query and generate cocktail
getDrinkID(getCocktail);
});
//Renders food card with given information
function renderFood(foodImg, foodName, ingredients, dishNumber) {
//setting up column and cards to add to the row
var column = $("<div>");
column.addClass("col s12 m6");
var card = $("<div>");
card.addClass("choices card z-depth-4");
//Create dish card
card.attr("data-number", dishNumber);
//set up image with title and button---------
var cardImg = $("<div>");
cardImg.addClass("card-image");
var img = $("<img>");
img.attr("src", foodImg);
//setting up title
var titleSpan = $("<span>");
titleSpan.addClass("card-title");
var title = $("<h3>");
title.text(foodName);
titleSpan.append(title);
cardImg.append(img);
cardImg.append(titleSpan);
//setting up button only if dishes are choices => ingredients string is empty
if (ingredients === "") {
var aTag = $("<a>");
aTag.addClass("btn-floating btn-large btn waves-effect waves-red halfway-fab cyan pulse");
var iTag = $("<i>");
iTag.addClass("material-icons");
iTag.text("add");
aTag.append(iTag);
// ----------------------------------------
cardImg.append(aTag);
}
//done setting up image -------------------
//setting up content-----------------------
var content = $("<div>");
content.addClass("card-content");
var recipe = $("<p>");
recipe.text(ingredients);
content.append(recipe);
//done setting up content / Append everythin to row
card.append(cardImg);
card.append(content);
column.append(card);
$("#choices").append(column);
}