ingredientSetup.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. let ingredientSetup = {
  2. existingIngredientElements: [],
  3. newIngredientElements: [],
  4. populateIngredients: function(){
  5. let tBody = document.createElement("tbody");
  6. for(let ingredient of ingredients){
  7. let row = document.createElement("tr");
  8. row.id = ingredient._id;
  9. let add = document.createElement("td");
  10. let checkbox = document.createElement("input");
  11. checkbox.type = "checkbox";
  12. add.appendChild(checkbox);
  13. row.appendChild(add);
  14. let name = document.createElement("td");
  15. name.innerText = ingredient.name;
  16. row.appendChild(name);
  17. let category = document.createElement("td");
  18. category.innerText = ingredient.category;
  19. row.appendChild(category);
  20. let quantity = document.createElement("td");
  21. let quantityInput = document.createElement("input");
  22. quantityInput.type = "number";
  23. quantityInput.step = "0.01";
  24. quantityInput.min = "0";
  25. quantity.appendChild(quantityInput);
  26. row.appendChild(quantity);
  27. let unit = document.createElement("td");
  28. unit.innerText = ingredient.unitType;
  29. row.appendChild(unit);
  30. let idField = document.createElement("input");
  31. idField.type = "hidden";
  32. idField.value = ingredient._id;
  33. tBody.appendChild(row);
  34. let oldTBody = document.querySelector("#ingredient-display tbody");
  35. oldTBody.parentNode.replaceChild(tBody, oldTBody);
  36. this.existingIngredientElements.push(row);
  37. this.displayExistingIngredients();
  38. }
  39. },
  40. displayExistingIngredients: function(){
  41. addIngredients.style.display = "flex";
  42. newIngredients.style.display = "none";
  43. createRecipes.style.display = "none";
  44. },
  45. displayNewIngredients: function(){
  46. addIngredients.style.display = "none";
  47. newIngredients.style.display = "flex";
  48. createRecipes.style.display = "none";
  49. },
  50. newIngredientField: function(){
  51. let body = document.querySelector("#inputField tbody");
  52. let row = document.createElement("tr");
  53. let name = document.createElement("td");
  54. let nameInput = document.createElement("input");
  55. nameInput.type = "text";
  56. nameInput.onblur = ()=>{checkValid("name", nameInput)};
  57. name.appendChild(nameInput);
  58. row.appendChild(name);
  59. let category = document.createElement("td");
  60. let categoryInput = document.createElement("input");
  61. categoryInput.type = "text"
  62. categoryInput.onblur = ()=>{checkValid("category", categoryInput)};
  63. category.appendChild(categoryInput);
  64. row.appendChild(category);
  65. let quantity = document.createElement("td");
  66. let quantityInput = document.createElement("input");
  67. quantityInput.type = "number";
  68. quantityInput.step = "0.01";
  69. quantityInput.onblur = ()=>{checkValid("quantity", quantityInput)};
  70. quantity.appendChild(quantityInput);
  71. row.appendChild(quantity);
  72. let unit = document.createElement("td");
  73. let unitInput = document.createElement("input");
  74. unitInput.type = "text";
  75. unitInput.onblur = ()=>{checkValid("unit", unitInput)};
  76. unit.appendChild(unitInput);
  77. row.appendChild(unit);
  78. let removeTd = document.createElement("td");
  79. let removeButton = document.createElement("button");
  80. removeButton.innerText = "-";
  81. removeButton.onclick = ()=>{this.removeRow(row)};
  82. removeTd.appendChild(removeButton);
  83. row.appendChild(removeTd);
  84. body.appendChild(row);
  85. this.newIngredientElements.push(row);
  86. },
  87. removeRow: function(row){
  88. for(let i = 0; i < this.newIngredientElements.length; i++){
  89. if(this.newIngredientElements[i] === row){
  90. this.newIngredientElements.splice(i, 1);
  91. }
  92. }
  93. row.parentNode.removeChild(row);
  94. },
  95. //refactor
  96. //nothin should run unless everything is valid
  97. createIngredientsList: function(){
  98. data.ingredients = [];
  99. for(let ingredient of this.existingIngredientElements){
  100. if(ingredient.children[0].children[0].checked){
  101. data.ingredients.push({
  102. id: ingredient.id,
  103. name: ingredient.children[1].textContent,
  104. quantity: ingredient.children[3].children[0].value,
  105. unitType: ingredient.children[4].textContent
  106. });
  107. }
  108. }
  109. let newIngredient = [];
  110. let newIngredientQuantity = [];
  111. for(let ingredient of this.newIngredientElements){
  112. newIngredient.push({
  113. name: ingredient.children[0].children[0].value,
  114. category: ingredient.children[1].children[0].value,
  115. unitType: ingredient.children[3].children[0].value
  116. });
  117. newIngredientQuantity.push({
  118. name: ingredient.children[0].children[0].value,
  119. quantity: ingredient.children[2].children[0].value
  120. });
  121. }
  122. let isValid = true;
  123. for(let i = 0; i < newIngredient.length; i++){
  124. if(!validator.ingredient.all(newIngredient[i], newIngredientQuantity[i].quantity)){
  125. isValid = false;
  126. data.ingredients = [];
  127. break;
  128. }
  129. }
  130. if(isValid){
  131. axios.post("/ingredients/create", newIngredient)
  132. .then((result)=>{
  133. for(let ingredient of result.data){
  134. let newIngredient = {
  135. id: ingredient._id,
  136. name: ingredient.name,
  137. unitType: ingredient.unitType
  138. }
  139. for(let item of newIngredientQuantity){
  140. if(ingredient.name === item.name){
  141. newIngredient.quantity = item.quantity;
  142. }
  143. }
  144. data.ingredients.push(newIngredient);
  145. }
  146. banner.createNotification("All ingredients have been created and added to your inventory");
  147. recipeSetup.createRecipePage();
  148. })
  149. .catch((err)=>{
  150. banner.createError("There has been an error and your ingredients have not been saved");
  151. console.log(err);
  152. });
  153. }
  154. },
  155. addRecipeIngredientField: function(){
  156. let body = document.querySelector("#recipes tbody");
  157. let row = document.createElement("tr");
  158. body.appendChild(row);
  159. let ingTd = document.createElement("td");
  160. row.appendChild(ingTd);
  161. let ingName = document.createElement("select");
  162. for(let ingredient of data.ingredients){
  163. let newOption = document.createElement("option");
  164. newOption.innerText = ingredient.name;
  165. newOption.value = ingredient.id;
  166. ingName.appendChild(newOption);
  167. }
  168. ingTd.appendChild(ingName);
  169. let quantTd = document.createElement("td");
  170. row.appendChild(quantTd);
  171. let ingQuant = document.createElement("input");
  172. ingQuant.type = "number";
  173. ingQuant.step = "0.01";
  174. ingQuant.min = "0";
  175. quantTd.appendChild(ingQuant);
  176. let removeTd = document.createElement("td");
  177. row.appendChild(removeTd);
  178. let removeButton = document.createElement("button");
  179. removeButton.innerText = "-";
  180. removeButton.onclick = ()=>{row.parentNode.removeChild(row)};
  181. removeTd.appendChild(removeButton);
  182. }
  183. };