singleRecipe.js 9.3 KB

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