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