forked from ShikhaDhiman1/Healthy-Me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dietPage.js
52 lines (51 loc) · 1.83 KB
/
dietPage.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
// Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
import wixData from "wix-data";
import { local } from "wix-storage";
$w.onReady(function () {
let totalCalories = 0;
let foodItems = [];
$w("#myProgressBar").targetValue = 2000;
$w("#myProgressBar").value = totalCalories;
$w("#totalCal").html = `<h6>Total Calories : ${totalCalories}</h6>`;
$w("#msg").hide();
$w("#addFood").onClick(function () {
console.log("clicked");
const myCollection = "myCollection";
wixData
.query(myCollection)
.eq("foodItem", $w("#inputText").value)
.find()
.then((results) => {
if (results.items.length > 0) {
let foodItem = results.items[0];
console.log(results.items[0]); //see firstItem below
$w(
"#displayFood"
).html += `<center><pre><h2 style="color:green;">${foodItem["foodItem"]} ${foodItem["cals_per100Grams"]} cal</h2></pre> </center><br>`;
let obj = {
food: foodItem["foodItem"],
cal: foodItem["cals_per100Grams"],
};
foodItems.push(obj);
local.setItem("listItems", foodItems);
totalCalories += foodItem["cals_per100Grams"];
local.setItem("totalCal", totalCalories);
$w("#myProgressBar").value = totalCalories;
$w("#totalCal").html = `<h6>Total Calories : ${totalCalories}</h6>`;
if (totalCalories >= 2000) {
$w("#msg").text =
"Any more Calorie and you'll ruin your balanced diet for today!!!";
$w("#msg").show();
} else {
$w("#msg").hide();
}
} else {
// handle case where no matching
console.log("No such food item exists");
}
})
.catch((err) => {
console.log(err);
});
});
});