ingredientSetup.js 7.1 KB

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