recipeDetails.js 7.2 KB

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