singleRecipe.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. let singleRecipeObj = {
  2. display: function(recipe){
  3. controller.clearScreen();
  4. controller.singleRecipeStrand.style.display = "flex";
  5. let tbody = document.querySelector("tbody");
  6. while(tbody.children.length > 0){
  7. tbody.removeChild(tbody.firstChild);
  8. }
  9. document.querySelector("#singleRecipeStrand h1").innerText = recipe.name;
  10. document.querySelector("#addButton").onclick = ()=>{this.displayAdd(recipe)};
  11. for(let ingredient of recipe.ingredients){
  12. let row = document.createElement("tr");
  13. tbody.appendChild(row);
  14. let name = document.createElement("td");
  15. name.innerText = ingredient.ingredient.name;
  16. row.appendChild(name);
  17. let quantity = document.createElement("td");
  18. quantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  19. row.appendChild(quantity);
  20. let actions = document.createElement("td");
  21. row.appendChild(actions);
  22. let editButton = document.createElement("button");
  23. editButton.innerText = "Edit";
  24. editButton.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  25. actions.appendChild(editButton);
  26. let removeButton = document.createElement("button");
  27. removeButton.innerText = "Remove";
  28. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient._id, row);};
  29. actions.appendChild(removeButton);
  30. }
  31. },
  32. displayAdd: function(recipe){
  33. let tbody = document.querySelector("tbody");
  34. let row = document.createElement("tr");
  35. tbody.appendChild(row);
  36. let nameTd = document.createElement("td");
  37. row.appendChild(nameTd);
  38. let name = document.createElement("select");
  39. nameTd.appendChild(name);
  40. for(let item of merchant.inventory){
  41. let nameOption = document.createElement("option");
  42. nameOption.innerText = item.ingredient.name;
  43. nameOption.value = item.ingredient._id;
  44. name.appendChild(nameOption);
  45. }
  46. let quantityTd = document.createElement("td");
  47. row.appendChild(quantityTd);
  48. let quantity = document.createElement("input");
  49. quantity.type = "text";
  50. quantity.step = "0.01";
  51. quantityTd.appendChild(quantity);
  52. let actionTd = document.createElement("td");
  53. row.appendChild(actionTd);
  54. let saveButton = document.createElement("button");
  55. saveButton.innerText = "Save";
  56. actionTd.appendChild(saveButton);
  57. saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
  58. },
  59. addIngredient: function(recipe, ingredientId, quantity, row){
  60. let item = {
  61. ingredient: ingredientId,
  62. quantity: quantity
  63. }
  64. axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
  65. .then((newMerchant)=>{
  66. let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
  67. recipe.ingredients.push({
  68. ingredient: addIngredient.ingredient,
  69. quantity: item.quantity
  70. });
  71. //Change row from displaying options to showing default display
  72. while(row.children.length > 0){
  73. row.removeChild(row.firstChild);
  74. }
  75. let name = document.createElement("td");
  76. name.innerText = addIngredient.ingredient.name;
  77. row.appendChild(name);
  78. let quantity = document.createElement("td");
  79. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  80. row.appendChild(quantity);
  81. let actions = document.createElement("td");
  82. row.appendChild(actions);
  83. let editButton = document.createElement("button");
  84. editButton.innerText = "Edit";
  85. editButton.onclick = ()=>{this.editIngredient(row, addIngredient, recipe);};
  86. actions.appendChild(editButton);
  87. let removeButton = document.createElement("button");
  88. removeButton.innerText = "Remove";
  89. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  90. actions.appendChild(removeButton);
  91. banner.createNotification("Ingredient successfully added to database");
  92. })
  93. .catch((err)=>{
  94. row.parentNode.removeChild(row);
  95. banner.createError("There was an error and the recipe could not be updated");
  96. });
  97. },
  98. //Delete ingredient from table
  99. //Delete ingredient from database
  100. deleteIngredient: function(recipeId, ingredientId, row){
  101. row.parentNode.removeChild(row);
  102. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  103. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  104. if(updateRecipe.ingredients[i]._id === ingredientId){
  105. updateRecipe.ingredients.splice(i, 1);
  106. break;
  107. }
  108. }
  109. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  110. .then((result)=>{
  111. banner.createNotification("Ingredient has been removed from recipe");
  112. })
  113. .catch((err)=>{
  114. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  115. });
  116. },
  117. //Change quantity field to input
  118. //Change edit button
  119. editIngredient: function(row, ingredient, recipe){
  120. let td = row.children[1];
  121. td.innerText = "";
  122. let input = document.createElement("input");
  123. input.type = "number";
  124. input.step = "0.01";
  125. input.value = ingredient.quantity;
  126. td.appendChild(input);
  127. let para = document.createElement("p");
  128. para.innerText = ingredient.ingredient.unit;
  129. td.appendChild(para);
  130. let button = row.children[2].children[0];
  131. button.innerText = "Save";
  132. button.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  133. },
  134. updateIngredient: function(row, ingredient, recipe){
  135. let originalQuantity = ingredient.quantity;
  136. ingredient.quantity = row.children[1].children[0].value;
  137. let td = row.children[1];
  138. while(td.children.length > 0){
  139. td.removeChild(td.firstChild);
  140. }
  141. let button = row.children[2].children[0];
  142. button.innerText = "Edit";
  143. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  144. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  145. .then(()=>{
  146. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  147. banner.createNotification("Ingredient successfully updated");
  148. })
  149. .catch((err)=>{
  150. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  151. banner.createError("There was an error and the ingredient could not be updated");
  152. });
  153. },
  154. }