Преглед изворни кода

Fix add ingredient for recipe not displaying immediately on main page

Lee Morgan пре 6 година
родитељ
комит
bf2c87785b
2 измењених фајлова са 30 додато и 12 уклоњено
  1. 17 2
      controllers/merchantData.js
  2. 13 10
      views/recipesPage/singleRecipe.js

+ 17 - 2
controllers/merchantData.js

@@ -4,6 +4,7 @@ const bcrypt = require("bcryptjs");
 const Error = require("../models/error");
 const Merchant = require("../models/merchant");
 const Recipe = require("../models/recipe");
+const Ingredient = require("../models/ingredient");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 
 const token = "b48068eb-411a-918e-ea64-52007147e42c";
@@ -452,7 +453,8 @@ module.exports = {
     //Inputs:
     //  req.body.recipeId: Id of recipe to change
     //  req.body.item:  Ingredient to add with a quantity
-    //Returns: Nothing
+    //Returns: 
+    //  recipe: Updated recipe with populated ingredients
     addRecipeIngredient: function(req, res){
         if(!req.session.user){
             req.session.error = "Must be logged in to do that";
@@ -468,7 +470,20 @@ module.exports = {
                 
                 recipe.save()
                     .then((recipe)=>{
-                        return res.json({});
+                        recipe.populate("ingredients.ingredient", (err)=>{
+                            if(err){
+                                let errorMessage = "Error: could not retrieve ingredients.  Please refresh page to see changes";
+                                let error = new Error({
+                                    code: 626,
+                                    displayMessage: errorMessage,
+                                    error: err
+                                });
+                                error.save();
+
+                                return res.json(errorMessage);
+                            }
+                            return res.json(recipe);
+                        })
                     })
                     .catch((err)=>{
                         let errorMessage = "There was an error and the recipe could not be updated";

+ 13 - 10
views/recipesPage/singleRecipe.js

@@ -80,21 +80,26 @@ let singleRecipeObj = {
         }
         
         axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
-            .then((newMerchant)=>{
-                if(typeof(newMerchant.data) === "string"){
-                    banner.createError(newMerchant.data);
+            .then((response)=>{
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
                 }else{
-                    let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
-                    recipe.ingredients.push({
-                        ingredient: addIngredient.ingredient,
-                        quantity: item.quantity
-                    });
+                    for(let i = 0; i < merchant.recipes.length; i++){
+                        if(merchant.recipes[i]._id === recipe._id){
+                            merchant.recipes.splice(i, 1);
+                            break;
+                        }
+                    }
+                    merchant.recipes.push(response.data);
+                    recipesObj.isPopulated = false;
 
                     //Change row from displaying options to showing default display
                     while(row.children.length > 0){
                         row.removeChild(row.firstChild);
                     }
 
+                    let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
+
                     let name = document.createElement("td");
                     name.innerText = addIngredient.ingredient.name;
                     row.appendChild(name);
@@ -115,8 +120,6 @@ let singleRecipeObj = {
                     removeButton.innerText = "Remove";
                     removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
                     actions.appendChild(removeButton);
-
-                    banner.createNotification("Ingredient successfully added to database");
                 }
             })
             .catch((err)=>{