createIngredients.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. for(let row of tbody.children){
  48. let name = row.children[0].children[0].value;
  49. let category = row.children[1].children[0].value;
  50. let quantity = row.children[2].children[0].value;
  51. let unit = row.children[3].children[0].value;
  52. let checkName = validator.ingredient.name(name);
  53. let checkCategory = validator.ingredient.category(category);
  54. let checkQuantity = validator.ingredient.quantity(quantity);
  55. let checkUnit = validator.ingredient.unit(unit);
  56. if(checkName && checkCategory && checkQuantity && checkUnit){
  57. newIngredients.push({
  58. name: name,
  59. category: category,
  60. unit: unit
  61. });
  62. }else{
  63. isValid = false;
  64. break;
  65. }
  66. }
  67. if(isValid){
  68. axios.post("/ingredients/create", newIngredients)
  69. .then((ingredients)=>{
  70. for(let ingredient of ingredients.data){
  71. controller.data.ingredients.push({
  72. ingredient: {
  73. id: ingredient._id,
  74. name: ingredient.name,
  75. category: ingredient.category,
  76. unit: ingredient.unit
  77. },
  78. quantity: quantity
  79. });
  80. }
  81. banner.createNotification("All ingredients have been created and added to your inventory");
  82. if(recipes){
  83. createRecipesObj.display();
  84. }else{
  85. nameRecipesObj.display();
  86. }
  87. })
  88. .catch((err)=>{
  89. banner.createError("There has been an error and your ingredients have not been saved");
  90. console.log(err);
  91. });
  92. }
  93. }
  94. }