addIngredient.js 5.3 KB

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