recipeDetails.js 7.1 KB

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