recipes.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. let recipesPage = {
  2. currentRecipe: {},
  3. //Display all recipes on a card
  4. displayRecipes: function(){
  5. document.querySelector("#recipes").style.display = "flex";
  6. document.querySelector("#ingredient").style.display = "none";
  7. document.querySelector("#title").innerText = "Recipes";
  8. let body = document.querySelector("#recipesContainer");
  9. currentRecipe = {};
  10. while(body.children.length > 0){
  11. body.removeChild(body.firstChild);
  12. }
  13. for(let recipe of merchant.recipes){
  14. let recipeDiv = document.createElement("div");
  15. recipeDiv.classList = "recipe-card";
  16. recipeDiv.onclick = ()=>{this.displayOneRecipe(recipe._id)};
  17. body.appendChild(recipeDiv);
  18. let title = document.createElement("h2");
  19. title.innerText = recipe.name;
  20. recipeDiv.appendChild(title);
  21. let ingredientList = document.createElement("ul");
  22. recipeDiv.appendChild(ingredientList);
  23. for(let ingredient of recipe.ingredients){
  24. let ul = document.createElement("li");
  25. ul.innerText = ingredient.ingredient.name;
  26. ingredientList.appendChild(ul);
  27. }
  28. }
  29. },
  30. //Display a single recipe with all of its ingredients
  31. displayOneRecipe: function(recipeId){
  32. let recipesDiv = document.querySelector("#recipes");
  33. let ingredientDiv = document.querySelector("#ingredient");
  34. let tbody = document.querySelector("tbody");
  35. let title = document.querySelector("#title");
  36. while(tbody.children.length > 0){
  37. tbody.removeChild(tbody.firstChild);
  38. }
  39. this.currentRecipe = merchant.recipes.find(r => r._id === recipeId);
  40. document.querySelector("#addButton").onclick = ()=>{this.displayAdd(this.currentRecipe)};
  41. title.innerText = this.currentRecipe.name;
  42. recipesDiv.style.display = "none";
  43. ingredientDiv.style.display = "flex";
  44. for(let ingredient of this.currentRecipe.ingredients){
  45. let row = document.createElement("tr");
  46. row.recipeId = this.currentRecipe._id;
  47. tbody.appendChild(row);
  48. let name = document.createElement("td");
  49. name.innerText = ingredient.ingredient.name;
  50. row.appendChild(name);
  51. let quantity = document.createElement("td");
  52. quantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  53. row.appendChild(quantity);
  54. let actions = document.createElement("td");
  55. row.appendChild(actions);
  56. let editButton = document.createElement("button");
  57. editButton.innerText = "Edit";
  58. editButton.onclick = ()=>{this.editIngredient(row, ingredient);};
  59. actions.appendChild(editButton);
  60. let removeButton = document.createElement("button");
  61. removeButton.innerText = "Remove";
  62. removeButton.onclick = ()=>{this.deleteIngredient(recipeId, ingredient._id, row);};
  63. actions.appendChild(removeButton);
  64. }
  65. },
  66. //Display the box in the table for adding another ingredient to a recipe
  67. displayAdd: function(recipe){
  68. let tbody = document.querySelector("tbody");
  69. let row = document.createElement("tr");
  70. tbody.appendChild(row);
  71. let nameTd = document.createElement("td");
  72. row.appendChild(nameTd);
  73. let name = document.createElement("select");
  74. nameTd.appendChild(name);
  75. for(let item of merchant.inventory){
  76. let nameOption = document.createElement("option");
  77. nameOption.innerText = item.ingredient.name;
  78. nameOption.value = item.ingredient._id;
  79. name.appendChild(nameOption);
  80. }
  81. let quantityTd = document.createElement("td");
  82. row.appendChild(quantityTd);
  83. let quantity = document.createElement("input");
  84. quantity.type = "text";
  85. quantity.step = "0.01";
  86. quantityTd.appendChild(quantity);
  87. let actionTd = document.createElement("td");
  88. row.appendChild(actionTd);
  89. let saveButton = document.createElement("button");
  90. saveButton.innerText = "Save";
  91. actionTd.appendChild(saveButton);
  92. saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
  93. },
  94. addIngredient: function(recipe, ingredientId, quantity, row){
  95. let item = {
  96. ingredient: ingredientId,
  97. quantity: quantity
  98. }
  99. axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
  100. .then((newMerchant)=>{
  101. let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
  102. recipe.ingredients.push({
  103. ingredient: addIngredient.ingredient,
  104. quantity: item.quantity
  105. });
  106. //Change row from displaying options to showing default display
  107. while(row.children.length > 0){
  108. row.removeChild(row.firstChild);
  109. }
  110. let name = document.createElement("td");
  111. name.innerText = addIngredient.ingredient.name;
  112. row.appendChild(name);
  113. let quantity = document.createElement("td");
  114. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  115. row.appendChild(quantity);
  116. let actions = document.createElement("td");
  117. row.appendChild(actions);
  118. let editButton = document.createElement("button");
  119. editButton.innerText = "Edit";
  120. editButton.onclick = ()=>{this.editIngredient(row, addIngredient);};
  121. actions.appendChild(editButton);
  122. let removeButton = document.createElement("button");
  123. removeButton.innerText = "Remove";
  124. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  125. actions.appendChild(removeButton);
  126. banner.createNotification("Ingredient successfully added to database");
  127. })
  128. .catch((err)=>{
  129. row.parentNode.removeChild(row);
  130. console.log(err);
  131. banner.createError("There was an error and the recipe could not be updated");
  132. });
  133. },
  134. //Delete ingredient from table
  135. //Delete ingredient from database
  136. deleteIngredient: function(recipeId, ingredientId, row){
  137. row.parentNode.removeChild(row);
  138. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  139. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  140. if(updateRecipe.ingredients[i]._id === ingredientId){
  141. updateRecipe.ingredients.splice(i, 1);
  142. break;
  143. }
  144. }
  145. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  146. .then((result)=>{
  147. banner.createNotification("Ingredient has been removed from recipe");
  148. })
  149. .catch((err)=>{
  150. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  151. console.log(err);
  152. });
  153. },
  154. //Change quantity field to input
  155. //Change edit button
  156. editIngredient: function(row, ingredient){
  157. let td = row.children[1];
  158. td.innerText = "";
  159. let input = document.createElement("input");
  160. input.type = "number";
  161. input.step = "0.01";
  162. input.value = ingredient.quantity;
  163. td.appendChild(input);
  164. let para = document.createElement("p");
  165. para.innerText = ingredient.ingredient.unit;
  166. td.appendChild(para);
  167. let button = row.children[2].children[0];
  168. button.innerText = "Save";
  169. button.onclick = ()=>{this.updateIngredient(row, ingredient);};
  170. },
  171. updateIngredient: function(row, ingredient){
  172. let originalQuantity = ingredient.quantity;
  173. ingredient.quantity = row.children[1].children[0].value;
  174. let td = row.children[1];
  175. while(td.children.length > 0){
  176. td.removeChild(td.firstChild);
  177. }
  178. let button = row.children[2].children[0];
  179. button.innerText = "Edit";
  180. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  181. axios.post("/merchant/recipes/ingredients/update", {recipeId: this.currentRecipe._id, ingredient: ingredient})
  182. .then(()=>{
  183. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  184. banner.createNotification("Ingredient successfully updated");
  185. })
  186. .catch((err)=>{
  187. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  188. console.log(err);
  189. banner.createError("There was an error and the ingredient could not be updated");
  190. });
  191. },
  192. updateRecipes: function(){
  193. axios.get("/merchant/recipes/update")
  194. .then((result)=>{
  195. merchant = result.data.merchant;
  196. this.displayRecipes();
  197. banner.createNotification("Your recipes have been updated successfully");
  198. if(result.data.count > 0){
  199. banner.createError(`You have ${result.data.count} recipes with no ingredients. Please update them.`);
  200. }
  201. })
  202. .catch((err)=>{
  203. console.log(err);
  204. banner.createError("There was an error and your recipes could not be updated");
  205. });
  206. }
  207. }
  208. recipesPage.displayRecipes();