singleRecipe.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if(typeof(newMerchant.data) === "string"){
  67. banner.createError(newMerchant.data);
  68. }else{
  69. let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
  70. recipe.ingredients.push({
  71. ingredient: addIngredient.ingredient,
  72. quantity: item.quantity
  73. });
  74. //Change row from displaying options to showing default display
  75. while(row.children.length > 0){
  76. row.removeChild(row.firstChild);
  77. }
  78. let name = document.createElement("td");
  79. name.innerText = addIngredient.ingredient.name;
  80. row.appendChild(name);
  81. let quantity = document.createElement("td");
  82. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  83. row.appendChild(quantity);
  84. let actions = document.createElement("td");
  85. row.appendChild(actions);
  86. let editButton = document.createElement("button");
  87. editButton.innerText = "Edit";
  88. editButton.onclick = ()=>{this.editIngredient(row, addIngredient, recipe);};
  89. actions.appendChild(editButton);
  90. let removeButton = document.createElement("button");
  91. removeButton.innerText = "Remove";
  92. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  93. actions.appendChild(removeButton);
  94. banner.createNotification("Ingredient successfully added to database");
  95. }
  96. })
  97. .catch((err)=>{
  98. row.parentNode.removeChild(row);
  99. banner.createError("There was an error and the recipe could not be updated");
  100. });
  101. },
  102. //Delete ingredient from table
  103. //Delete ingredient from database
  104. deleteIngredient: function(recipeId, ingredientId, row){
  105. row.parentNode.removeChild(row);
  106. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  107. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  108. if(updateRecipe.ingredients[i]._id === ingredientId){
  109. updateRecipe.ingredients.splice(i, 1);
  110. break;
  111. }
  112. }
  113. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  114. .then((result)=>{
  115. if(typeof(result.data) === "string"){
  116. banner.createError(result.data);
  117. }else{
  118. banner.createNotification("Ingredient has been removed from recipe");
  119. }
  120. })
  121. .catch((err)=>{
  122. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  123. });
  124. },
  125. //Change quantity field to input
  126. //Change edit button
  127. editIngredient: function(row, ingredient, recipe){
  128. let td = row.children[1];
  129. td.innerText = "";
  130. let input = document.createElement("input");
  131. input.type = "number";
  132. input.step = "0.01";
  133. input.value = ingredient.quantity;
  134. td.appendChild(input);
  135. let para = document.createElement("p");
  136. para.innerText = ingredient.ingredient.unit;
  137. td.appendChild(para);
  138. let button = row.children[2].children[0];
  139. button.innerText = "Save";
  140. button.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  141. },
  142. updateIngredient: function(row, ingredient, recipe){
  143. let originalQuantity = ingredient.quantity;
  144. ingredient.quantity = row.children[1].children[0].value;
  145. let td = row.children[1];
  146. while(td.children.length > 0){
  147. td.removeChild(td.firstChild);
  148. }
  149. let button = row.children[2].children[0];
  150. button.innerText = "Edit";
  151. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  152. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  153. .then((result)=>{
  154. if(typeof(result.data) === "string"){
  155. banner.createError(result.data);
  156. }else{
  157. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  158. banner.createNotification("Ingredient successfully updated");
  159. }
  160. })
  161. .catch((err)=>{
  162. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  163. banner.createError("There was an error and the ingredient could not be updated");
  164. });
  165. },
  166. }