recipeDetails.js 6.8 KB

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