Przeglądaj źródła

Update state handling for adding a new recipe.

Lee Morgan 5 lat temu
rodzic
commit
7b6dc769fb

+ 31 - 32
views/dashboardPage/bundle.js

@@ -119,9 +119,9 @@ const Recipe = require("./Recipe.js");
 const Transaction = require("./Transaction.js");
 const Order = require("./Order.js");
 
-const home = require("../strands/home.js");
+const homeStrand = require("../strands/home.js");
 const ingredientsStrand = require("../strands/ingredients.js");
-const recipeBook = require("../strands/recipeBook");
+const recipeBookStrand = require("../strands/recipeBook");
 const analytics = require("../strands/analytics.js");
 const orders = require("../strands/orders");
 
@@ -327,7 +327,7 @@ class Merchant{
 
         this._ingredients.splice(index, 1);
 
-        home.drawInventoryCheckCard();
+        homeStrand.drawInventoryCheckCard();
         ingredientsStrand.populateByProperty();
     }
 
@@ -343,12 +343,29 @@ class Merchant{
         return this._recipes;
     }
 
-    addRecipe(id, name, price, ingredients){
-        let recipe = new Recipe(id, name, price, ingredients, this);
-
-        this._recipes.push(recipe);
+    /*
+    recipes: [{
+        _id: String
+        name: String
+        price: Number
+        ingredients: [{
+            ingredient: String (id)
+            quantity: Number
+        }]
+    }]
+    */
+    addRecipes(recipes){
+        for(let i = 0; i < recipes.length; i++){
+            this._recipes.push(new Recipe(
+                recipes[i]._id,
+                recipes[i].name,
+                recipes[i].price,
+                recipes[i].ingredients,
+                this
+            ));
+        }
 
-        recipeBook.isPopulated = false;
+        recipeBookStrand.populateRecipes();
     }
 
     removeRecipe(recipe){
@@ -359,7 +376,7 @@ class Merchant{
 
         this._recipes.splice(index, 1);
 
-        recipeBook.isPopulated = false;
+        recipeBookStrand.populateRecipes();
     }
 
     /*
@@ -396,7 +413,7 @@ class Merchant{
             }
         }
 
-        recipeBook.isPopulated = false;
+        recipeBookStrand.isPopulated = false;
     }
 
     get transactions(){
@@ -455,7 +472,7 @@ class Merchant{
             }
         }
 
-        home.isPopulated = false;
+        homeStrand.isPopulated = false;
         ingredientsStrand.isPopulated = false;
         analytics.newData = true;
     }
@@ -491,7 +508,7 @@ class Merchant{
             }
         }
 
-        home.isPopulated = false;
+        homeStrand.isPopulated = false;
         ingredientsStrand.isPopulated = false;
         analytics.newData = true;
     }
@@ -2198,26 +2215,7 @@ let newRecipe = {
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
-                    let ingredients = [];
-                    for(let i = 0; i < response.ingredients.length; i++){
-                        for(let j = 0; j < merchant.ingredients.length; j++){
-                            if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
-                                ingredients.push({
-                                    ingredient: merchant.ingredients[j].ingredient.id,
-                                    quantity: response.ingredients[i].quantity
-                                });
-
-                                break;
-                            }
-                        }
-                    }
-
-                    merchant.addRecipe(
-                        response._id,
-                        response.name,
-                        response.price,
-                        ingredients
-                    );
+                    merchant.addRecipes([response]);
 
                     controller.createBanner("RECIPE CREATED", "success");
                     controller.openStrand("recipeBook");
@@ -3864,6 +3862,7 @@ let recipeBook = {
             if(merchant.pos !== "none"){
                 document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate()};
             }
+            
             document.getElementById("recipeSearch").oninput = ()=>{this.search()};
 
             this.populateRecipes();

+ 29 - 12
views/dashboardPage/js/classes/Merchant.js

@@ -3,9 +3,9 @@ const Recipe = require("./Recipe.js");
 const Transaction = require("./Transaction.js");
 const Order = require("./Order.js");
 
-const home = require("../strands/home.js");
+const homeStrand = require("../strands/home.js");
 const ingredientsStrand = require("../strands/ingredients.js");
-const recipeBook = require("../strands/recipeBook");
+const recipeBookStrand = require("../strands/recipeBook");
 const analytics = require("../strands/analytics.js");
 const orders = require("../strands/orders");
 
@@ -211,7 +211,7 @@ class Merchant{
 
         this._ingredients.splice(index, 1);
 
-        home.drawInventoryCheckCard();
+        homeStrand.drawInventoryCheckCard();
         ingredientsStrand.populateByProperty();
     }
 
@@ -227,12 +227,29 @@ class Merchant{
         return this._recipes;
     }
 
-    addRecipe(id, name, price, ingredients){
-        let recipe = new Recipe(id, name, price, ingredients, this);
-
-        this._recipes.push(recipe);
+    /*
+    recipes: [{
+        _id: String
+        name: String
+        price: Number
+        ingredients: [{
+            ingredient: String (id)
+            quantity: Number
+        }]
+    }]
+    */
+    addRecipes(recipes){
+        for(let i = 0; i < recipes.length; i++){
+            this._recipes.push(new Recipe(
+                recipes[i]._id,
+                recipes[i].name,
+                recipes[i].price,
+                recipes[i].ingredients,
+                this
+            ));
+        }
 
-        recipeBook.isPopulated = false;
+        recipeBookStrand.populateRecipes();
     }
 
     removeRecipe(recipe){
@@ -243,7 +260,7 @@ class Merchant{
 
         this._recipes.splice(index, 1);
 
-        recipeBook.isPopulated = false;
+        recipeBookStrand.populateRecipes();
     }
 
     /*
@@ -280,7 +297,7 @@ class Merchant{
             }
         }
 
-        recipeBook.isPopulated = false;
+        recipeBookStrand.isPopulated = false;
     }
 
     get transactions(){
@@ -339,7 +356,7 @@ class Merchant{
             }
         }
 
-        home.isPopulated = false;
+        homeStrand.isPopulated = false;
         ingredientsStrand.isPopulated = false;
         analytics.newData = true;
     }
@@ -375,7 +392,7 @@ class Merchant{
             }
         }
 
-        home.isPopulated = false;
+        homeStrand.isPopulated = false;
         ingredientsStrand.isPopulated = false;
         analytics.newData = true;
     }

+ 1 - 20
views/dashboardPage/js/sidebars/newRecipe.js

@@ -97,26 +97,7 @@ let newRecipe = {
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
-                    let ingredients = [];
-                    for(let i = 0; i < response.ingredients.length; i++){
-                        for(let j = 0; j < merchant.ingredients.length; j++){
-                            if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
-                                ingredients.push({
-                                    ingredient: merchant.ingredients[j].ingredient.id,
-                                    quantity: response.ingredients[i].quantity
-                                });
-
-                                break;
-                            }
-                        }
-                    }
-
-                    merchant.addRecipe(
-                        response._id,
-                        response.name,
-                        response.price,
-                        ingredients
-                    );
+                    merchant.addRecipes([response]);
 
                     controller.createBanner("RECIPE CREATED", "success");
                     controller.openStrand("recipeBook");

+ 1 - 0
views/dashboardPage/js/strands/recipeBook.js

@@ -11,6 +11,7 @@ let recipeBook = {
             if(merchant.pos !== "none"){
                 document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate()};
             }
+            
             document.getElementById("recipeSearch").oninput = ()=>{this.search()};
 
             this.populateRecipes();