recipes.js 8.1 KB

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