recipes.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. let body = document.querySelector("#recipesContainer");
  8. currentRecipe = {};
  9. while(body.children.length > 0){
  10. body.removeChild(body.firstChild);
  11. }
  12. for(let recipe of merchant.recipes){
  13. let recipeDiv = document.createElement("div");
  14. recipeDiv.classList = "recipe-card";
  15. recipeDiv.onclick = ()=>{this.displayOneRecipe(recipe._id)};
  16. body.appendChild(recipeDiv);
  17. let title = document.createElement("h2");
  18. title.innerText = recipe.name;
  19. recipeDiv.appendChild(title);
  20. let ingredientList = document.createElement("ul");
  21. recipeDiv.appendChild(ingredientList);
  22. for(let ingredient of recipe.ingredients){
  23. let ul = document.createElement("li");
  24. ul.innerText = ingredient.ingredient.name;
  25. ingredientList.appendChild(ul);
  26. }
  27. }
  28. },
  29. //Display a single recipe with all of its ingredients
  30. displayOneRecipe: function(recipeId){
  31. let recipesDiv = document.querySelector("#recipes");
  32. let ingredientDiv = document.querySelector("#ingredient");
  33. let tbody = document.querySelector("tbody");
  34. let title = document.querySelector("#title");
  35. while(tbody.children.length > 0){
  36. tbody.removeChild(tbody.firstChild);
  37. }
  38. this.currentRecipe = merchant.recipes.find(r => r._id === recipeId);
  39. document.querySelector("#addButton").onclick = ()=>{this.displayAdd(this.currentRecipe)};
  40. title.innerText = this.currentRecipe.name;
  41. recipesDiv.style.display = "none";
  42. ingredientDiv.style.display = "flex";
  43. for(let ingredient of this.currentRecipe.ingredients){
  44. let row = document.createElement("tr");
  45. row.recipeId = this.currentRecipe._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(recipeId, 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, row);};
  91. },
  92. addIngredient: function(recipe, ingredientId, quantity, row){
  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: item.quantity
  103. });
  104. //Change row from displaying options to showing default display
  105. while(row.children.length > 0){
  106. row.removeChild(row.firstChild);
  107. }
  108. let name = document.createElement("td");
  109. name.innerText = addIngredient.ingredient.name;
  110. row.appendChild(name);
  111. let quantity = document.createElement("td");
  112. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  113. row.appendChild(quantity);
  114. let actions = document.createElement("td");
  115. row.appendChild(actions);
  116. let editButton = document.createElement("button");
  117. editButton.innerText = "Edit";
  118. editButton.onclick = ()=>{this.editIngredient(row, addIngredient);};
  119. actions.appendChild(editButton);
  120. let removeButton = document.createElement("button");
  121. removeButton.innerText = "Remove";
  122. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  123. actions.appendChild(removeButton);
  124. banner.createNotification("Ingredient successfully added to database");
  125. })
  126. .catch((err)=>{
  127. row.parentNode.removeChild(row);
  128. console.log(err);
  129. banner.createError("There was an error and the recipe could not be updated");
  130. });
  131. },
  132. //Delete ingredient from table
  133. //Delete ingredient from database
  134. deleteIngredient: function(recipeId, ingredientId, row){
  135. row.parentNode.removeChild(row);
  136. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  137. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  138. if(updateRecipe.ingredients[i]._id === ingredientId){
  139. updateRecipe.ingredients.splice(i, 1);
  140. break;
  141. }
  142. }
  143. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  144. .then((result)=>{
  145. banner.createNotification("Ingredient has been removed from recipe");
  146. })
  147. .catch((err)=>{
  148. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  149. console.log(err);
  150. });
  151. },
  152. //Change quantity field to input
  153. //Change edit button
  154. editIngredient: function(row, ingredient){
  155. let td = row.children[1];
  156. td.innerText = "";
  157. let input = document.createElement("input");
  158. input.type = "number";
  159. input.step = "0.01";
  160. input.value = ingredient.quantity;
  161. td.appendChild(input);
  162. let para = document.createElement("p");
  163. para.innerText = ingredient.ingredient.unit;
  164. td.appendChild(para);
  165. let button = row.children[2].children[0];
  166. button.innerText = "Save";
  167. button.onclick = ()=>{this.updateIngredient(row, ingredient);};
  168. },
  169. updateIngredient: function(row, ingredient){
  170. let originalQuantity = ingredient.quantity;
  171. ingredient.quantity = row.children[1].children[0].value;
  172. let td = row.children[1];
  173. while(td.children.length > 0){
  174. td.removeChild(td.firstChild);
  175. }
  176. let button = row.children[2].children[0];
  177. button.innerText = "Edit";
  178. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  179. axios.post("/merchant/recipes/ingredients/update", {recipeId: this.currentRecipe._id, ingredient: ingredient})
  180. .then(()=>{
  181. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  182. banner.createNotification("Ingredient successfully updated");
  183. })
  184. .catch((err)=>{
  185. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  186. console.log(err);
  187. banner.createError("There was an error and the ingredient could not be updated");
  188. });
  189. },
  190. updateRecipes: function(){
  191. axios.get("/merchant/recipes/update")
  192. .then((result)=>{
  193. merchant = result.data.merchant;
  194. this.displayRecipes();
  195. banner.createNotification("Your recipes have been updated successfully");
  196. if(result.data.count > 0){
  197. banner.createError(`You have ${result.data.count} recipes with no ingredients. Please update them.`);
  198. }
  199. })
  200. .catch((err)=>{
  201. console.log(err);
  202. banner.createError("There was an error and your recipes could not be updated");
  203. });
  204. }
  205. }
  206. recipesPage.displayRecipes();