recipeDetails.js 6.2 KB

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