recipes.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. banner.createNotification("Ingredient has been removed from recipe");
  70. })
  71. .catch((err)=>{
  72. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  73. console.log(err);
  74. });
  75. },
  76. //Change quantity field to input
  77. //Change edit button
  78. editIngredient: function(row, ingredient){
  79. let td = row.children[1];
  80. td.innerText = "";
  81. let input = document.createElement("input");
  82. input.type = "number";
  83. input.step = "0.01";
  84. input.value = ingredient.quantity;
  85. td.appendChild(input);
  86. let para = document.createElement("p");
  87. para.innerText = ingredient.ingredient.unitType;
  88. td.appendChild(para);
  89. let button = row.children[2].children[0];
  90. button.innerText = "Save";
  91. button.onclick = ()=>{this.updateIngredient(row, ingredient);};
  92. },
  93. updateIngredient: function(row, ingredient){
  94. ingredient.quantity = row.children[1].children[0].value;
  95. let td = row.children[1];
  96. while(td.children.length > 0){
  97. td.removeChild(td.firstChild);
  98. }
  99. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unitType}`;
  100. let button = row.children[2].children[0];
  101. button.innerText = "Edit";
  102. button.onclick = ()=>{this.editIngredient(row, ingredient);};
  103. axios.post("/merchant/update", merchant)
  104. .then((recipe)=>{
  105. banner.createNotification("Ingredient successfully updated");
  106. })
  107. .catch((err)=>{
  108. console.log(err);
  109. banner.createError("There was an error and the ingredient could not be updated");
  110. });
  111. }
  112. }
  113. recipesPage.displayRecipes();