singleRecipe.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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("#recipeName").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.classList = "button-small";
  25. editButton.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  26. actions.appendChild(editButton);
  27. let removeButton = document.createElement("button");
  28. removeButton.innerText = "Remove";
  29. removeButton.classList = "button-small";
  30. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient._id, row);};
  31. actions.appendChild(removeButton);
  32. }
  33. },
  34. displayAdd: function(recipe){
  35. let tbody = document.querySelector("tbody");
  36. let row = document.createElement("tr");
  37. tbody.appendChild(row);
  38. let nameTd = document.createElement("td");
  39. row.appendChild(nameTd);
  40. let name = document.createElement("select");
  41. nameTd.appendChild(name);
  42. for(let item of merchant.inventory){
  43. let nameOption = document.createElement("option");
  44. nameOption.innerText = item.ingredient.name;
  45. nameOption.value = item.ingredient._id;
  46. name.appendChild(nameOption);
  47. }
  48. let quantityTd = document.createElement("td");
  49. row.appendChild(quantityTd);
  50. let quantity = document.createElement("input");
  51. quantity.type = "text";
  52. quantity.step = "0.01";
  53. quantityTd.appendChild(quantity);
  54. let actionTd = document.createElement("td");
  55. row.appendChild(actionTd);
  56. let saveButton = document.createElement("button");
  57. saveButton.innerText = "Save";
  58. actionTd.appendChild(saveButton);
  59. saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
  60. },
  61. addIngredient: function(recipe, ingredientId, quantity, row){
  62. let item = {
  63. ingredient: ingredientId,
  64. quantity: quantity
  65. }
  66. for(let ingredient of recipe.ingredients){
  67. if(ingredient.ingredient._id === ingredientId){
  68. banner.createError("That ingredient is already in this recipe");
  69. return;
  70. }
  71. }
  72. axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
  73. .then((response)=>{
  74. if(typeof(response.data) === "string"){
  75. banner.createError(response.data);
  76. }else{
  77. for(let i = 0; i < merchant.recipes.length; i++){
  78. if(merchant.recipes[i]._id === recipe._id){
  79. merchant.recipes.splice(i, 1);
  80. break;
  81. }
  82. }
  83. merchant.recipes.push(response.data);
  84. recipesObj.isPopulated = false;
  85. //Change row from displaying options to showing default display
  86. while(row.children.length > 0){
  87. row.removeChild(row.firstChild);
  88. }
  89. let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
  90. let name = document.createElement("td");
  91. name.innerText = addIngredient.ingredient.name;
  92. row.appendChild(name);
  93. let quantity = document.createElement("td");
  94. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  95. row.appendChild(quantity);
  96. let actions = document.createElement("td");
  97. row.appendChild(actions);
  98. let editButton = document.createElement("button");
  99. editButton.innerText = "Edit";
  100. editButton.onclick = ()=>{this.editIngredient(row, addIngredient, recipe);};
  101. actions.appendChild(editButton);
  102. let removeButton = document.createElement("button");
  103. removeButton.innerText = "Remove";
  104. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  105. actions.appendChild(removeButton);
  106. }
  107. })
  108. .catch((err)=>{
  109. row.parentNode.removeChild(row);
  110. banner.createError("There was an error and the recipe could not be updated");
  111. });
  112. },
  113. //Delete ingredient from table
  114. //Delete ingredient from database
  115. deleteIngredient: function(recipeId, ingredientId, row){
  116. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  117. .then((result)=>{
  118. if(typeof(result.data) === "string"){
  119. banner.createError(result.data);
  120. }else{
  121. row.parentNode.removeChild(row);
  122. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  123. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  124. if(updateRecipe.ingredients[i]._id === ingredientId){
  125. updateRecipe.ingredients.splice(i, 1);
  126. break;
  127. }
  128. }
  129. recipesObj.isPopulated = false;
  130. }
  131. })
  132. .catch((err)=>{
  133. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  134. });
  135. },
  136. //Change quantity field to input
  137. //Change edit button
  138. editIngredient: function(row, ingredient, recipe){
  139. let td = row.children[1];
  140. td.innerText = "";
  141. let input = document.createElement("input");
  142. input.type = "number";
  143. input.step = "0.01";
  144. input.value = ingredient.quantity;
  145. td.appendChild(input);
  146. let para = document.createElement("p");
  147. para.innerText = ingredient.ingredient.unit;
  148. td.appendChild(para);
  149. let button = row.children[2].children[0];
  150. button.innerText = "Save";
  151. button.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  152. },
  153. updateIngredient: function(row, ingredient, recipe){
  154. let originalQuantity = ingredient.quantity;
  155. ingredient.quantity = row.children[1].children[0].value;
  156. let td = row.children[1];
  157. while(td.children.length > 0){
  158. td.removeChild(td.firstChild);
  159. }
  160. let button = row.children[2].children[0];
  161. button.innerText = "Edit";
  162. button.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  163. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  164. .then((result)=>{
  165. if(typeof(result.data) === "string"){
  166. banner.createError(result.data);
  167. }else{
  168. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  169. banner.createNotification("Ingredient successfully updated");
  170. }
  171. })
  172. .catch((err)=>{
  173. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  174. banner.createError("There was an error and the ingredient could not be updated");
  175. });
  176. },
  177. }