Quellcode durchsuchen

Creating new recipes now works

Lee Morgan vor 5 Jahren
Ursprung
Commit
9030406a16
3 geänderte Dateien mit 64 neuen und 16 gelöschten Zeilen
  1. 32 8
      views/dashboardPage/bundle.js
  2. 11 0
      views/dashboardPage/js/Recipe.js
  3. 21 8
      views/dashboardPage/js/newRecipe.js

+ 32 - 8
views/dashboardPage/bundle.js

@@ -1018,6 +1018,17 @@ class RecipeIngredient{
     }
 }
 
+/*
+Recipe Object
+id = database id of recipe
+name = name of recipe
+price = price of recipe in cents
+ingredients = [{
+    ingredient: Ingredient Object,
+    quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
+}]
+parent = merchant that it belongs to
+*/
 class Recipe{
     constructor(id, name, price, ingredients, parent){
         if(price < 0){
@@ -2858,7 +2869,7 @@ let newRecipe = {
                 if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
                     newRecipe.ingredients.push({
                         ingredient: inputs[i].children[1].children[0].value,
-                        quantity: controller.convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
+                        quantity: merchant.ingredients[j].convertToBase(inputs[i].children[2].children[0].value)
                     });
 
                     break;
@@ -2881,16 +2892,29 @@ let newRecipe = {
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-                    const recipe = new Recipe(
+                    let ingredients = [];
+                    for(let i = 0; i < response.ingredients.length; i++){
+                        for(let j = 0; j < merchant.ingredients.length; j++){
+                            if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
+                                ingredients.push({
+                                    ingredient: merchant.ingredients[j].ingredient,
+                                    quantity: response.ingredients[i].quantity
+                                });
+
+                                break;
+                            }
+                        }
+                    }
+
+                    merchant.addRecipe(new Recipe(
                         response._id,
                         response.name,
                         response.price,
-                        response.ingredients,
-                        merchant,
-                    );
-                    
-                    merchant.addRecipe(recipe);
-                    banner.createNotification("RECIPE CREATED");
+                        ingredients,
+                        merchant
+                    ));
+
+                    controller.openStrand("recipeBook");
                 }
             })
             .catch((err)=>{

+ 11 - 0
views/dashboardPage/js/Recipe.js

@@ -83,6 +83,17 @@ class RecipeIngredient{
     }
 }
 
+/*
+Recipe Object
+id = database id of recipe
+name = name of recipe
+price = price of recipe in cents
+ingredients = [{
+    ingredient: Ingredient Object,
+    quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
+}]
+parent = merchant that it belongs to
+*/
 class Recipe{
     constructor(id, name, price, ingredients, parent){
         if(price < 0){

+ 21 - 8
views/dashboardPage/js/newRecipe.js

@@ -65,7 +65,7 @@ let newRecipe = {
                 if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
                     newRecipe.ingredients.push({
                         ingredient: inputs[i].children[1].children[0].value,
-                        quantity: controller.convertToMain(merchant.ingredients[j].ingredient.unit, inputs[i].children[2].children[0].value)
+                        quantity: merchant.ingredients[j].convertToBase(inputs[i].children[2].children[0].value)
                     });
 
                     break;
@@ -88,16 +88,29 @@ let newRecipe = {
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-                    const recipe = new Recipe(
+                    let ingredients = [];
+                    for(let i = 0; i < response.ingredients.length; i++){
+                        for(let j = 0; j < merchant.ingredients.length; j++){
+                            if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
+                                ingredients.push({
+                                    ingredient: merchant.ingredients[j].ingredient,
+                                    quantity: response.ingredients[i].quantity
+                                });
+
+                                break;
+                            }
+                        }
+                    }
+
+                    merchant.addRecipe(new Recipe(
                         response._id,
                         response.name,
                         response.price,
-                        response.ingredients,
-                        merchant,
-                    );
-                    
-                    merchant.addRecipe(recipe);
-                    banner.createNotification("RECIPE CREATED");
+                        ingredients,
+                        merchant
+                    ));
+
+                    controller.openStrand("recipeBook");
                 }
             })
             .catch((err)=>{