recipes.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. title.innerText = recipe.name;
  35. recipesDiv.style.display = "none";
  36. ingredientDiv.style.display = "flex";
  37. recipeUpdate.style.display = "none";
  38. for(let ingredient of recipe.ingredients){
  39. let row = document.createElement("tr");
  40. row.recipeId = recipe._id;
  41. tbody.appendChild(row);
  42. let name = document.createElement("td");
  43. name.innerText = ingredient.ingredient.name;
  44. row.appendChild(name);
  45. let quantity = document.createElement("td");
  46. quantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unitType}`;
  47. row.appendChild(quantity);
  48. let actions = document.createElement("td");
  49. row.appendChild(actions);
  50. let editButton = document.createElement("button");
  51. editButton.innerText = "Edit";
  52. editButton.onclick = ()=>{this.editIngredient(row, ingredient);};
  53. actions.appendChild(editButton);
  54. let removeButton = document.createElement("button");
  55. removeButton.innerText = "Remove";
  56. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient._id, row);};
  57. actions.appendChild(removeButton);
  58. }
  59. },
  60. //Delete ingredient from table
  61. //Delete ingredient from database
  62. deleteIngredient: function(recipeId, ingredientId, row){
  63. row.parentNode.removeChild(row);
  64. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  65. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  66. if(updateRecipe.ingredients[i]._id === ingredientId){
  67. updateRecipe.ingredients.splice(i, 1);
  68. break;
  69. }
  70. }
  71. axios.post("/merchant/update", merchant)
  72. .then((result)=>{
  73. merchant = result;
  74. banner.createNotification("Ingredient has been removed from recipe");
  75. })
  76. .catch((err)=>{
  77. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  78. console.log(err);
  79. });
  80. },
  81. //Change quantity field to input
  82. //Change edit button
  83. editIngredient: function(row, ingredient){
  84. let td = row.children[1];
  85. td.innerText = "";
  86. let input = document.createElement("input");
  87. input.type = "number";
  88. input.step = "0.01";
  89. input.value = ingredient.quantity;
  90. td.appendChild(input);
  91. let para = document.createElement("p");
  92. para.innerText = ingredient.ingredient.unitType;
  93. td.appendChild(para);
  94. let button = row.children[2].children[0];
  95. button.innerText = "Save";
  96. button.onclick = ()=>{this.updateIngredient(row, ingredient);};
  97. },
  98. updateIngredient: function(row, ingredient){
  99. ingredient.quantity = row.children[1].children[0].value;
  100. let td = row.children[1];
  101. while(td.children.length > 0){
  102. td.removeChild(td.firstChild);
  103. }
  104. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unitType}`;
  105. let button = row.children[2].children[0];
  106. button.innerText = "Edit";
  107. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  108. axios.post("/merchant/update", merchant)
  109. .then((recipe)=>{
  110. banner.createNotification("Ingredient successfully updated");
  111. })
  112. .catch((err)=>{
  113. console.log(err);
  114. banner.createError("There was an error and the ingredient could not be updated");
  115. });
  116. },
  117. updateRecipes: function(){
  118. axios.get("/recipes/update")
  119. .then((result)=>{
  120. merchant = result.data.merchant;
  121. this.displayRecipes();
  122. banner.createNotification("Your recipes have been updated successfully");
  123. if(result.data.count > 0){
  124. banner.createError(`You have ${result.data.count} recipes with no ingredients. Please update them.`);
  125. }
  126. })
  127. .catch((err)=>{
  128. console.log(err);
  129. banner.createError("There was an error and your recipes could not be updated");
  130. });
  131. }
  132. }
  133. recipesPage.displayRecipes();