recipeDetails.js 6.8 KB

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