editRecipe.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if(ingredients[i].children[1].style.display === "none"){
  65. let selector = ingredients[i].children[2];
  66. let ingredient = selector.options[selector.selectedIndex].ingredient;
  67. data.ingredients.push({
  68. ingredient: ingredient.ingredient.id,
  69. quantity: ingredient.convertToBase(quantity)
  70. });
  71. }else{
  72. data.ingredients.push({
  73. ingredient: ingredients[i].ingredient.ingredient.id,
  74. quantity: ingredients[i].ingredient.convertToBase(quantity)
  75. });
  76. }
  77. }
  78. let loader = document.getElementById("loaderContainer");
  79. loader.style.display = "flex";
  80. fetch("/recipe/update", {
  81. method: "put",
  82. headers: {
  83. "Content-Type": "application/json;charset=utf-8"
  84. },
  85. body: JSON.stringify(data)
  86. })
  87. .then(response => response.json())
  88. .then((response)=>{
  89. if(typeof(response) === "string"){
  90. banner.createError(response);
  91. }else{
  92. merchant.updateRecipe(response);
  93. controller.openStrand("recipeBook");
  94. }
  95. })
  96. .catch((err)=>{
  97. console.log(err);
  98. banner.createError("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE");
  99. })
  100. .finally(()=>{
  101. loader.style.display = "none";
  102. });
  103. }
  104. }
  105. module.exports = editRecipe;