Explorar el Código

Add ability to remove recipes for non-posers

Lee Morgan hace 6 años
padre
commit
970d5817d0

+ 35 - 0
controllers/merchantData.js

@@ -163,6 +163,41 @@ module.exports = {
             });
     },
 
+    //POST - removes a single recipe
+    //Inputs:
+    // req.body: the id of the recipe to be removed
+    removeRecipe: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                if(merchant.pos === "clover"){
+                    return res.json("Error: you must edit your recipes inside Clover");
+                }
+                
+                for(let i = 0; i < merchant.recipes.length; i++){
+                    if(merchant.recipes[i].toString() === req.body.id){
+                        merchant.recipes.splice(i, 1);
+                        break;
+                    }
+                }
+
+                merchant.save()
+                    .then((updatedMerchant)=>{
+                        return res.json({});
+                    })
+                    .catch((err)=>{
+                        return res.json("Error: unable to save data")
+                    })
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve merchant data");
+            });
+    },
+
     //POST - Adds an ingredient to merchant's inventory
     //Inputs:
     //  req.body: array of objects containing ingredient id and quantity

+ 1 - 0
routes.js

@@ -17,6 +17,7 @@ module.exports = function(app){
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/recipes/update", merchantData.updateRecipes);
+    app.post("/merchant/recipes/remove", merchantData.removeRecipe);
     app.post("/merchant/ingredients/create", merchantData.addMerchantIngredient);
     app.post("/merchant/ingredients/remove", merchantData.removeMerchantIngredient);
     app.post("/merchant/ingredients/update", merchantData.updateMerchantIngredient);

+ 4 - 1
views/dashboardPage/dashboard.ejs

@@ -213,8 +213,11 @@
         <div id="singleRecipeAction" class="action">
             <h2 id="recipeName"></h2>
 
-            <div class="buttonsDiv">
+            <div class="buttonBox">
                 <button class="button" id="addButton">Add Ingredient</button>
+                <% if(merchant.pos === "none"){ %>
+                    <button class="button" id="removeButton">Remove</button>
+                <% } %>
             </div>
 
             <table>

+ 23 - 0
views/dashboardPage/singleRecipe.js

@@ -11,6 +11,7 @@ window.singleRecipeObj = {
 
         document.querySelector("#recipeName").innerText = recipe.name;
         document.querySelector("#addButton").onclick = ()=>{this.displayAdd(recipe)};
+        document.querySelector("#removeButton").onclick = ()=>{this.removeRecipe(recipe)};
 
         for(let ingredient of recipe.ingredients){
             let row = document.createElement("tr");
@@ -259,4 +260,26 @@ window.singleRecipeObj = {
             td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
         }
     },
+
+    removeRecipe(recipe){
+        axios.post("/merchant/recipes/remove", {id: recipe._id})
+            .then((response)=>{
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    for(let i = 0; i < merchant.recipes.length; i++){
+                        if(merchant.recipes[i]._id === recipe._id){
+                            merchant.recipes.splice(i, 1);
+                            break;
+                        }
+                    }
+
+                    window.recipesObj.isPopulated = false;
+                    window.recipesObj.display();
+                }
+            })
+            .catch((err)=>{
+                banner.createError("Warning:  Something went wrong, try refreshing the page");
+            });
+    }
 }