-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (147 loc) · 6.42 KB
/
index.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
let prev = document.getElementById("ingredient");
async function getData() {
let ingredients = document.getElementById("ingredient");
let count = 0;
if (prev != ingredients) {
document.body.innerHTML = `<div class = "header"><br><br>
<h1>What's in your fridge today?</h1><br>
<h4>Create something delicious with what you have at home!</h4><br><br>
<input id = "ingredient"type="text" placeholder = "Cheese, Tomatoes, etc..."><br><br><br>
<button id = "btn" onclick = "getData()" type = "button">Search</button>
</div>`;
}
prev = ingredients;
let url = `https://api.spoonacular.com/recipes/findByIngredients?apiKey=${api_key}&ingredients=${ingredients.value}`;
let response = await fetch(url);
let data = await response.json();
console.log(data);
let missingIngredients = [];
let unusedIngredients = [];
let itemName = "", missedCount = 0, usedCount = 0;
for (let x = 0; x < data.length; x++) {
const {image,
missedIngredientCount : missedCount,
missedIngredients,
title : itemName,
usedIngredientCount : usedCount,
usedIngredients} = data[x];
for (let x = 0; x < missedIngredients.length; x++) {
const {original} = missedIngredients[x];
missingIngredients.push(original);
}
for (let x = 0; x < usedIngredients.length; x++) {
const {original} = usedIngredients[x];
unusedIngredients.push(original);
}
load(image, itemName, percentOfIngredients(missedCount, usedCount), count);
missingIngredients = [];
unusedIngredients = [];
count++;
}
count = 0;
while (count < data.length) {
let id = getId(count, data);
let url_2 = `https://api.spoonacular.com/recipes/${id}/information?apiKey=${api_key}`;
let response_2 = await fetch(url_2);
let data_2 = await response_2.json();
const {sourceUrl} = data_2;
loadrecipes(getIngredients(count, data), getName(count, data), percentageYouHave(count, data), count, sourceUrl);
count++;
}
}
function getIngredients(count, data) {
let getIngredients = "";
const {missedIngredients} = data[count];
for (let x = 0; x < missedIngredients.length; x++) {
const {original} = missedIngredients[x];
getIngredients += original + "⭐";
}
const {usedIngredients} = data[count];
for (let x = 0; x < usedIngredients.length; x++) {
const {original} = usedIngredients[x];
getIngredients += original + "⭐";
}
console.log(getIngredients);
return getIngredients;
}
function percentageYouHave(count, data) {
const {missedIngredientCount, usedIngredientCount } = data[count];
return percentOfIngredients(missedIngredientCount, usedIngredientCount);
}
function getName(count, data) {
const {title} = data[count];
return title;
}
function getId(count, data) {
const {id} = data[count];
return id;
}
async function load(image, name, percentage, count) {
document.body.innerHTML += `<div class = "content">
<img src = "${image}">
<div class = "posting" id = "posting${count}">
<h2 id = "name" class = "child">${name}</h2><br>
<h6 class = "child">You have ${percentage}% of the ingredients</h6><br>
<button class = "child" id = "ingred${count}">View Ingredients</button><br><br>
<button class = "child" id = "viewIngredients">View Recipe</button>
</div>
</div>`
}
function loadrecipes(ingredientsList, title, percentage, count, url) { // url is for View Recipe - just need to add it to the button
console.log(url);
let content = document.getElementById(`ingred${count}`);
let posting = document.getElementById(`posting${count}`);
content.classList.add("addStyle");
content.addEventListener("click", (event) => {
while (posting.firstChild) {
posting.removeChild(posting.lastChild);
}
const heading = document.createElement("h2");
const ingredients = document.createElement("h6");
const img = document.createElement("img");
heading.className = "h2";
img.src = "images/back.png";
img.id = `image${count}`;
img.classList.add("image");
heading.appendChild(document.createTextNode("Ingredients"));
ingredients.appendChild(document.createTextNode(ingredientsList));
posting.appendChild(heading);
posting.appendChild(ingredients);
posting.appendChild(img);
document.getElementById(`image${count}`).addEventListener("click", (event) => {
while (posting.firstChild) {
posting.removeChild(posting.lastChild);
}
const name = document.createElement("h2");
const quantity = document.createElement("h6");
const btn1 = document.createElement("button");
const btn2 = document.createElement("button");
let linebreak = document.createElement("br");
name.id = "name";
btn1.id = "ingred";
btn2.id = "viewIngredients";
name.className = "child";
quantity.className = "child";
btn1.className = "child";
btn2.className = "child";
name.appendChild(document.createTextNode(title));
quantity.appendChild(document.createTextNode(`You have ${percentage}% of the ingredients`));
btn1.appendChild(document.createTextNode("View Ingredients"));
btn2.appendChild(document.createTextNode("View Recipe"));
posting.appendChild(name);
posting.appendChild(linebreak);
posting.appendChild(quantity);
linebreak = document.createElement("br");
posting.appendChild(linebreak);
posting.appendChild(btn1);
linebreak = document.createElement("br");
posting.appendChild(linebreak);
linebreak = document.createElement("br");
posting.appendChild(linebreak);
posting.appendChild(btn2);
})
})
}
function percentOfIngredients(missing, not_missing) {
return (not_missing / (missing + not_missing)).toFixed(2);
}