Pārlūkot izejas kodu

Create button to change default units for ingredients

Lee Morgan 6 gadi atpakaļ
vecāks
revīzija
0dfb923ad9

+ 25 - 0
controllers/merchantData.js

@@ -236,6 +236,31 @@ module.exports = {
             });
     },
 
+    //PUT - Update the default unit for a single ingredient
+    ingredientDefaultUnit: 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)=>{
+                for(let i = 0; i < merchant.inventory.length; i++){
+                    if(merchant.inventory[i].ingredient.toString() === req.params.id){
+                        merchant.inventory[i].defaultUnit =req.params.unit;
+                    }
+                }
+
+                return merchant.save()
+            })
+            .then((merchant)=>{
+                return res.json({});
+            })
+            .catch((err)=>{
+                return res.json("ERROR: UNABLE TO UPDATE DEFAULT UNIT");
+            });
+    },
+
     /*
     POST - Update the quantity for a merchant inventory item
     req.body = [{

+ 1 - 0
routes.js

@@ -19,6 +19,7 @@ module.exports = function(app){
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
     app.post("/merchant/ingredients/add", merchantData.addMerchantIngredient);
     app.delete("/merchant/ingredients/remove/:id", merchantData.removeMerchantIngredient);
+    app.put("/merchant/ingredients/update/:id/:unit", merchantData.ingredientDefaultUnit);
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient);
     app.post("/merchant/password", merchantData.updatePassword);
 

+ 2 - 0
views/dashboardPage/sidebars/ingredientDetails.ejs

@@ -51,5 +51,7 @@
 
     <div id="ingredientButtons" class="ingredientButtons"></div>
 
+    <button id="defaultUnit" class="button" onclick="ingredientDetailsComp.changeUnitDefault()">SET DEFAULT</button>
+
     <button id="editSubmitButton" class="button" onclick="ingredientDetailsComp.editSubmit()" style="display: none;">SAVE CHANGES</button>
 </div>

+ 27 - 0
views/dashboardPage/sidebars/sidebars.js

@@ -790,6 +790,33 @@ let ingredientDetailsComp = {
 
         console.log(newActive);
         newActive.classList.add("unitActive");
+    },
+
+    changeUnitDefault: function(){
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        let id = this.ingredient.ingredient.id;
+        let unit = this.ingredient.ingredient.unit;
+        fetch(`/merchant/ingredients/update/${id}/${unit}`, {
+            method: "put",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+        })
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED");
+                }
+            })
+            .catch((err)=>{
+                banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }
 

+ 0 - 1
views/shared/shared.css

@@ -155,7 +155,6 @@ form{
     text-align: center;
     font-size: 20px;
     font-weight: bold;
-    margin-right: 33px;
     min-width: 100px;
 }