addIngredient.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. window.addIngredientObj = {
  2. isPopulated: false,
  3. rows: [],
  4. display: function(){
  5. clearScreen();
  6. document.querySelector("#addIngredientAction").style.display = "flex";
  7. if(!this.isPopulated){
  8. this.populateIngredients();
  9. this.isPopulated = true;
  10. }
  11. },
  12. populateIngredients: function(){
  13. axios.get("/ingredients")
  14. .then((response)=>{
  15. if(typeof(response.data) === "string"){
  16. banner.createError(response.data);
  17. }else{
  18. let tbody = document.querySelector("#addIngredientAction tbody");
  19. for(let ingredient of response.data){
  20. let exists = false;
  21. for(let merchIngredient of merchant.inventory){
  22. if(ingredient._id === merchIngredient.ingredient._id){
  23. exists = true;
  24. break;
  25. }
  26. }
  27. if(!exists){
  28. let row = document.createElement("tr");
  29. row._id = ingredient._id;
  30. this.rows.push(row);
  31. tbody.appendChild(row);
  32. let checkbox = document.createElement("td");
  33. row.appendChild(checkbox);
  34. let checkboxInput = document.createElement("input");
  35. checkboxInput.type = "checkbox";
  36. checkbox.appendChild(checkboxInput);
  37. let name = document.createElement("td");
  38. name.innerText = ingredient.name;
  39. row.appendChild(name);
  40. let category = document.createElement("td");
  41. category.innerText = ingredient.category;
  42. row.appendChild(category);
  43. let unit = document.createElement("td");
  44. unit.innerText = ingredient.unit;
  45. row.appendChild(unit);
  46. let quantity = document.createElement("td");
  47. row.appendChild(quantity);
  48. let quantityInput = document.createElement("input");
  49. quantityInput.type = "number";
  50. quantityInput.step = "0.01";
  51. quantityInput.min = "0";
  52. quantity.appendChild(quantityInput);
  53. }
  54. }
  55. }
  56. })
  57. .catch((err)=>{
  58. banner.createError("Error: Could not retrieve ingredients");
  59. inventoryObj.display();
  60. });
  61. },
  62. submitAdd: function(){
  63. event.preventDefault();
  64. let addList = [];
  65. for(let row of this.rows){
  66. if(row.children[0].children[0].checked){
  67. let quantity = row.children[4].children[0].value;
  68. if(validator.ingredient.quantity(quantity)){
  69. addList.append({
  70. ingredient: row._id,
  71. quantity: quantity
  72. });
  73. }else{
  74. break;
  75. }
  76. }
  77. }
  78. axios.post("/merchant/ingredients/create", addList)
  79. .then((response)=>{
  80. banner.createNotification("All ingredients successfully added");
  81. this.isPopulated = false;
  82. window.inventoryObj.display();
  83. })
  84. .catch((err)=>{
  85. banner.createError("Error: Something went wrong. Try refreshing the page");
  86. });
  87. },
  88. submitNew: function(){
  89. event.preventDefault();
  90. let ingredient = {
  91. name: document.querySelector("#newName").value,
  92. category: document.querySelector("#newCategory").value,
  93. unit: document.querySelector("#newUnit").value
  94. }
  95. let quantity = document.querySelector("#newQuantity").value;
  96. if(validator.ingredient.all(ingredient, quantity)){
  97. axios.post("/ingredients/createone", {ingredient: ingredient, quantity: quantity})
  98. .then((response)=>{
  99. if(typeof(response.data) === "string"){
  100. banner.createError(response.data);
  101. }else{
  102. merchant.inventory.push(response.data);
  103. inventoryObj.display();
  104. inventoryObj.filter();
  105. for(let input of document.querySelectorAll("#createIngredientInput input")){
  106. input.value = "";
  107. }
  108. }
  109. })
  110. .catch((err)=>{
  111. banner.createError("Something went wrong and the ingredient could not be created");
  112. });
  113. }
  114. }
  115. }