-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (121 loc) · 4.5 KB
/
script.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
//the GAS fetching the edaman data for the basic number of calories, fats, etc
const GAS_URL1 = `https://script.google.com/macros/s/AKfycbwAMKTd1JJT53-0Z-FoFN02gT3Wr1CS8wM9-CUH26h0uFZ3jezmVPBy1sUO0r3oLiAS/exec`
//This is the GAS_URL2 for looking at the specific suggestion, allergy.
const GAS_URL2 = `https://script.google.com/macros/s/AKfycbyD-Nwvaj1OeOpyqpx8RkFvYQ47yWuejWxvXc5Muu0EX1P-R7ptq0ACRVncWM4qAWRxhA/exec`
try {
main()
}
catch (e) {
console.log("Error:" + e.message)
}
function main() {
document.getElementById('btn').addEventListener('click', btnClicked)
}
function btnClicked() {
let value = document.getElementById('inputField').value
getParseData(value)
getParseData2(value)
}
/**
after getting the parsed data, we need to assign it into
our html value*/
async function assignParse(data) {
//console.log(data.text)
// console.log("text: ",data.text)
// console.log("foodid: ",data.parsed[0].food.foodId)
// console.log("kcal: ", data.parsed[0].food.nutrients.ENERC_KCAL)
// console.log("protein: ", data.parsed[0].food.nutrients.PROCNT)
// console.log("fat: ", data.parsed[0].food.nutrients.FAT)
// console.log("kcal: ", data.parsed[0].food.nutrients.ENERC_KCAL)
// console.log("image: ", data.parsed[0].food.image)
// console.log("measure uri: ", data.hints[0].measures[0].uri)
document.getElementById('item').innerHTML = data.text;
document.getElementById('energy').innerHTML = data.parsed[0].food.nutrients.ENERC_KCAL;
document.getElementById('protein').innerHTML = data.parsed[0].food.nutrients.PROCNT;
document.getElementById('fat').innerHTML = data.parsed[0].food.nutrients.FAT;
document.getElementById('image').setAttribute('src', data.parsed[0].food.image)
displayOutput();
// getPost(foodid = data.parsed[0].food.foodId, measureuri = data.hints[0].measures[0].uri);
}
// function getPost(foodid, measureuri) {
// fetch(GAS_URL1, {
// method: 'POST',
// body: JSON.stringify({ ingredients: [{ measureURI: measureuri, foodId: foodid }] })
// })
// .then(response => response.json())
// .then(data => getsuggestion(data))
// .catch(error => console.error(error));
// }
// function getsuggestion(data) {
// console.log(data.healthLabels);
// document.getElementById('suggestion').innerHTML;
// for(let i = 0; i < data.healthLabels.length; i++){
// // document.getElementById('suggestion').innerHTML += data.healthLabels[i] + "<br>";
// document.getElementById('suggestion').innerHTML +=
// `${i+1}: ${data.healthLabels[i]}
// <br>`;
// }
// }
function assignsuggestion(data){
//console.log(data.dietLabels);
//assign the information for diet informations from api
document.getElementById('diet').innerHTML = ""
if(data.dietLabels.length==0){
document.getElementById('diet').innerHTML = "No Diet Informations."
}
for(let i = 0; i < data.dietLabels.length; i++){
document.getElementById('diet').innerHTML +=
`<br>
${i+1}: ${data.dietLabels[i]}
<br>`;
}
document.getElementById('suggestion').innerHTML = ""
//document.getElementById('suggestion').innerHTML;
for(let i = 0; i < data.healthLabels.length-2; i++){
// document.getElementById('suggestion').innerHTML += data.healthLabels[i] + "<br>";
document.getElementById('suggestion').innerHTML +=
`${i+1}: ${data.healthLabels[i]} ${'\xa0'.repeat(10)} ${i+2}: ${data.healthLabels[i+2]}
<br>`;
}
}
function displayOutput() {
document.getElementById('output').hidden = false;
}
/**
this method will call the doGet in google app script*/
async function getParseData(value) {
try {
const url = GAS_URL1 + "?ingr=" + encodeURIComponent(value);
//print out the url for debugging in postman
console.log(url)
const data = await fetchData(url);
}
catch (e) {
console.log("unable to get the nutrient data: " + e.message);
}
}
async function getParseData2(value) {
try{
const url = GAS_URL2 + "?ingr=" + encodeURIComponent(value);
console.log(url)
//start fetching the data that came from the gas2
fetch(url)
.then(response => response.json())
.then(data => {
//console.log(data);
assignsuggestion(data);
})
.catch(error => console.error(error));
}catch (e) {
console.log("unable to get the nutrient data: " + e.message);
}
}
async function fetchData(url) {
fetch(url)
.then(response => response.json())
.then(data => {
//console.log(data);
assignParse(data);
})
.catch(error => console.error(error));
}