ingredientSetup.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. existingIngredients: 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. }
  40. },
  41. redisplayExistingIngredients: function(){
  42. controller.clearScreen();
  43. controller.addIngredientsComp.style.display = "flex";
  44. },
  45. //Display new ingredients table
  46. //Hide other tables
  47. createIngredients: function(){
  48. controller.clearScreen();
  49. controller.newIngredientsComp.style.display = "flex";
  50. },
  51. //Creates a new, empty row in table to input data
  52. newIngredientField: function(){
  53. let body = document.querySelector("#inputField tbody");
  54. let row = document.createElement("tr");
  55. let name = document.createElement("td");
  56. let nameInput = document.createElement("input");
  57. nameInput.type = "text";
  58. nameInput.onblur = ()=>{controller.checkValid("name", nameInput)};
  59. name.appendChild(nameInput);
  60. row.appendChild(name);
  61. let category = document.createElement("td");
  62. let categoryInput = document.createElement("input");
  63. categoryInput.type = "text"
  64. categoryInput.onblur = ()=>{controller.checkValid("category", categoryInput)};
  65. category.appendChild(categoryInput);
  66. row.appendChild(category);
  67. let quantity = document.createElement("td");
  68. let quantityInput = document.createElement("input");
  69. quantityInput.type = "number";
  70. quantityInput.step = "0.01";
  71. quantityInput.onblur = ()=>{controller.checkValid("quantity", quantityInput)};
  72. quantity.appendChild(quantityInput);
  73. row.appendChild(quantity);
  74. let unit = document.createElement("td");
  75. let unitInput = document.createElement("input");
  76. unitInput.type = "text";
  77. unitInput.onblur = ()=>{controller.checkValid("unit", unitInput)};
  78. unit.appendChild(unitInput);
  79. row.appendChild(unit);
  80. let removeTd = document.createElement("td");
  81. let removeButton = document.createElement("button");
  82. removeButton.innerText = "-";
  83. removeButton.onclick = ()=>{this.removeRow(row)};
  84. removeTd.appendChild(removeButton);
  85. row.appendChild(removeTd);
  86. body.appendChild(row);
  87. this.newIngredientElements.push(row);
  88. },
  89. //Remove row from new ingredients table
  90. removeRow: function(row){
  91. for(let i = 0; i < this.newIngredientElements.length; i++){
  92. if(this.newIngredientElements[i] === row){
  93. this.newIngredientElements.splice(i, 1);
  94. }
  95. }
  96. row.parentNode.removeChild(row);
  97. },
  98. //refactor
  99. //nothin should run unless everything is valid
  100. createIngredientsList: function(){
  101. controller.data.ingredients = [];
  102. for(let ingredient of this.existingIngredientElements){
  103. if(ingredient.children[0].children[0].checked){
  104. controller.data.ingredients.push({
  105. id: ingredient.id,
  106. name: ingredient.children[1].textContent,
  107. quantity: ingredient.children[3].children[0].value,
  108. unit: ingredient.children[4].textContent
  109. });
  110. }
  111. }
  112. let newIngredient = [];
  113. let newIngredientQuantity = [];
  114. for(let ingredient of this.newIngredientElements){
  115. newIngredient.push({
  116. name: ingredient.children[0].children[0].value,
  117. category: ingredient.children[1].children[0].value,
  118. unit: ingredient.children[3].children[0].value
  119. });
  120. newIngredientQuantity.push({
  121. name: ingredient.children[0].children[0].value,
  122. quantity: ingredient.children[2].children[0].value
  123. });
  124. }
  125. let isValid = true;
  126. for(let i = 0; i < newIngredient.length; i++){
  127. if(!validator.ingredient.all(newIngredient[i], newIngredientQuantity[i].quantity)){
  128. isValid = false;
  129. controller.data.ingredients = [];
  130. break;
  131. }
  132. }
  133. if(isValid){
  134. axios.post("/ingredients/create", newIngredient)
  135. .then((result)=>{
  136. for(let ingredient of result.data){
  137. let newIngredient = {
  138. id: ingredient._id,
  139. name: ingredient.name,
  140. unit: ingredient.unit
  141. }
  142. for(let item of newIngredientQuantity){
  143. if(ingredient.name === item.name){
  144. newIngredient.quantity = item.quantity;
  145. }
  146. }
  147. controller.data.ingredients.push(newIngredient);
  148. }
  149. banner.createNotification("All ingredients have been created and added to your inventory");
  150. recipeSetup.createRecipePage();
  151. })
  152. .catch((err)=>{
  153. banner.createError("There has been an error and your ingredients have not been saved");
  154. console.log(err);
  155. });
  156. }
  157. }
  158. };