Kaynağa Gözat

Can now submit subingredients directly from the modal.

Lee Morgan 5 yıl önce
ebeveyn
işleme
f75ffee761
3 değiştirilmiş dosya ile 51 ekleme ve 20 silme
  1. 14 13
      controllers/ingredientData.js
  2. 0 1
      routes.js
  3. 37 6
      views/dashboardPage/js/modal.js

+ 14 - 13
controllers/ingredientData.js

@@ -58,7 +58,7 @@ module.exports = {
     },
 
     /*
-    POST: Updates data for a single ingredient
+    PUT: Updates data for a single ingredient
     req.body = {
         id: id of the ingredient,
         name: new name of the ingredient,
@@ -74,12 +74,10 @@ module.exports = {
     error response = '$' delimited String
     */
     updateIngredient: function(req, res){
-        let popMerchant = res.locals.merchant.populate("inventory.ingredient").execPopulate();
-
-        Promise.all([Ingredient.findOne({_id: req.body.id}), popMerchant])
+        Ingredient.findOne({_id: req.body.id})
             .then((response)=>{
-                response[0].name = req.body.name;
-                response[0].category = req.body.category;
+                response.name = req.body.name;
+                response.category = req.body.category;
                 
                 //find and update ingredient on merchant
                 for(let i = 0; i < res.locals.merchant.inventory.length; i++){
@@ -100,7 +98,7 @@ module.exports = {
                         break;
                     }
                 }
-                return Promise.all([response[0].save(), res.locals.merchant.save()])
+                return Promise.all([response.save(), res.locals.merchant.save()])
             })
             .then((response)=>{
                 return res.json({
@@ -121,22 +119,25 @@ module.exports = {
     PUT: updates subingredients on an ingredient
     req.body = {
         id: String (top-level ingredient id),
-        ingredients: {
+        ingredients: [{
             ingredient: String (id)
             quantity: Number
-        }
+        }]
     }
     response = Ingredient
     error response = '$' delimited String
     */
     updateSubIngredients: function(req, res){
+        let popMerchant = res.locals.merchant.populate("inventory.ingredient").execPopulate();
+
         let stack = [];
-        Ingredient.findOne({_id: req.body.id})
+        Promise.all([Ingredient.findOne({_id: req.body.id}), popMerchant])
             .then((response)=>{
-                response.ingredients = req.body.ingredients;
+                response[0].ingredients = req.body.ingredients;
 
                 // Check ingredients for circular references
                 let isCircular = (ingredient, original)=>{
+
                     if(ingredient.ingredients.length === 0) {
                         stack.pop();
                         return false;
@@ -160,13 +161,13 @@ module.exports = {
                             let ingredient = res.locals.merchant.inventory[j].ingredient;
                             stack = [ingredient];
                             if(ingredient._id.toString() === req.body.id) throw "circular";
-                            if(isCircular(ingredient, response) === true) throw "circular";
+                            if(isCircular(ingredient, response[0]) === true) throw "circular";
                             break;
                         }
                     }
                 }
 
-                return response.save();
+                return response[0].save();
             })
             .then((ingredient)=>{
                 return res.json(ingredient);

+ 0 - 1
routes.js

@@ -24,7 +24,6 @@ module.exports = function(app){
     app.get("/resetpassword/*", renderer.displayPassReset);
     
     //Merchant
-    
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.post("/merchant/add/none", session, merchantData.addMerchantNone);
     app.put("/merchant/ingredients/update", session, merchantData.updateIngredientQuantities); //also updates some data in ingredients

+ 37 - 6
views/dashboardPage/js/modal.js

@@ -14,7 +14,7 @@ let modal = {
             title: document.getElementById("feedbackTitle").value,
             content: document.getElementById("feedbackContent").value,
             date: new Date()
-        }
+        };
 
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
@@ -224,19 +224,50 @@ let modal = {
 
         //SUBMIT SUB INGREDIENTS
         document.getElementById("submitEditSubIngredients").onclick = ()=>{
-            let subIngredients = [];
+            let data = {
+                id: ingredient.id,
+                ingredients: []
+            };
 
             for(let i = 1; i < right.children.length; i++){
-                subIngredients.push({
+                data.ingredients.push({
                     ingredient: right.children[i].ingredient.id,
                     quantity: parseFloat(right.children[i].children[1].children[0].value)
                 });
             }
 
-            ingredient.replaceIngredients(subIngredients);
+            console.log(data);
+
+            let loader = document.getElementById("loaderContainer");
+            loader.style.display = "flex";
+
+            fetch("/ingredients/subingredients", {
+                method: "put",
+                headers: {
+                    "Content-Type": "application/json"
+                },
+                body: JSON.stringify(data)
+            })
+                .then(response => response.json())
+                .then((response)=>{
+                    if(typeof(response) === "string"){
+                        controller.createBanner(response, "error");
+                    }else{
+                        console.log(response);
+                    }
+                })
+                .catch((err)=>{
+                    console.log(err);
+                    controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
+                })
+                .finally(()=>{
+                    loader.style.display = "none";
+                });
+
+            // ingredient.replaceIngredients(subIngredients);
 
-            controller.closeModal();
-            controller.createBanner("YOUR SUB-INGREDIENTS WILL NOT BE SAVED UNTIL YOU SUBMIT CHANGES", "alert");
+            // controller.closeModal();
+            // controller.createBanner("YOUR SUB-INGREDIENTS WILL NOT BE SAVED UNTIL YOU SUBMIT CHANGES", "alert");
         }
     },