Browse Source

Update error handling in otherData.js. Minor bug fixes.

Lee Morgan 6 years ago
parent
commit
76a22cf7ae

+ 1 - 5
controllers/ingredientData.js

@@ -29,13 +29,9 @@ module.exports = {
     //Returns:
     //  ingredients: list containing the newly created ingredients
     createNewIngredients: function(req, res){
-        if(!req.session.user){
-            req.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
         Ingredient.create(req.body)
             .then((ingredients)=>{
+                console.log(ingredients);
                 return res.json(ingredients);
             })
             .catch((err)=>{

+ 2 - 2
controllers/merchantData.js

@@ -469,7 +469,7 @@ module.exports = {
                 
                 recipe.save()
                     .then((recipe)=>{
-                        return res.json();
+                        return res.json({});
                     })
                     .catch((err)=>{
                         let errorMessage = "There was an error and the recipe could not be updated";
@@ -514,7 +514,7 @@ module.exports = {
                         ingredient.quantity = req.body.ingredient.quantity;
                         recipe.save()
                             .then((recipe)=>{
-                                return res.json();
+                                return res.json({});
                             })
                             .catch((err)=>{
                                 let errorMessage = "There was an error and the recipe could not be updated";

+ 19 - 20
views/merchantSetupPage/createIngredients.js

@@ -90,30 +90,12 @@ let createIngredientsObj = {
             }
         }
 
-        if(isValid){
+        if(isValid  && axiosIngredients.length > 0){
             axios.post("/ingredients/create", axiosIngredients)
                 .then((response)=>{
                     if(typeof(response.data) === "string"){
                         banner.createError(response.data);
-                    }else{
-                        for(let ingredient of newIngredients){
-                            for(let createdIngredient of response.data){
-                                if(createdIngredient.name === ingredient.ingredient.name){
-                                    ingredient.ingredient.id = createdIngredient._id;
-                                    break;
-                                }
-                            }
-
-                            controller.data.inventory.push(ingredient);
-                        }
-
-                        banner.createNotification("All ingredients have been created and added to your inventory");
-
-                        if(recipes){
-                            createRecipesObj.display();
-                        }else{
-                            nameRecipesObj.display();
-                        }
+                        return;
                     }
                 })
                 .catch((err)=>{
@@ -121,5 +103,22 @@ let createIngredientsObj = {
                     console.log(err);
                 });
         }
+
+        for(let ingredient of newIngredients){
+            for(let createdIngredient of response.data){
+                if(createdIngredient.name === ingredient.ingredient.name){
+                    ingredient.ingredient.id = createdIngredient._id;
+                    break;
+                }
+            }
+
+            controller.data.inventory.push(ingredient);
+        }
+
+        if(recipes){
+            createRecipesObj.display();
+        }else{
+            nameRecipesObj.display();
+        }
     }
 }

+ 3 - 1
views/recipesPage/recipes.ejs

@@ -12,7 +12,9 @@
         <div id="recipesStrand">
             <h1>Recipes</h1>
 
-            <button id="recipeUpdate" onclick="recipesObj.updateRecipes()">Update Recipes</button>
+            <% if(merchant.pos !== "none"){ %>
+                <button id="recipeUpdate" onclick="recipesObj.updateRecipes()">Update Recipes</button>
+            <% } %>
 
             <a href="/inventory">Return to Inventory</a>
 

+ 1 - 1
views/recipesPage/singleRecipe.js

@@ -183,7 +183,7 @@ let singleRecipeObj = {
 
         let button = row.children[2].children[0];
         button.innerText = "Edit";
-        button.onclick = ()=>{this.editIngredient(row, ingredient);};
+        button.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
 
         axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
             .then((result)=>{