singleRecipe.js 9.2 KB

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