Przeglądaj źródła

Prevent duplicate ingredients being added on front end.

Lee Morgan 6 lat temu
rodzic
commit
073ed2f05b

+ 15 - 4
views/inventoryPage/addIngredient.js

@@ -19,11 +19,22 @@ let addIngredientObj = {
                 }else{
                     let select = document.querySelector("#addIngredientStrand select");
 
+                    
                     for(let ingredient of response.data){
-                        let option = document.createElement("option");
-                        option.value = ingredient._id;
-                        option.innerText = `${ingredient.name} (${ingredient.unit})`;
-                        select.appendChild(option);
+                        let exists = false;
+                        for(let merchIngredient of merchant.inventory){
+                            if(ingredient._id === merchIngredient.ingredient._id){
+                                exists = true;
+                                break;
+                            }
+                        }
+
+                        if(!exists){
+                            let option = document.createElement("option");
+                            option.value = ingredient._id;
+                            option.innerText = `${ingredient.name} (${ingredient.unit})`;
+                            select.appendChild(option);
+                        }
                     }
                 }
             })

+ 14 - 12
views/recipesPage/singleRecipe.js

@@ -56,10 +56,20 @@ let singleRecipeObj = {
         nameTd.appendChild(name);
 
         for(let item of merchant.inventory){
-            let nameOption = document.createElement("option");
-            nameOption.innerText = `${item.ingredient.name} (${item.ingredient.unit})`;
-            nameOption.value = item.ingredient._id;
-            name.appendChild(nameOption);
+            let exists = false;
+            for(let recipeIngredient of recipe.ingredients){
+                if(item.ingredient._id === recipeIngredient.ingredient._id){
+                    exists = true;
+                    break;
+                }
+            }
+
+            if(!exists){
+                let nameOption = document.createElement("option");
+                nameOption.innerText = `${item.ingredient.name} (${item.ingredient.unit})`;
+                nameOption.value = item.ingredient._id;
+                name.appendChild(nameOption);
+            }
         }
 
         let quantityTd = document.createElement("td");
@@ -86,14 +96,6 @@ let singleRecipeObj = {
             ingredient: ingredientId,
             quantity: quantity
         }
-
-        //Just don't display duplicates...
-        for(let ingredient of recipe.ingredients){
-            if(ingredient.ingredient._id === ingredientId){
-                banner.createError("That ingredient is already in this recipe");
-                return;
-            }
-        }
         
         if(validator.ingredient.quantity(item.quantity)){
             axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})