Răsfoiți Sursa

Change editRecipes function to take in an array

Lee Morgan 6 ani în urmă
părinte
comite
b40460e96b

+ 16 - 14
views/dashboardPage/Merchant.js

@@ -158,27 +158,29 @@ class Merchant{
     /*
     Updates a recipe in the merchants list of recipes
     Can create, edit or remove
-    recipe = Recipe object
+    recipe = [Recipe object]
     remove = will remove recipe when true
     */
-    editRecipe(recipe, remove = false){
+    editRecipes(recipes, remove = false){
         let isNew = true;
 
-        for(let i = 0; i < this.recipes.length; i++){
-            if(recipe === this.recipes[i]){
-                if(remove){
-                    this.recipes.splice(i, 1);
-                }else{
-                    this.recipes[i] = recipe;
-                }
+        for(let i = 0; i < recipes.length; i++){
+            for(let j = 0; j < this.recipes.length; j++){
+                if(recipes[i] === this.recipes[j]){
+                    if(remove){
+                        this.recipes.splice(j, 1);
+                    }else{
+                        this.recipes[j] = recipes[i];
+                    }
 
-                isNew = false;
-                break;
+                    isNew = false;
+                    break;
+                }
             }
-        }
 
-        if(isNew){
-            merchant.recipes.push(recipe);
+            if(isNew){
+                merchant.recipes.push(recipes[i]);
+            }
         }
 
         recipeBookStrandObj.populateRecipes();

+ 10 - 2
views/dashboardPage/recipeBook.js

@@ -75,8 +75,9 @@ window.recipeBookStrandObj = {
         })
             .then(response => response.json())
             .then((response)=>{
+                let newRecipes = [];
                 for(let i = 0; i < response.new.length; i++){
-                    merchant.editRecipe(new Recipe(
+                    newRecipes.push(new Recipe(
                         response.new[i]._id,
                         response.new[i].name,
                         response.new[i].price,
@@ -84,15 +85,22 @@ window.recipeBookStrandObj = {
                         []
                     ));
                 }
+                if(newRecipes.length > 0){
+                    merchant.editRecipes(newRecipes);
+                }
 
+                let removeRecipes = [];
                 for(let i = 0; i < response.removed.length; i++){
                     for(let j = 0; j < merchant.recipes.length; j++){
                         if(response.removed[i]._id === merchant.recipes[j].id){
-                            merchant.editRecipe(merchant.recipes[j], true);
+                            removeRecipes.push(merchant.recipes[j], true);
                             break;
                         }
                     }
                 }
+                if(removeRecipes.length > 0){
+                    merchant.editRecipes(removeRecipes, true);
+                }
             })
             .catch((err)=>{
                 banner.createError("SOMETHING WENT WRONG.  PLEASE REFRESH THE PAGE");

+ 3 - 3
views/dashboardPage/sidebars/sidebars.js

@@ -119,7 +119,7 @@ let recipeDetailsComp = {
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-                    merchant.editRecipe(this.recipe);
+                    merchant.editRecipes([this.recipe]);
                     banner.createNotification("RECIPE UPDATE");
                 }
             })
@@ -140,7 +140,7 @@ let recipeDetailsComp = {
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-                    merchant.editRecipe(this.recipe, true);
+                    merchant.editRecipes([this.recipe], true);
                     banner.createNotification("RECIPE REMOVED");
                 }
             })
@@ -853,7 +853,7 @@ let newRecipeComp = {
                         merchant,
                     );
                     
-                    merchant.editRecipe(recipe);
+                    merchant.editRecipes([recipe]);
                     banner.createNotification("RECIPE CREATED");
                 }
             })