Просмотр исходного кода

Recipe updates on front end automatically

Lee Morgan 6 лет назад
Родитель
Сommit
8e336b5593
3 измененных файлов с 64 добавлено и 4 удалено
  1. 2 1
      controllers/recipeData.js
  2. 48 0
      views/dashboardPage/controller.js
  3. 14 3
      views/dashboardPage/recipeBook.js

+ 2 - 1
controllers/recipeData.js

@@ -9,6 +9,7 @@ module.exports = {
     //  req.body.ingredients: array of ingredients (object) in recipe
     //      id: id of ingredient
     //      quantity: quantity of ingredient in recipe
+    //Returns newly created ingredient
     createRecipe: function(req, res){
         if(!req.session.user){
             req.session.error = "Must be logged in to do that";
@@ -37,7 +38,7 @@ module.exports = {
 
         recipe.save()
             .then((newRecipe)=>{
-                return res.json({});
+                return res.json(newRecipe);
             })
             .catch((err)=>{
                 console.log(err);

+ 48 - 0
views/dashboardPage/controller.js

@@ -62,6 +62,54 @@ let updateInventory = (ingredients, remove = false)=>{
     closeSidebar();
 }
 
+/*
+Updates a recipe in the merchants list of recipes
+Can create, edit or remove
+Inputs:
+    recipe: object
+        _id: id of recipe
+        name: name of recipe
+        price: price of recipe
+        ingredients: list of ingredients
+            ingredient: id of ingredient
+            quantity: quantity of ingredient
+    remove: if true, remove ingredient from inventory
+*/
+let updateRecipes = (recipe, remove = false)=>{
+    let isNew = true;
+    let index = 0;
+    for(let i = 0; i < merchant.recipes.length; i++){
+        if(recipe.id === merchant.recipes[i]._id){
+            if(remove){
+                merchant.recipes.splice(i, 1);
+            }else{
+                merchant.recipes[i] = recipe;
+                index = i;
+            }
+
+            isNew = false;
+            break;
+        }
+    }
+
+    if(isNew){
+        merchant.recipes.push(recipe);
+        index = merchant.recipes.length - 1;
+    }
+
+    for(let i = 0; i < recipe.ingredients.length; i++){
+        for(let j = 0; j < merchant.inventory.length; j++){
+            if(merchant.inventory[j].ingredient._id === recipe.ingredients[i].ingredient){
+                recipe.ingredients[i].ingredient = merchant.inventory[j].ingredient;
+                break;
+            }
+        }
+    }
+
+    recipeBookStrandObj.populateRecipes();
+    closeSidebar();
+}
+
 //Close any open sidebar
 let closeSidebar = ()=>{
     let sidebar = document.querySelector("#sidebarDiv");

+ 14 - 3
views/dashboardPage/recipeBook.js

@@ -12,6 +12,10 @@ window.recipeBookStrandObj = {
     populateRecipes: function(){
         let recipeList = document.querySelector("#recipeList");
 
+        while(recipeList.children.length > 0){
+            recipeList.removeChild(recipeList.firstChild);
+        }
+
         for(let recipe of merchant.recipes){
             let recipeDiv = document.createElement("div");
             recipeDiv.classList = "recipeItem";
@@ -128,11 +132,18 @@ window.recipeBookStrandObj = {
             },
             body: JSON.stringify(newRecipe)
         })
+            .then((response) => response.json())
             .then((response)=>{
-                if(typeof(response.data) === "string"){
-                    banner.createError(response.data);
+                if(typeof(response) === "string"){
+                    banner.createError(response);
                 }else{
-                    banner.createNotification("New recipe successfully created")
+                    newRecipe._id = response._id;
+                    newRecipe.price = Math.round(newRecipe.price * 100);
+                    for(let i = 0; i < newRecipe.ingredients.length; i++){
+                        newRecipe.ingredients[i].quantity = parseFloat(newRecipe.ingredients[i].quantity);
+                    }
+                    updateRecipes(newRecipe);
+                    banner.createNotification("New recipe successfully created");
                 }
             })
             .catch((err)=>{