singleRecipe.js 12 KB

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