recipes.js 9.2 KB

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