editRecipe.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. ingredients: []
  57. }
  58. if(merchant.pos === "none"){
  59. data.name = document.getElementById("editRecipeName").value;
  60. }
  61. let ingredients = document.getElementById("editRecipeIngList").children;
  62. for(let i = 0; i < ingredients.length; i++){
  63. const quantity = parseFloat(ingredients[i].children[3].value);
  64. let newIngredient = {};
  65. let ingredient = {};
  66. if(ingredients[i].children[1].style.display === "none"){
  67. let selector = ingredients[i].children[2];
  68. ingredient = selector.options[selector.selectedIndex].ingredient;
  69. newIngredient = {
  70. ingredient: ingredient.ingredient.id,
  71. quantity: controller.baseUnit(quantity, ingredient.ingredient.unit)
  72. };
  73. }else{
  74. ingredient = ingredients[i].ingredient;
  75. newIngredient = {
  76. ingredient: ingredient.ingredient.id,
  77. quantity: controller.baseUnit(quantity, ingredient.ingredient.unit)
  78. };
  79. }
  80. data.ingredients.push(newIngredient);
  81. }
  82. let loader = document.getElementById("loaderContainer");
  83. loader.style.display = "flex";
  84. fetch("/recipe/update", {
  85. method: "put",
  86. headers: {
  87. "Content-Type": "application/json;charset=utf-8"
  88. },
  89. body: JSON.stringify(data)
  90. })
  91. .then(response => response.json())
  92. .then((response)=>{
  93. if(typeof(response) === "string"){
  94. controller.createBanner(response, "error");
  95. }else{
  96. merchant.updateRecipe(recipe, response);
  97. state.updateRecipes();
  98. controller.openStrand("recipeBook");
  99. controller.createBanner("RECIPE UPDATED", "success");
  100. }
  101. })
  102. .catch((err)=>{
  103. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  104. })
  105. .finally(()=>{
  106. loader.style.display = "none";
  107. });
  108. }
  109. }
  110. module.exports = editRecipe;