Przeglądaj źródła

Fix updating of recipes

Lee Morgan 6 lat temu
rodzic
commit
b3fc0ae22f

+ 5 - 31
controllers/recipeData.js

@@ -52,7 +52,7 @@ module.exports = {
     /*
     /*
     PUT - Update a single recipe
     PUT - Update a single recipe
     req.body = {
     req.body = {
-        _id: id of recipe,
+        id: id of recipe,
         name: name of recipe,
         name: name of recipe,
         price: price of recipe,
         price: price of recipe,
         ingredients: [{
         ingredients: [{
@@ -67,39 +67,13 @@ module.exports = {
             return res.redirect("/");
             return res.redirect("/");
         }
         }
 
 
-        Recipe.findOne({_id: req.body._id})
+        console.log(req.body);
+
+        Recipe.findOne({_id: req.body.id})
             .then((recipe)=>{
             .then((recipe)=>{
                 recipe.name = req.body.name;
                 recipe.name = req.body.name;
                 recipe.price = req.body.price;
                 recipe.price = req.body.price;
-                
-                for(let i = 0; i < req.body.ingredients.length; i++){
-                    let isNew = true;
-                    for(let j = 0; j < recipe.ingredients.length; j++){
-                        if(req.body.ingredients[i].ingredient === recipe.ingredients[j].ingredient._id.toString()){
-                            isNew = false;
-                            recipe.ingredients[j].quantity = req.body.ingredients[i].quantity;
-                            break;
-                        }
-                    }
-
-                    if(isNew){
-                        recipe.ingredients.push(req.body.ingredients[i]);
-                    }
-                }
-
-                for(let i = 0; i < recipe.ingredients.length; i++){
-                    let doesntExist = true;
-                    for(let j = 0; j < req.body.ingredients.length; j++){
-                        if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredients[j].ingredient){
-                            doesntExist = false;
-                            break;
-                        }
-                    }
-
-                    if(doesntExist){
-                        recipe.ingredients.splice(i, 1);
-                    }
-                }
+                recipe.ingredients = req.body.ingredients;
 
 
                 return recipe.save()
                 return recipe.save()
             })
             })

+ 1 - 0
views/dashboardPage/Merchant.js

@@ -60,6 +60,7 @@ class Transaction{
 class Merchant{
 class Merchant{
     constructor(oldMerchant, transactions){
     constructor(oldMerchant, transactions){
         this.name = oldMerchant.name;
         this.name = oldMerchant.name;
+        this.pos = oldMerchant.pos;
         this.ingredients = [];
         this.ingredients = [];
         this.recipes = [];
         this.recipes = [];
         this.transactions = [];
         this.transactions = [];

+ 42 - 32
views/dashboardPage/components/components.js

@@ -20,7 +20,7 @@ let recipeDetailsComp = {
 
 
             ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
             ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
             ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
             ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
-            ingredientDiv._id = recipe.ingredients[i].ingredient._id;
+            ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
             ingredientDiv.name = recipe.ingredients[i].ingredient.name;
             ingredientDiv.name = recipe.ingredients[i].ingredient.name;
 
 
             ingredientList.appendChild(ingredientDiv);
             ingredientList.appendChild(ingredientDiv);
@@ -70,49 +70,59 @@ let recipeDetailsComp = {
     },
     },
 
 
     update: function(){
     update: function(){
-        let updatedRecipe = {
-            _id: this.recipe._id,
-            name: document.querySelector("#recipeNameIn").value || this.recipe.name,
-            price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
-            ingredients: []
-        }
+        this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
+        this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
+        this.recipe.ingredients = [];
 
 
         let divs = document.querySelector("#recipeIngredientList").children;
         let divs = document.querySelector("#recipeIngredientList").children;
         for(let i = 0; i < divs.length; i++){
         for(let i = 0; i < divs.length; i++){
             if(divs[i].name === "new"){
             if(divs[i].name === "new"){
-                updatedRecipe.ingredients.push({
-                    ingredient: divs[i].children[0].value,
+                let select = divs[i].children[0];
+                this.recipe.ingredients.push({
+                    ingredient: select.options[select.selectedIndex].ingredient,
                     quantity: divs[i].children[1].value
                     quantity: divs[i].children[1].value
                 })
                 })
             }else{
             }else{
-                updatedRecipe.ingredients.push({
-                    ingredient: divs[i]._id,
+                this.recipe.ingredients.push({
+                    ingredient: divs[i].ingredient,
                     quantity: divs[i].children[1].value || divs[i].children[1].placeholder
                     quantity: divs[i].children[1].value || divs[i].children[1].placeholder
                 });
                 });
             }
             }
         }
         }
 
 
-        if(validator.recipe(updatedRecipe)){
-            fetch("/recipe/update", {
-                method: "PUT",
-                headers: {
-                    "Content-Type": "application/json;charset=utf-8"
-                },
-                body: JSON.stringify(updatedRecipe)
-            })
-                .then((response) => response.json())
-                .then((response)=>{
-                    if(typeof(response) === "string"){
-                        banner.createError(response);
-                    }else{
-                        updateRecipes(updatedRecipe);
-                        banner.createNotification("Recipe successfully updated");
-                    }
-                })
-                .catch((err)=>{
-                    banner.createError("Something went wrong.  Please refresh the page");
-                })
+        let data = {
+            id: this.recipe.id,
+            name: this.recipe.name,
+            price: this.recipe.price,
+            ingredients: []
         }
         }
+
+        for(let i = 0; i < this.recipe.ingredients.length; i++){
+            data.ingredients.push({
+                ingredient: this.recipe.ingredients[i].ingredient.id,
+                quantity: this.recipe.ingredients[i].quantity
+            });
+        }
+
+        fetch("/recipe/update", {
+            method: "PUT",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+            body: JSON.stringify(data)
+        })
+            .then((response) => response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    merchant.editRecipe(this.recipe);
+                    banner.createNotification("Recipe successfully updated");
+                }
+            })
+            .catch((err)=>{
+                banner.createError("Something went wrong.  Please refresh the page");
+            })
     },
     },
 
 
     remove: function(){
     remove: function(){
@@ -148,7 +158,7 @@ let recipeDetailsComp = {
             for(let j = 0; j < categories[i].ingredients.length; j++){
             for(let j = 0; j < categories[i].ingredients.length; j++){
                 let option = document.createElement("option");
                 let option = document.createElement("option");
                 option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
                 option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
-                option.value = categories[i].ingredients[j].id;
+                option.ingredient = categories[i].ingredients[j].ingredient;
                 optGroup.appendChild(option);
                 optGroup.appendChild(option);
             }
             }
         }
         }