editRecipe2.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. let editRecipe = {
  2. display: function(recipe){
  3. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  4. let nameInput = document.getElementById("editRecipeName");
  5. if(merchant.pos === "none"){
  6. nameInput.value = recipe.name;
  7. let categoryInput = document.getElementById("editRecipeCategory");
  8. categoryInput.value = recipe.category;
  9. categoryInput.parentElement.style.display = "block";
  10. }else{
  11. document.getElementById("editRecipeNoName").innerText = recipe.name;
  12. nameInput.parentNode.style.display = "none";
  13. }
  14. //Populate ingredients
  15. let ingredientList = document.getElementById("editRecipeIngList");
  16. while(ingredientList.children.length > 0){
  17. ingredientList.removeChild(ingredientList.firstChild);
  18. }
  19. let template = document.getElementById("editRecipeIng").content.children[0];
  20. for(let i = 0; i < recipe.ingredients.length; i++){
  21. let ingredientDiv = template.cloneNode(true);
  22. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  23. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  24. ingredientDiv.children[2].style.display = "none";
  25. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  26. ingredientDiv.ingredient = recipe.ingredients[i];
  27. ingredientList.appendChild(ingredientDiv);
  28. }
  29. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  30. document.getElementById("editRecipePrice").value = recipe.price;
  31. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  32. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  33. },
  34. newIngredient: function(){
  35. let ingredientList = document.getElementById("editRecipeIngList");
  36. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  37. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  38. ingredientDiv.children[1].style.display = "none";
  39. ingredientDiv.children[3].value = "0.00";
  40. //Populate selector
  41. let categories = merchant.categorizeIngredients();
  42. for(let i = 0; i < categories.length; i++){
  43. let group = document.createElement("optgroup");
  44. group.label = categories[i].name;
  45. for(let j = 0; j < categories[i].ingredients.length; j++){
  46. let option = document.createElement("option");
  47. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  48. option.ingredient = categories[i].ingredients[j];
  49. group.appendChild(option);
  50. }
  51. ingredientDiv.children[2].appendChild(group);
  52. }
  53. ingredientList.appendChild(ingredientDiv);
  54. },
  55. submit: function(recipe){
  56. let data = {
  57. id: recipe.id,
  58. name: recipe.name,
  59. price: document.getElementById("editRecipePrice").value * 100,
  60. category: document.getElementById("editRecipeCategory").value,
  61. ingredients: []
  62. }
  63. if(merchant.pos === "none"){
  64. data.name = document.getElementById("editRecipeName").value;
  65. }
  66. let ingredients = document.getElementById("editRecipeIngList").children;
  67. for(let i = 0; i < ingredients.length; i++){
  68. const quantity = parseFloat(ingredients[i].children[3].value);
  69. let newIngredient = {};
  70. let ingredient = {};
  71. if(ingredients[i].children[1].style.display === "none"){
  72. let selector = ingredients[i].children[2];
  73. ingredient = selector.options[selector.selectedIndex].ingredient;
  74. newIngredient = {
  75. ingredient: ingredient.ingredient.id,
  76. quantity: controller.baseUnit(quantity, ingredient.ingredient.unit)
  77. };
  78. }else{
  79. ingredient = ingredients[i].ingredient;
  80. newIngredient = {
  81. ingredient: ingredient.ingredient.id,
  82. quantity: controller.baseUnit(quantity, ingredient.ingredient.unit)
  83. };
  84. }
  85. data.ingredients.push(newIngredient);
  86. }
  87. let loader = document.getElementById("loaderContainer");
  88. loader.style.display = "flex";
  89. fetch("/recipe/update", {
  90. method: "put",
  91. headers: {
  92. "Content-Type": "application/json;charset=utf-8"
  93. },
  94. body: JSON.stringify(data)
  95. })
  96. .then(response => response.json())
  97. .then((response)=>{
  98. if(typeof(response) === "string"){
  99. controller.createBanner(response, "error");
  100. }else{
  101. merchant.updateRecipe(recipe, response);
  102. state.updateRecipes();
  103. controller.openStrand("recipeBook");
  104. controller.createBanner("RECIPE UPDATED", "success");
  105. }
  106. })
  107. .catch((err)=>{
  108. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  109. })
  110. .finally(()=>{
  111. loader.style.display = "none";
  112. });
  113. }
  114. }
  115. module.exports = editRecipe;