recipes.js 5.6 KB

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