createIngredients.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. let createIngredientsObj = {
  2. display: function(){
  3. controller.clearScreen();
  4. controller.createIngredientsStrand.style.display = "flex";
  5. },
  6. //Creates a new, empty row in table to input data
  7. newIngredientField: function(){
  8. let tbody = document.querySelector("#inputField tbody");
  9. let row = document.createElement("tr");
  10. tbody.appendChild(row);
  11. let name = document.createElement("td");
  12. row.appendChild(name);
  13. let nameInput = document.createElement("input");
  14. nameInput.type = "text";
  15. nameInput.onblur = ()=>{controller.checkValid("name", nameInput)};
  16. name.appendChild(nameInput);
  17. let category = document.createElement("td");
  18. row.appendChild(category);
  19. let categoryInput = document.createElement("input");
  20. categoryInput.type = "text"
  21. categoryInput.onblur = ()=>{controller.checkValid("category", categoryInput)};
  22. category.appendChild(categoryInput);
  23. let quantity = document.createElement("td");
  24. row.appendChild(quantity);
  25. let quantityInput = document.createElement("input");
  26. quantityInput.type = "number";
  27. quantityInput.step = "0.01";
  28. quantityInput.onblur = ()=>{controller.checkValid("quantity", quantityInput)};
  29. quantity.appendChild(quantityInput);
  30. let unit = document.createElement("td");
  31. row.appendChild(unit);
  32. let unitInput = document.createElement("input");
  33. unitInput.type = "text";
  34. unitInput.onblur = ()=>{controller.checkValid("unit", unitInput)};
  35. unit.appendChild(unitInput);
  36. let removeTd = document.createElement("td");
  37. row.appendChild(removeTd);
  38. let removeButton = document.createElement("button");
  39. removeButton.innerText = "-";
  40. removeButton.onclick = ()=>{row.parentNode.removeChild(row);};
  41. removeTd.appendChild(removeButton);
  42. },
  43. submit: function(){
  44. let tbody = document.querySelector("#inputField tbody");
  45. let isValid = true;
  46. let newIngredients = [];
  47. let axiosIngredients = [];
  48. for(let row of tbody.children){
  49. let name = row.children[0].children[0].value;
  50. let category = row.children[1].children[0].value;
  51. let quantity = row.children[2].children[0].value;
  52. let unit = row.children[3].children[0].value;
  53. let checkName = validator.ingredient.name(name);
  54. let checkCategory = validator.ingredient.category(category);
  55. let checkQuantity = validator.ingredient.quantity(quantity);
  56. let checkUnit = validator.ingredient.unit(unit);
  57. if(checkName && checkCategory && checkQuantity && checkUnit){
  58. let newIngredient = {
  59. name: name,
  60. category: category,
  61. unit: unit
  62. }
  63. axiosIngredients.push(newIngredient);
  64. newIngredients.push({
  65. ingredient: newIngredient,
  66. quantity: quantity
  67. });
  68. }else{
  69. isValid = false;
  70. break;
  71. }
  72. }
  73. if(isValid){
  74. axios.post("/ingredients/create", axiosIngredients)
  75. .then((ingredients)=>{
  76. for(let ingredient of newIngredients){
  77. for(let createdIngredient of ingredients.data){
  78. if(createdIngredient.name === ingredient.ingredient.name){
  79. ingredient.ingredient.id = createdIngredient._id;
  80. break;
  81. }
  82. }
  83. controller.data.inventory.push(ingredient);
  84. }
  85. banner.createNotification("All ingredients have been created and added to your inventory");
  86. if(recipes){
  87. createRecipesObj.display();
  88. }else{
  89. nameRecipesObj.display();
  90. }
  91. })
  92. .catch((err)=>{
  93. banner.createError("There has been an error and your ingredients have not been saved");
  94. console.log(err);
  95. });
  96. }
  97. }
  98. }