recipes.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. let recipesPage = {
  2. //Display all recipes on a card
  3. displayRecipes: function(){
  4. let body = document.querySelector("#recipesContainer");
  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. 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);};
  89. },
  90. addIngredient: function(recipe, ingredientId, quantity){
  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: quantity
  101. });
  102. banner.createNotification("Ingredient successfully added to database");
  103. })
  104. .catch((err)=>{
  105. console.log(err);
  106. banner.createError("There was an error and the recipe could not be updated");
  107. })
  108. .finally(()=>{
  109. this.displayOneRecipe(recipe._id);
  110. });
  111. },
  112. //Delete ingredient from table
  113. //Delete ingredient from database
  114. deleteIngredient: function(recipeId, ingredientId, row){
  115. row.parentNode.removeChild(row);
  116. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  117. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  118. if(updateRecipe.ingredients[i]._id === ingredientId){
  119. updateRecipe.ingredients.splice(i, 1);
  120. break;
  121. }
  122. }
  123. axios.post("/merchant/update", merchant)
  124. .then((result)=>{
  125. merchant = result;
  126. banner.createNotification("Ingredient has been removed from recipe");
  127. })
  128. .catch((err)=>{
  129. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  130. console.log(err);
  131. });
  132. },
  133. //Change quantity field to input
  134. //Change edit button
  135. editIngredient: function(row, ingredient){
  136. let td = row.children[1];
  137. td.innerText = "";
  138. let input = document.createElement("input");
  139. input.type = "number";
  140. input.step = "0.01";
  141. input.value = ingredient.quantity;
  142. td.appendChild(input);
  143. let para = document.createElement("p");
  144. para.innerText = ingredient.ingredient.unit;
  145. td.appendChild(para);
  146. let button = row.children[2].children[0];
  147. button.innerText = "Save";
  148. button.onclick = ()=>{this.updateIngredient(row, ingredient);};
  149. },
  150. updateIngredient: function(row, ingredient){
  151. ingredient.quantity = row.children[1].children[0].value;
  152. let td = row.children[1];
  153. while(td.children.length > 0){
  154. td.removeChild(td.firstChild);
  155. }
  156. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  157. let button = row.children[2].children[0];
  158. button.innerText = "Edit";
  159. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  160. axios.post("/merchant/update", merchant)
  161. .then((recipe)=>{
  162. banner.createNotification("Ingredient successfully updated");
  163. })
  164. .catch((err)=>{
  165. console.log(err);
  166. banner.createError("There was an error and the ingredient could not be updated");
  167. });
  168. },
  169. updateRecipes: function(){
  170. axios.get("/recipes/update")
  171. .then((result)=>{
  172. merchant = result.data.merchant;
  173. this.displayRecipes();
  174. banner.createNotification("Your recipes have been updated successfully");
  175. if(result.data.count > 0){
  176. banner.createError(`You have ${result.data.count} recipes with no ingredients. Please update them.`);
  177. }
  178. })
  179. .catch((err)=>{
  180. console.log(err);
  181. banner.createError("There was an error and your recipes could not be updated");
  182. });
  183. }
  184. }
  185. recipesPage.displayRecipes();