singleRecipe.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. for(let ingredient of recipe.ingredients){
  65. if(ingredient.ingredient._id === ingredientId){
  66. banner.createError("That ingredient is already in this recipe");
  67. return;
  68. }
  69. }
  70. axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
  71. .then((response)=>{
  72. if(typeof(response.data) === "string"){
  73. banner.createError(response.data);
  74. }else{
  75. for(let i = 0; i < merchant.recipes.length; i++){
  76. if(merchant.recipes[i]._id === recipe._id){
  77. merchant.recipes.splice(i, 1);
  78. break;
  79. }
  80. }
  81. merchant.recipes.push(response.data);
  82. recipesObj.isPopulated = false;
  83. //Change row from displaying options to showing default display
  84. while(row.children.length > 0){
  85. row.removeChild(row.firstChild);
  86. }
  87. let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
  88. let name = document.createElement("td");
  89. name.innerText = addIngredient.ingredient.name;
  90. row.appendChild(name);
  91. let quantity = document.createElement("td");
  92. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  93. row.appendChild(quantity);
  94. let actions = document.createElement("td");
  95. row.appendChild(actions);
  96. let editButton = document.createElement("button");
  97. editButton.innerText = "Edit";
  98. editButton.onclick = ()=>{this.editIngredient(row, addIngredient, recipe);};
  99. actions.appendChild(editButton);
  100. let removeButton = document.createElement("button");
  101. removeButton.innerText = "Remove";
  102. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
  103. actions.appendChild(removeButton);
  104. }
  105. })
  106. .catch((err)=>{
  107. row.parentNode.removeChild(row);
  108. banner.createError("There was an error and the recipe could not be updated");
  109. });
  110. },
  111. //Delete ingredient from table
  112. //Delete ingredient from database
  113. deleteIngredient: function(recipeId, ingredientId, row){
  114. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
  115. .then((result)=>{
  116. if(typeof(result.data) === "string"){
  117. banner.createError(result.data);
  118. }else{
  119. row.parentNode.removeChild(row);
  120. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  121. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  122. if(updateRecipe.ingredients[i]._id === ingredientId){
  123. updateRecipe.ingredients.splice(i, 1);
  124. break;
  125. }
  126. }
  127. recipesObj.isPopulated = false;
  128. }
  129. })
  130. .catch((err)=>{
  131. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  132. });
  133. },
  134. //Change quantity field to input
  135. //Change edit button
  136. editIngredient: function(row, ingredient, recipe){
  137. let td = row.children[1];
  138. td.innerText = "";
  139. let input = document.createElement("input");
  140. input.type = "number";
  141. input.step = "0.01";
  142. input.value = ingredient.quantity;
  143. td.appendChild(input);
  144. let para = document.createElement("p");
  145. para.innerText = ingredient.ingredient.unit;
  146. td.appendChild(para);
  147. let button = row.children[2].children[0];
  148. button.innerText = "Save";
  149. button.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  150. },
  151. updateIngredient: function(row, ingredient, recipe){
  152. let originalQuantity = ingredient.quantity;
  153. ingredient.quantity = row.children[1].children[0].value;
  154. let td = row.children[1];
  155. while(td.children.length > 0){
  156. td.removeChild(td.firstChild);
  157. }
  158. let button = row.children[2].children[0];
  159. button.innerText = "Edit";
  160. button.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  161. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  162. .then((result)=>{
  163. if(typeof(result.data) === "string"){
  164. banner.createError(result.data);
  165. }else{
  166. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  167. banner.createNotification("Ingredient successfully updated");
  168. }
  169. })
  170. .catch((err)=>{
  171. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  172. banner.createError("There was an error and the ingredient could not be updated");
  173. });
  174. },
  175. }