editRecipe.js 5.4 KB

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