singleRecipe.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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;
  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. quantityTd.appendChild(quantity);
  54. let actionTd = document.createElement("td");
  55. row.appendChild(actionTd);
  56. let saveButton = document.createElement("button");
  57. saveButton.innerText = "Save";
  58. saveButton.classList = "button-small";
  59. saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value, row);};
  60. actionTd.appendChild(saveButton);
  61. },
  62. addIngredient: function(recipe, ingredientId, quantity, row){
  63. let item = {
  64. ingredient: ingredientId,
  65. quantity: quantity
  66. }
  67. // Lets just not show the ingredient... duh
  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. td.appendChild(input);
  152. let para = document.createElement("p");
  153. para.innerText = ingredient.ingredient.unit;
  154. td.appendChild(para);
  155. let button = row.children[2].children[0];
  156. button.innerText = "Save";
  157. button.onclick = ()=>{this.updateIngredient(row, ingredient, recipe);};
  158. },
  159. updateIngredient: function(row, ingredient, recipe){
  160. let originalQuantity = ingredient.quantity;
  161. ingredient.quantity = row.children[1].children[0].value;
  162. let td = row.children[1];
  163. while(td.children.length > 0){
  164. td.removeChild(td.firstChild);
  165. }
  166. let button = row.children[2].children[0];
  167. button.innerText = "Edit";
  168. button.onclick = ()=>{this.editIngredient(row, ingredient, recipe);};
  169. if(validator.ingredient.quantity(ingredient.quantity)){
  170. axios.post("/merchant/recipes/ingredients/update", {recipeId: recipe._id, ingredient: ingredient})
  171. .then((result)=>{
  172. if(typeof(result.data) === "string"){
  173. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  174. banner.createError(result.data);
  175. }else{
  176. td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  177. banner.createNotification("Ingredient successfully updated");
  178. }
  179. })
  180. .catch((err)=>{
  181. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  182. banner.createError("There was an error and the ingredient could not be updated");
  183. });
  184. }else{
  185. td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
  186. }
  187. },
  188. }