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

New recipes can now be submitted again.

Lee Morgan пре 5 година
родитељ
комит
643d60ad5f
1 измењених фајлова са 33 додато и 4 уклоњено
  1. 33 4
      views/dashboardPage/js/sidebars/newRecipe.js

+ 33 - 4
views/dashboardPage/js/sidebars/newRecipe.js

@@ -56,13 +56,42 @@ let newRecipe = {
 
         let ingredients = document.getElementById("newRecipeChosenList").children;
         for(let i = 0; i < ingredients.length; i++){
+            let ingredient = ingredients[i].ingredient;
+
             data.ingredients.push({
-                ingredient: ingredients[i].ingredient.id,
-                quantity: ingredients[i].children[1].children[0].value
-            })
+                ingredient: ingredient.id,
+                quantity: controller.baseUnit(ingredients[i].children[1].children[0].value, ingredient.unit)
+            });
         }
 
-        console.log(data);
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/recipe/create", {
+            method: "post",
+            headers: {
+                "Content-Type": "application/json"
+            },
+            body: JSON.stringify(data)
+        })
+            .then(response => response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    controller.createBanner(response, "error");
+                }else{
+                    merchant.addRecipes([response]);
+                    state.updateRecipes();
+
+                    controller.createBanner("RECIPE CREATED", "success");
+                    controller.openStrand("recipeBook");
+                }
+            })
+            .catch((err)=>{
+                controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 };