singleRecipe.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. window.singleRecipeObj = {
  2. display: function(recipe){
  3. clearScreen();
  4. document.querySelector("#singleRecipeAction").style.display = "flex";
  5. let tbody = document.querySelector("#singleRecipeAction 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, ingredient.quantity, row);};
  32. actions.appendChild(removeButton);
  33. }
  34. },
  35. displayAdd: function(recipe){
  36. let tbody = document.querySelector("#singleRecipeAction 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 exists = false;
  46. for(let recipeIngredient of recipe.ingredients){
  47. if(item.ingredient._id === recipeIngredient.ingredient._id){
  48. exists = true;
  49. break;
  50. }
  51. }
  52. if(!exists){
  53. let nameOption = document.createElement("option");
  54. nameOption.innerText = `${item.ingredient.name} (${item.ingredient.unit})`;
  55. nameOption.value = item.ingredient._id;
  56. name.appendChild(nameOption);
  57. }
  58. }
  59. let quantityTd = document.createElement("td");
  60. row.appendChild(quantityTd);
  61. let quantity = document.createElement("input");
  62. quantity.type = "text";
  63. quantity.step = "0.01";
  64. quantity.onkeypress = (event)=>{if(event.keyCode===13) this.addIngredient(recipe, name.value, quantity.value, row)};
  65. quantityTd.appendChild(quantity);
  66. let actionTd = document.createElement("td");
  67. row.appendChild(actionTd);
  68. let saveButton = document.createElement("button");
  69. saveButton.innerText = "Save";
  70. saveButton.classList = "button-small";
  71. saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
  72. actionTd.appendChild(saveButton);
  73. let cancelButton = document.createElement("button");
  74. cancelButton.innerText = "Cancel";
  75. cancelButton.classList = "button-small";
  76. cancelButton.onclick = ()=>{tbody.removeChild(row)};
  77. actionTd.appendChild(cancelButton);
  78. },
  79. addIngredient: function(recipe, ingredientId, quantity, row){
  80. let item = {
  81. ingredient: ingredientId,
  82. quantity: quantity
  83. }
  84. if(validator.ingredient.quantity(item.quantity)){
  85. axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
  86. .then((response)=>{
  87. if(typeof(response.data) === "string"){
  88. banner.createError(response.data);
  89. }else{
  90. for(let i = 0; i < merchant.recipes.length; i++){
  91. if(merchant.recipes[i]._id === recipe._id){
  92. merchant.recipes.splice(i, 1);
  93. break;
  94. }
  95. }
  96. merchant.recipes.push(response.data);
  97. recipesObj.isPopulated = false;
  98. while(row.children.length > 0){
  99. row.removeChild(row.firstChild);
  100. }
  101. let addIngredient = response.data.ingredients.find(i => i.ingredient._id === ingredientId);
  102. let name = document.createElement("td");
  103. name.innerText = addIngredient.ingredient.name;
  104. row.appendChild(name);
  105. let quantity = document.createElement("td");
  106. quantity.innerText = `${item.quantity} ${addIngredient.ingredient.unit}`;
  107. row.appendChild(quantity);
  108. let actions = document.createElement("td");
  109. row.appendChild(actions);
  110. let editButton = document.createElement("button");
  111. editButton.innerText = "Edit";
  112. editButton.classList = "button-small";
  113. editButton.onclick = ()=>{this.editIngredient(row, addIngredient, recipe);};
  114. actions.appendChild(editButton);
  115. let removeButton = document.createElement("button");
  116. removeButton.innerText = "Remove";
  117. removeButton.classList = "button-small";
  118. removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, quantity, row);};
  119. actions.appendChild(removeButton);
  120. }
  121. })
  122. .catch((err)=>{
  123. row.parentNode.removeChild(row);
  124. banner.createError("There was an error and the recipe could not be updated");
  125. });
  126. }
  127. },
  128. //Delete ingredient from table
  129. //Delete ingredient from database
  130. deleteIngredient: function(recipeId, ingredientId, quantity, row){
  131. axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId, quantity: quantity})
  132. .then((result)=>{
  133. if(typeof(result.data) === "string"){
  134. banner.createError(result.data);
  135. }else{
  136. row.parentNode.removeChild(row);
  137. let updateRecipe = merchant.recipes.find(r => r._id === recipeId);
  138. for(let i = 0; i < updateRecipe.ingredients.length; i++){
  139. if(updateRecipe.ingredients[i].ingredient._id === ingredientId){
  140. updateRecipe.ingredients.splice(i, 1);
  141. break;
  142. }
  143. }
  144. recipesObj.isPopulated = false;
  145. }
  146. })
  147. .catch((err)=>{
  148. banner.createError("There was an error and the ingredient could not be removed from the recipe");
  149. });
  150. },
  151. //Change quantity field to input
  152. //Change edit button
  153. editIngredient: function(row, ingredient, recipe){
  154. let td = row.children[1];
  155. td.innerText = "";
  156. let input = document.createElement("input");
  157. input.type = "number";
  158. input.step = "0.01";
  159. input.value = ingredient.quantity;
  160. input.onkeypress = (event)=>{if(event.keyCode===13) this.updateIngredient(row, ingredient, recipe)};
  161. td.appendChild(input);
  162. let para = document.createElement("p");
  163. para.innerText = ingredient.ingredient.unit;
  164. td.appendChild(para);
  165. let editButton = row.children[2].children[0];
  166. editButton.innerText = "Save";
  167. editButton.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  168. let removeButton = row.children[2].children[1];
  169. removeButton.innerText = "Cancel";
  170. removeButton.onclick = ()=>{this.cancelEdit(row, ingredient, recipe)};
  171. input.focus();
  172. },
  173. cancelEdit: function(row, ingredient, recipe){
  174. let quantity = row.children[1];
  175. quantity.removeChild(quantity.firstChild);
  176. quantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  177. let saveButton = row.children[2].children[0];
  178. saveButton.innerText = "Edit";
  179. saveButton.onclick = ()=>{this.editIngredient(row, ingredient, recipe)};
  180. let cancelButton = row.children[2].children[1];
  181. cancelButton.innerText = "Remove";
  182. cancelButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient.ingredient._id, row)};
  183. },
  184. updateIngredient: function(row, ingredient, recipe){
  185. let originalQuantity = ingredient.quantity;
  186. ingredient.quantity = row.children[1].children[0].value;
  187. let td = row.children[1];
  188. while(td.children.length > 0){
  189. td.removeChild(td.firstChild);
  190. }
  191. let saveButton = row.children[2].children[0];
  192. saveButton.innerText = "Edit";
  193. saveButton.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  194. let cancelButton = row.children[2].children[1];
  195. cancelButton.innerText = "Remove";
  196. cancelButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient.ingredient._id, row)};
  197. if(validator.ingredient.quantity(ingredient.quantity)){
  198. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  199. .then((result)=>{
  200. if(typeof(result.data) === "string"){
  201. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  202. banner.createError(result.data);
  203. }else{
  204. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  205. banner.createNotification("Ingredient successfully updated");
  206. }
  207. })
  208. .catch((err)=>{
  209. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  210. banner.createError("There was an error and the ingredient could not be updated");
  211. });
  212. }else{
  213. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  214. }
  215. },
  216. }