singleRecipe.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. let singleRecipeObj = {
  2. display: function(recipe){
  3. controller.clearScreen();
  4. controller.singleRecipeStrand.style.display = "flex";
  5. let tbody = document.querySelector("tbody");
  6. while(tbody.children.length > 0){
  7. tbody.removeChild(tbody.firstChild);
  8. }
  9. document.querySelector("#singleRecipeStrand h1").innerText = recipe.name;
  10. document.querySelector("#addButton").onclick = ()=>{this.displayAdd(recipe)};
  11. for(let ingredient of recipe.ingredients){
  12. let row = document.createElement("tr");
  13. tbody.appendChild(row);
  14. let name = document.createElement("td");
  15. name.innerText = ingredient.ingredient.name;
  16. row.appendChild(name);
  17. let quantity = document.createElement("td");
  18. quantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  19. row.appendChild(quantity);
  20. let actions = document.createElement("td");
  21. row.appendChild(actions);
  22. let editButton = document.createElement("button");
  23. editButton.innerText = "Edit";
  24. editButton.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  25. actions.appendChild(editButton);
  26. let removeButton = document.createElement("button");
  27. removeButton.innerText = "Remove";
  28. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient._id, row);};
  29. actions.appendChild(removeButton);
  30. }
  31. },
  32. displayAdd: function(recipe){
  33. let tbody = document.querySelector("tbody");
  34. let row = document.createElement("tr");
  35. tbody.appendChild(row);
  36. let nameTd = document.createElement("td");
  37. row.appendChild(nameTd);
  38. let name = document.createElement("select");
  39. nameTd.appendChild(name);
  40. for(let item of merchant.inventory){
  41. let nameOption = document.createElement("option");
  42. nameOption.innerText = item.ingredient.name;
  43. nameOption.value = item.ingredient._id;
  44. name.appendChild(nameOption);
  45. }
  46. let quantityTd = document.createElement("td");
  47. row.appendChild(quantityTd);
  48. let quantity = document.createElement("input");
  49. quantity.type = "text";
  50. quantity.step = "0.01";
  51. quantityTd.appendChild(quantity);
  52. let actionTd = document.createElement("td");
  53. row.appendChild(actionTd);
  54. let saveButton = document.createElement("button");
  55. saveButton.innerText = "Save";
  56. actionTd.appendChild(saveButton);
  57. saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
  58. },
  59. addIngredient: function(recipe, ingredientId, quantity, row){
  60. let item = {
  61. ingredient: ingredientId,
  62. quantity: quantity
  63. }
  64. axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
  65. .then((response)=>{
  66. if(typeof(response.data) === "string"){
  67. banner.createError(response.data);
  68. }else{
  69. for(let i = 0; i < merchant.recipes.length; i++){
  70. if(merchant.recipes[i]._id === recipe._id){
  71. merchant.recipes.splice(i, 1);
  72. break;
  73. }
  74. }
  75. merchant.recipes.push(response.data);
  76. recipesObj.isPopulated = false;
  77. //Change row from displaying options to showing default display
  78. while(row.children.length > 0){
  79. row.removeChild(row.firstChild);
  80. }
  81. let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
  82. let name = document.createElement("td");
  83. name.innerText = addIngredient.ingredient.name;
  84. row.appendChild(name);
  85. let quantity = document.createElement("td");
  86. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  87. row.appendChild(quantity);
  88. let actions = document.createElement("td");
  89. row.appendChild(actions);
  90. let editButton = document.createElement("button");
  91. editButton.innerText = "Edit";
  92. editButton.onclick = ()=>{this.editIngredient(row, addIngredient, recipe);};
  93. actions.appendChild(editButton);
  94. let removeButton = document.createElement("button");
  95. removeButton.innerText = "Remove";
  96. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  97. actions.appendChild(removeButton);
  98. }
  99. })
  100. .catch((err)=>{
  101. row.parentNode.removeChild(row);
  102. banner.createError("There was an error and the recipe could not be updated");
  103. });
  104. },
  105. //Delete ingredient from table
  106. //Delete ingredient from database
  107. deleteIngredient: function(recipeId, ingredientId, row){
  108. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  109. .then((result)=>{
  110. if(typeof(result.data) === "string"){
  111. banner.createError(result.data);
  112. }else{
  113. row.parentNode.removeChild(row);
  114. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  115. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  116. if(updateRecipe.ingredients[i]._id === ingredientId){
  117. updateRecipe.ingredients.splice(i, 1);
  118. break;
  119. }
  120. }
  121. recipesObj.isPopulated = false;
  122. }
  123. })
  124. .catch((err)=>{
  125. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  126. });
  127. },
  128. //Change quantity field to input
  129. //Change edit button
  130. editIngredient: function(row, ingredient, recipe){
  131. let td = row.children[1];
  132. td.innerText = "";
  133. let input = document.createElement("input");
  134. input.type = "number";
  135. input.step = "0.01";
  136. input.value = ingredient.quantity;
  137. td.appendChild(input);
  138. let para = document.createElement("p");
  139. para.innerText = ingredient.ingredient.unit;
  140. td.appendChild(para);
  141. let button = row.children[2].children[0];
  142. button.innerText = "Save";
  143. button.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  144. },
  145. updateIngredient: function(row, ingredient, recipe){
  146. let originalQuantity = ingredient.quantity;
  147. ingredient.quantity = row.children[1].children[0].value;
  148. let td = row.children[1];
  149. while(td.children.length > 0){
  150. td.removeChild(td.firstChild);
  151. }
  152. let button = row.children[2].children[0];
  153. button.innerText = "Edit";
  154. button.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  155. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  156. .then((result)=>{
  157. if(typeof(result.data) === "string"){
  158. banner.createError(result.data);
  159. }else{
  160. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  161. banner.createNotification("Ingredient successfully updated");
  162. }
  163. })
  164. .catch((err)=>{
  165. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  166. banner.createError("There was an error and the ingredient could not be updated");
  167. });
  168. },
  169. }