Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// storage Controller

const StorageCtrl = function(){
return {
storeItem: function(item){
Expand Down Expand Up @@ -286,13 +284,14 @@ const AppCtrl = function (ItemCtrl, StorageCtrl, UICtrl) {
}

// Add Items Function
function addItems(e) {
async function addItems(e) {
const meal = document.querySelector(UICtrl.getUiSelectors().mealInput);
const calories = document.querySelector(UICtrl.getUiSelectors().caloriesInput);

// ADD ITEMS
if (meal.value !== '' && calories.value !== '') {
const newItem = ItemCtrl.addMeal(meal.value, calories.value);
if (meal.value !== '') {
const fetchedCalories = await fetchCalories(meal.value);
const newItem = ItemCtrl.addMeal(meal.value, fetchedCalories);

// add this item into ui
UICtrl.addListItem(newItem);
Expand All @@ -310,6 +309,25 @@ const AppCtrl = function (ItemCtrl, StorageCtrl, UICtrl) {
e.preventDefault();
}

// Fetch Calories Function
async function fetchCalories(meal) {
const apiKey = 'YOUR_API_KEY';
const apiId = 'YOUR_API_ID';
const response = await fetch(`https://trackapi.nutritionix.com/v2/natural/nutrients`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-app-id': apiId,
'x-app-key': apiKey
},
body: JSON.stringify({
query: meal
})
});
const data = await response.json();
return data.foods[0].nf_calories;
}

//Enable Edit State
const enableEditState = function(e){

Expand Down Expand Up @@ -408,4 +426,4 @@ const AppCtrl = function (ItemCtrl, StorageCtrl, UICtrl) {

}(ItemCtrl,StorageCtrl, UICtrl)

AppCtrl.init();
AppCtrl.init();
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<label for="add-meal">Meal</label>
</div>
<div class="input-field col s6 ">
<input type="number" placeholder="Approx. Calories" id="add-calories" class="white-text" min="0">
<input type="number" placeholder="Approx. Calories" id="add-calories" class="white-text" min="0" disabled>
<label for="add-calories">Calories</label>
</div>
</div>
Expand Down Expand Up @@ -130,4 +130,4 @@ <h5 class="center">Total Calories : <span id="total-calories">0</span></h5>

</body>

</html>
</html>