Ver código fonte

Update the analytics page.

Lee Morgan 5 anos atrás
pai
commit
d10a63c409

+ 1 - 2
views/dashboardPage/js/classes/Merchant.js

@@ -481,7 +481,7 @@ class Merchant{
             if(current.ingredient === main) return current.quantity;
             
             for(let i = 0; i < current.ingredient.subIngredients.length; i++){
-                checkIngredient(current.ingredient.subIngredients[i], main);
+                return checkIngredient(current.ingredient.subIngredients[i], main);
             }
 
             return 0;
@@ -494,7 +494,6 @@ class Merchant{
                 let transactionRecipe = this._transactions[i].recipes[j];
                 for(let k = 0; k < transactionRecipe.recipe.ingredients.length; k++){
                     let recipeIngredient = transactionRecipe.recipe.ingredients[k];
-                    let actualIngredient = recipeIngredient.ingredient;
                     
                     total += checkIngredient(recipeIngredient, ingredient);
                 }

+ 17 - 3
views/dashboardPage/js/classes/Transaction.js

@@ -46,13 +46,27 @@ class Transaction{
     getIngredientQuantity(ingredient){
         let quantity = 0;
 
+        let traverseSubIngredients = (current, main)=>{
+            if(current.ingredient === main) return current.quantity;
+
+            for(let i = 0; i < current.ingredient.subIngredients.length; i++){
+                return traverseSubIngredients(current.ingredient.subIngredients[i], main);
+            }
+
+            return 0;
+        }
+
         for(let i = 0; i < this._recipes.length; i++){
             const recipe = this._recipes[i].recipe;
             for(let j = 0; j < recipe.ingredients.length; j++){
-                if(recipe.ingredients[j].ingredient === ingredient){
+                let actualIngredient = recipe.ingredients[j].ingredient;
+                if(actualIngredient === ingredient){
                     quantity += recipe.ingredients[j].quantity * this._recipes[i].quantity;
-
-                    break;
+                }else{
+                    for(let k = 0; k < actualIngredient.subIngredients.length; k++){
+                        quantity += traverseSubIngredients(actualIngredient.subIngredients[i], ingredient) * this._recipes.quantity;
+                    }
+                    
                 }
             }
         }

+ 1 - 3
views/dashboardPage/js/strands/analytics.js

@@ -136,9 +136,7 @@ let analytics = {
     },
 
     displayIngredient: function(){
-        if(this.ingredient === undefined  || this.transactionsByDate.length === 0){
-            return;
-        }
+        if(this.ingredient === undefined  || this.transactionsByDate.length === 0) return;
 
         //break down data into dates and quantities
         let dates = [];