-
Notifications
You must be signed in to change notification settings - Fork 0
/
mealBuilder.js
197 lines (161 loc) · 6.28 KB
/
mealBuilder.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
/**************************************************************************
* Author: John Konecny
* Date Created: 6/7/2017
* Filename: mealBuilder.js
* Handles adds and deletes for SQL objects
***************************************************************************/
apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
/**************************************************************************
* Adds event listener for search
***************************************************************************/
function bindButtons(){
document.getElementById('foodSearchBtn').addEventListener('click', searchRequest);
}
//assigns get request to search button
function searchRequest(event){
//xml request object to open weather
var req = new XMLHttpRequest();
//clear up the old search results
var searchResults = document.getElementById('searchResults');
clearNodeChildren(searchResults);
searchResults.appendChild(createStatusEle("Loading Search Results..."));
var searchStr = document.getElementById('foodSearchTxt').value;
req.open('GET', 'https://api.nal.usda.gov/ndb/search/?format=json&q=' + searchStr + '&sort=n&max=10&offset=0&api_key=' + apiKey, true);
//perform a get request to get the ndbno of food items which are relevant to searchStr
req.addEventListener('load',function(){
if(req.status >= 200 && req.status < 400){
//clear loading header
var searchResults = document.getElementById('searchResults');
clearNodeChildren(searchResults);
var reqs = [];
var searchResultsObj = JSON.parse(req.responseText);
//get food data for all search results
for(i = 0; i < searchResultsObj.list.item.length; i++)
{
reqs[i] = new XMLHttpRequest();
reqs[i].open('GET', 'https://api.nal.usda.gov/ndb/reports/?ndbno=' + searchResultsObj.list.item[i].ndbno + '&type=b&format=json&api_key=' + apiKey, true);
reqs[i].addEventListener('load',createGetHandler(reqs[i]));
reqs[i].send(null);
event.preventDefault();
}
for(i = 0; i < searchResultsObj.list.item.length; i++){
}
}
else {
var searchResults = document.getElementById('searchResults');
clearNodeChildren(searchResults);
searchResults.appendChild(createStatusEle("Search failed!"));
}
});
req.send(null);
event.preventDefault();
}
//handles the get requests by storing req within this function
//i.e. this is call within a loop of get request req will not be lost
function createGetHandler(req) {
return function(){
if(req.status >= 200 && req.status < 400){
var searchResults = document.getElementById('searchResults');
var itemReport = JSON.parse(req.responseText);
searchResults.appendChild(createResultForm(itemReport));
}
else {
var searchResults = document.getElementById('searchResults');
searchResults.appendChild(createStatusEle("Search failed!"));
}
};
};
//removes all nodes of a given child
function clearNodeChildren(node)
{
node = document.getElementById('searchResults');
while (node.hasChildNodes()) {
node.removeChild(node.lastChild);
}
}
//creates an element with <h3> tags which displays msg
function createStatusEle(msg) {
var header = document.createElement("h3");
header.textContent = msg;
return header;
}
//createResultFrom creates this:
/*<form action = "mealBuilder.php" method="post" onsubmit="return formvalidate(this);">
<fieldset>
<legend>FUN DIP, CANDY, CHERRY YUM DIDDLY DIP, RAZZ APPLE MAGIC DIP, UPC: 079200622411</legend>
<input type="hidden" name="ndbno" value="45124695" readonly="readonly"/>
<input type="hidden" name="name" value="FUN DIP, CANDY, CHERRY YUM DIDDLY DIP, RAZZ APPLE MAGIC DIP, UPC: 079200622411" readonly="readonly"/>
<label>Amount of Food</label>
<input type="number" name="amount" value="1" min="0"/>
<label>Units of Amount (readonly)</label>
<input type="text" name="ru" value="g" readonly="readonly"/>
<label>Calories Per Unit (readonly)</label>
<input type="number" name="energy" value="3.75" readonly="readonly"/>
<input type="hidden" name="cmd" value="add" />
<input type="submit" class="add" value="add" />
</fieldset>
</form>*/
//creates form for user to add a search result to
function createResultForm(itemReport) {
var formEle = document.createElement("form");
formEle.action = "mealBuilder.php";
formEle.method = "post";
fieldset = document.createElement("fieldset");
//get all used information from itemReport
obj = {};
obj.ndbno = itemReport.report.food.ndbno;
obj.name = itemReport.report.food.name;
obj.ru = itemReport.report.food.ru;
obj.energy = 0.0;
//search for nutrients
for(i = 0; i < itemReport.report.food.nutrients.length; i++)
{
if(itemReport.report.food.nutrients[i].nutrient_id == 208)
{
obj.energy = parseFloat(itemReport.report.food.nutrients[i].value) / 100;
}
}
fieldset.appendChild(createLegendEle(obj.name));
fieldset.appendChild(createInputEle("hidden", "ndbno", obj.ndbno, true));
fieldset.appendChild(createInputEle("hidden", "name", obj.name, true));
fieldset.appendChild(createLabelEle("Amount of Food"));
fieldset.appendChild(createInputEle("number", "amount", 0, false));
fieldset.appendChild(createLabelEle("Units of Amount (readonly)"));
fieldset.appendChild(createInputEle("text", "ru", obj.ru, true));
fieldset.appendChild(createLabelEle("Calories Per Unit (readonly)"));
fieldset.appendChild(createInputEle("number", "energy", obj.energy, true));
fieldset.appendChild(createInputEle("hidden", "cmd", "add", false));
fieldset.appendChild(createInputEle("submit", "", "add", false));
formEle.appendChild(fieldset);
return formEle;
}
//creates a legend element
function createLegendEle(msg) {
var legend = document.createElement("legend");
legend.textContent = msg;
return legend;
}
//creates a label element
function createLabelEle(msg) {
var label = document.createElement("label");
label.textContent = msg;
return label;
}
//create an input element
function createInputEle(type, name, value, isReadonly) {
var inputEle = document.createElement("input");
inputEle.type = type;
inputEle.name = name;
inputEle.value = value;
if(isReadonly)
{
inputEle.setAttribute("readOnly","readonly");
}
if(type == "number")
{
inputEle.min = 0;
}
return inputEle;
}
//binds all buttons
bindButtons();