recipeDetails.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. let recipeDetails = {
  2. recipe: {},
  3. display: function(recipe){
  4. this.recipe = recipe;
  5. document.getElementById("recipeName").style.display = "block";
  6. document.getElementById("recipeNameIn").style.display = "none";
  7. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  8. let ingredientList = document.getElementById("recipeIngredientList");
  9. while(ingredientList.children.length > 0){
  10. ingredientList.removeChild(ingredientList.firstChild);
  11. }
  12. let template = document.getElementById("recipeIngredient").content.children[0];
  13. for(let i = 0; i < recipe.ingredients.length; i++){
  14. ingredientDiv = template.cloneNode(true);
  15. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  16. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].ingredient.convert(recipe.ingredients[i].quantity).toFixed(2)} ${recipe.ingredients[i].ingredient.unit}`;
  17. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  18. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  19. ingredientList.appendChild(ingredientDiv);
  20. }
  21. document.getElementById("addRecIng").style.display = "none";
  22. let price = document.getElementById("recipePrice");
  23. price.children[1].style.display = "block";
  24. price.children[2].style.display = "none";
  25. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  26. document.getElementById("recipeUpdate").style.display = "none";
  27. document.getElementById("editRecipeBtn").onclick = ()=>{this.edit()};
  28. if(merchant.pos === "none"){
  29. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove()};
  30. }
  31. document.getElementById("addRecIng").onclick = ()=>{this.displayAddIngredient()};
  32. document.getElementById("recipeUpdate").onclick = ()=>{this.update()};
  33. },
  34. edit: function(){
  35. let ingredientDivs = document.getElementById("recipeIngredientList");
  36. if(merchant.pos === "none"){
  37. let name = document.getElementById("recipeName");
  38. let nameIn = document.getElementById("recipeNameIn");
  39. name.style.display = "none";
  40. nameIn.style.display = "block";
  41. nameIn.value = this.recipe.name;
  42. let price = document.getElementById("recipePrice");
  43. price.children[1].style.display = "none";
  44. price.children[2].style.display = "block";
  45. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  46. }
  47. for(let i = 0; i < ingredientDivs.children.length; i++){
  48. let div = ingredientDivs.children[i];
  49. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  50. div.children[1].style.display = "block";
  51. div.children[1].value = this.recipe.ingredients[i].ingredient.convert(this.recipe.ingredients[i].quantity).toFixed(2);
  52. div.children[3].style.display = "block";
  53. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  54. }
  55. document.getElementById("addRecIng").style.display = "flex";
  56. document.getElementById("recipeUpdate").style.display = "flex";
  57. },
  58. update: function(){
  59. this.recipe.name = document.getElementById("recipeNameIn").value || this.recipe.name;
  60. this.recipe.price = Math.round((document.getElementById("recipePrice").children[2].value * 100)) || this.recipe.price;
  61. this.recipe.ingredients = [];
  62. let divs = document.getElementById("recipeIngredientList").children;
  63. for(let i = 0; i < divs.length; i++){
  64. if(divs[i].name === "new"){
  65. let select = divs[i].children[0];
  66. this.recipe.ingredients.push({
  67. ingredient: select.options[select.selectedIndex].ingredient,
  68. quantity: controller.convertToMain(select.options[select.selectedIndex].ingredient.unit, divs[i].children[1].value)
  69. });
  70. }else{
  71. this.recipe.ingredients.push({
  72. ingredient: divs[i].ingredient,
  73. quantity: controller.convertToMain(divs[i].ingredient.unit, divs[i].children[1].value)
  74. });
  75. }
  76. }
  77. let data = {
  78. id: this.recipe.id,
  79. name: this.recipe.name,
  80. price: this.recipe.price,
  81. ingredients: []
  82. }
  83. for(let i = 0; i < this.recipe.ingredients.length; i++){
  84. data.ingredients.push({
  85. ingredient: this.recipe.ingredients[i].ingredient.id,
  86. quantity: this.recipe.ingredients[i].quantity
  87. });
  88. }
  89. let loader = document.getElementById("loaderContainer");
  90. loader.style.display = "flex";
  91. fetch("/recipe/update", {
  92. method: "PUT",
  93. headers: {
  94. "Content-Type": "application/json;charset=utf-8"
  95. },
  96. body: JSON.stringify(data)
  97. })
  98. .then((response) => response.json())
  99. .then((response)=>{
  100. if(typeof(response) === "string"){
  101. banner.createError(response);
  102. }else{
  103. window.merchant.editRecipes([this.recipe]);
  104. banner.createNotification("RECIPE UPDATE");
  105. }
  106. })
  107. .catch((err)=>{
  108. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  109. })
  110. .finally(()=>{
  111. loader.style.display = "none";
  112. });
  113. },
  114. remove: function(){
  115. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  116. method: "DELETE"
  117. })
  118. .then((response) => response.json())
  119. .then((response)=>{
  120. if(typeof(response) === "string"){
  121. banner.createError(response);
  122. }else{
  123. merchant.editRecipes([this.recipe], true);
  124. banner.createNotification("RECIPE REMOVED");
  125. }
  126. })
  127. .catch((err)=>{
  128. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  129. });
  130. },
  131. displayAddIngredient: function(){
  132. let template = document.getElementById("addRecIngredient").content.children[0].cloneNode(true);
  133. template.name = "new";
  134. document.getElementById("recipeIngredientList").appendChild(template);
  135. let categories = window.merchant.categorizeIngredients();
  136. for(let i = 0; i < categories.length; i++){
  137. let optGroup = document.createElement("optgroup");
  138. optGroup.label = categories[i].name;
  139. template.children[0].appendChild(optGroup);
  140. for(let j = 0; j < categories[i].ingredients.length; j++){
  141. let option = document.createElement("option");
  142. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  143. option.ingredient = categories[i].ingredients[j].ingredient;
  144. optGroup.appendChild(option);
  145. }
  146. }
  147. }
  148. }
  149. module.exports = recipeDetails;