editRecipe.js 5.3 KB

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