recipes.js 9.8 KB

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