recipes.js 7.8 KB

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