recipes.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. let recipesPage = {
  2. //Display all recipes on a card
  3. displayRecipes: function(){
  4. let body = document.querySelector(".container");
  5. while(body.children.length > 0){
  6. body.removeChild(body.firstChild);
  7. }
  8. for(let recipe of recipes){
  9. let recipeDiv = document.createElement("div");
  10. recipeDiv.classList = "recipe-card";
  11. recipeDiv.onclick = ()=>{this.displayOneRecipe(recipe)};
  12. body.appendChild(recipeDiv);
  13. let title = document.createElement("h2");
  14. title.innerText = recipe.name;
  15. recipeDiv.appendChild(title);
  16. let ingredientList = document.createElement("ul");
  17. recipeDiv.appendChild(ingredientList);
  18. for(let ingredient of recipe.ingredients){
  19. let ul = document.createElement("li");
  20. ul.innerText = ingredient.id.name;
  21. ingredientList.appendChild(ul);
  22. }
  23. }
  24. },
  25. //Display a single recipe with all of its ingredients
  26. displayOneRecipe: function(recipe){
  27. let recipesDiv = document.querySelector("#recipes");
  28. let ingredientDiv = document.querySelector("#ingredient");
  29. let tbody = document.querySelector("tbody");
  30. let title = document.querySelector("#title");
  31. title.innerText = recipe.name;
  32. recipesDiv.style.display = "none";
  33. ingredientDiv.style.display = "flex";
  34. let delRecipe = document.querySelector("#delRecipe");
  35. delRecipe.style.display = "inline-block";
  36. delRecipe.onclick = ()=>{this.deleteRecipe(recipe);};
  37. for(let ingredient of recipe.ingredients){
  38. let row = document.createElement("tr");
  39. row.recipeId = recipe._id;
  40. tbody.appendChild(row);
  41. let name = document.createElement("td");
  42. name.innerText = ingredient.id.name;
  43. row.appendChild(name);
  44. let quantity = document.createElement("td");
  45. quantity.innerText = `${ingredient.quantity} ${ingredient.id.unitType}`;
  46. row.appendChild(quantity);
  47. let actions = document.createElement("td");
  48. row.appendChild(actions);
  49. let editButton = document.createElement("button");
  50. editButton.innerText = "Edit";
  51. editButton.onclick = ()=>{this.editIngredient(row, ingredient);};
  52. actions.appendChild(editButton);
  53. let removeButton = document.createElement("button");
  54. removeButton.innerText = "Remove";
  55. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient._id, row);};
  56. actions.appendChild(removeButton);
  57. }
  58. },
  59. //Delete ingredient from table
  60. //Delete ingredient from database
  61. deleteIngredient: function(recipeId, ingredientId, row){
  62. row.parentNode.removeChild(row);
  63. axios.post("/recipes/ingredients/remove", {recipeId: recipeId, ingredientId:ingredientId})
  64. .then((result)=>{
  65. banner.createNotification("Ingredient has been removed from recipe");
  66. })
  67. .catch((err)=>{
  68. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  69. console.log(err);
  70. });
  71. },
  72. //Change quantity field to input
  73. //Change edit button
  74. editIngredient: function(row, ingredient){
  75. let td = row.children[1];
  76. td.innerText = "";
  77. let input = document.createElement("input");
  78. input.type = "number";
  79. input.step = "0.01";
  80. input.value = ingredient.quantity;
  81. td.appendChild(input);
  82. let para = document.createElement("p");
  83. para.innerText = ingredient.id.unitType;
  84. td.appendChild(para);
  85. let button = row.children[2].children[0];
  86. button.innerText = "Save";
  87. button.onclick = ()=>{this.updateIngredient(row, ingredient);};
  88. },
  89. updateIngredient: function(row, ingredient){
  90. ingredient.quantity = row.children[1].children[0].value;
  91. let td = row.children[1];
  92. while(td.children.length > 0){
  93. td.removeChild(td.firstChild);
  94. }
  95. td.innerText = `${ingredient.quantity} ${ingredient.id.unitType}`;
  96. let button = row.children[2].children[0];
  97. button.innerText = "Edit";
  98. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  99. axios.post("/recipes/ingredients/update", {recipeId: row.recipeId, ingredient: ingredient})
  100. .then((recipe)=>{
  101. banner.createNotification("Ingredient successfully updated");
  102. })
  103. .catch((err)=>{
  104. console.log(err);
  105. banner.createError("There was an error and the ingredient could not be updated");
  106. });
  107. },
  108. deleteRecipe: function(recipe){
  109. for(let i = 0; i < recipes.length; i++){
  110. if(recipes[i]._id === recipe._id){
  111. recipes.splice(i, 1);
  112. break;
  113. }
  114. }
  115. let ingredientDiv = document.querySelector("#ingredient");
  116. let recipesDiv = document.querySelector("#recipes");
  117. ingredientDiv.style.display = "none";
  118. recipesDiv.style.display = "flex";
  119. this.displayRecipes();
  120. axios.post("recipes/remove", {id: recipe._id})
  121. .then((recipe)=>{
  122. banner.createNotification("Recipe removed");
  123. })
  124. .catch((err)=>{
  125. console.log(err);
  126. banner.createError("There was an error and the recipe could not be removed");
  127. });
  128. }
  129. }
  130. recipesPage.displayRecipes();