Explorar o código

Bug fix when adding recipes

Lee Morgan %!s(int64=6) %!d(string=hai) anos
pai
achega
98df7a166e
Modificáronse 3 ficheiros con 75 adicións e 43 borrados
  1. 26 26
      controllers/home.js
  2. 16 8
      routes.js
  3. 33 9
      views/recipesPage/recipes.js

+ 26 - 26
controllers/home.js

@@ -191,32 +191,6 @@ module.exports = {
             });
     },
 
-    deleteRecipeIngredient: function(req, res){
-        Recipe.findOne({_id: req.body.recipeId})
-            .populate("ingredients.id")
-            .then((recipe)=>{
-                for(let i = 0; i < recipe.ingredients.length; i++){
-                    if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
-                        recipe.ingredients.splice(i, 1);
-                        break;
-                    }   
-                }
-
-                recipe.save()
-                    .then((recipe)=>{
-                        return res.json(recipe);
-                    })
-                    .catch((err)=>{
-                        console.log(err);
-                        return res.render("error");
-                    });
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
     updateRecipes: function(req, res){
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
@@ -326,5 +300,31 @@ module.exports = {
                 console.log(err);
                 return res.render("error");
             });
+    },
+
+    removeRecipeIngredient: function(req, res){
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
+                for(let i = 0; i < recipe.ingredients.length; i++){
+                    if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
+                        recipe.ingredients.splice(i, 1);
+                        break;
+                    }
+                }
+
+                merchant.save()
+                    .then((result)=>{
+                        return res.json();
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
     }
 }

+ 16 - 8
routes.js

@@ -1,17 +1,25 @@
 const home = require("./controllers/home");
 
 module.exports = function(app){
+    //Render page
     app.get("/", home.displayInventory);
     app.get("/merchant/new", home.merchantSetup);
-    app.get("/getrecipes", home.getCloverRecipes);
-    app.post("/merchant/create", home.createMerchant);
-    app.post("/merchant/update", home.updateMerchant);
-    app.post("/ingredients/create", home.createNewIngredients);
-    app.post("/ingredients/createone", home.createIngredient);
     app.get("/recipes", home.displayRecipes);
-    app.post("/recipes/ingredients/remove", home.deleteRecipeIngredient);
-    app.get("/recipes/update", home.updateRecipes);
-    app.get("/ingredients", home.getIngredients);
+
+    //Merchant
+    app.get("/merchant/recipes/update", home.updateRecipes); //this is fucked
+    app.post("/merchant/create", home.createMerchant);
+    app.post("/merchant/update", home.updateMerchant); //Too generic
     app.post("/merchant/ingredients/create", home.addMerchantIngredient);
     app.post("/merchant/recipes/ingredients/create", home.addRecipeIngredient);
+    app.post("/merchant/recipes/ingredients/remove", home.removeRecipeIngredient);
+
+    //Ingredients
+    app.get("/ingredients", home.getIngredients);
+    app.post("/ingredients/create", home.createNewIngredients);
+    app.post("/ingredients/createone", home.createIngredient);  //also adds to merchant
+
+    //Clover API
+    
+    app.get("/getrecipes", home.getCloverRecipes);
 }

+ 33 - 9
views/recipesPage/recipes.js

@@ -109,10 +109,10 @@ let recipesPage = {
         let saveButton = document.createElement("button");
         saveButton.innerText = "Save";
         actionTd.appendChild(saveButton);
-        saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value);};
+        saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
     },
 
-    addIngredient: function(recipe, ingredientId, quantity){
+    addIngredient: function(recipe, ingredientId, quantity, row){
         let item = {
             ingredient: ingredientId,
             quantity: quantity
@@ -123,16 +123,41 @@ let recipesPage = {
                 let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
                 recipe.ingredients.push({
                     ingredient: addIngredient.ingredient,
-                    quantity: quantity
+                    quantity: item.quantity
                 });
+
+                //Change row from displaying options to showing default display
+                while(row.children.length > 0){
+                    row.removeChild(row.firstChild);
+                }
+
+                let name = document.createElement("td");
+                name.innerText = addIngredient.ingredient.name;
+                row.appendChild(name);
+
+                let quantity = document.createElement("td");
+                quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
+                row.appendChild(quantity);
+
+                let actions = document.createElement("td");
+                row.appendChild(actions);
+
+                let editButton = document.createElement("button");
+                editButton.innerText = "Edit";
+                editButton.onclick = ()=>{this.editIngredient(row, addIngredient);};
+                actions.appendChild(editButton);
+
+                let removeButton = document.createElement("button");
+                removeButton.innerText = "Remove";
+                removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
+                actions.appendChild(removeButton);
+
                 banner.createNotification("Ingredient successfully added to database");
             })
             .catch((err)=>{
+                row.parentNode.removeChild(row);
                 console.log(err);
                 banner.createError("There was an error and the recipe could not be updated");
-            })
-            .finally(()=>{
-                this.displayOneRecipe(recipe._id);
             });
     },
 
@@ -149,9 +174,8 @@ let recipesPage = {
             }
         }
         
-        axios.post("/merchant/update", merchant)
+        axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
             .then((result)=>{
-                merchant = result;
                 banner.createNotification("Ingredient has been removed from recipe");
             })
             .catch((err)=>{
@@ -204,7 +228,7 @@ let recipesPage = {
     },
 
     updateRecipes: function(){
-        axios.get("/recipes/update")
+        axios.get("/merchant/recipes/update")
             .then((result)=>{
                 merchant = result.data.merchant;
                 this.displayRecipes();