Browse Source

Bug fix: was adding/removing recipes instead of updating, therefore causing referencing issues.

Lee Morgan 5 years ago
parent
commit
c0bc1a1128

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

@@ -361,13 +361,31 @@ class Merchant{
         }
     }
 
+    /*
+    Updates a single recipe
+    recipe: Recipe
+    updates: Object
+    */
+    updateRecipe(recipe, updates){
+        recipe.name = updates.name;
+        recipe.category = updates.category;
+        recipe.hidden = updates.category;
+        recipe.price = updates.price;
+
+        recipe.clearIngredients();
+        for(let i = 0; i < updates.ingredients.length; i++){
+            newIngredient = this.getIngredient(updates.ingredients[i].ingredient);
+            recipe.addIngredient(newIngredient.ingredient, updates.ingredients[i].quantity);
+        }
+
+        recipe.calculateIngredientTotals();
+    }
+
     removeRecipe(recipe){
         const index = this._recipes.indexOf(recipe);
         if(index === undefined) return false;
 
         this._recipes.splice(index, 1);
-
-        state.updateRecipes();
     }
 
     /*

+ 10 - 0
views/dashboardPage/js/classes/Recipe.js

@@ -93,6 +93,10 @@ class Recipe{
         return this._category;
     }
 
+    set category(category){
+        this._category = category;
+    }
+
     get price(){
         return this._price / 100;
     }
@@ -117,6 +121,10 @@ class Recipe{
         return this._ingredients;
     }
 
+    clearIngredients(){
+        this._ingredients = [];
+    }
+
     get ingredientTotals(){
         return this._ingredientTotals;
     }
@@ -138,6 +146,8 @@ class Recipe{
     }
 
     calculateIngredientTotals(){
+        this._ingredientTotals = {};
+        
         let traverseIngredient = (ingredient, multiplier)=>{
             for(let i = 0; i < ingredient.subIngredients.length; i++){
                 traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);                

+ 1 - 2
views/dashboardPage/js/sidebars/editRecipe.js

@@ -113,8 +113,7 @@ let editRecipe = {
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
-                    merchant.removeRecipe(recipe)
-                    merchant.addRecipes([response]);
+                    merchant.updateRecipe(recipe, response);
                     state.updateRecipes();
                     controller.openStrand("recipeBook");
                     controller.createBanner("RECIPE UPDATED", "success");