inventory.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. let inventoryObj = {
  2. items: [],
  3. currentSort: "",
  4. isPopulated: false,
  5. display: function(){
  6. controller.clearScreen();
  7. controller.inventoryStrand.style.display = "flex";
  8. if(!this.isPopulated){
  9. this.filter();
  10. this.isPopulated = true;
  11. }
  12. },
  13. populateIngredients: function(){
  14. let tbody = document.querySelector("#inventoryStrand tbody");
  15. while(tbody.children.length > 0){
  16. tbody.removeChild(tbody.firstChild);
  17. }
  18. for(let item of this.items){
  19. let row = document.createElement("tr");
  20. tbody.appendChild(row);
  21. let name = document.createElement("td");
  22. name.innerText = item.name;
  23. row.appendChild(name);
  24. let category = document.createElement("td");
  25. category.innerText = item.category;
  26. row.appendChild(category);
  27. let quantity = document.createElement("td");
  28. quantity.innerText = item.quantity;
  29. row.appendChild(quantity);
  30. let unit = document.createElement("td");
  31. unit.innerText = item.unit;
  32. row.appendChild(unit);
  33. let action = document.createElement("td");
  34. row.appendChild(action);
  35. let editBtn = document.createElement("button");
  36. editBtn.onclick = ()=>{this.editIngredient(item.id, row)};
  37. editBtn.innerText = "Edit";
  38. editBtn.className = "edit-button"
  39. action.appendChild(editBtn);
  40. let removeBtn = document.createElement("button");
  41. removeBtn.onclick = ()=>{this.removeIngredient(item.id, row)};
  42. removeBtn.innerText = "Remove";
  43. removeBtn.className = "edit-button";
  44. action.appendChild(removeBtn);
  45. }
  46. },
  47. //sorts this.items by specified property
  48. sortIngredients: function(property){
  49. if(this.currentSort === property){
  50. this.items.sort((a, b) => (a[property] > b[property]) ? -1 : 1);
  51. this.currentSort = "";
  52. }else{
  53. this.items.sort((a, b) => (a[property] > b[property]) ? 1 : -1);
  54. this.currentSort = property;
  55. }
  56. this.populateIngredients();
  57. },
  58. //Empty this.items list
  59. //Add ingredients back to this.items list based on this.filter input
  60. filter: function(){
  61. this.items = [];
  62. let searchString = document.querySelector("#filter").value.toLowerCase();
  63. for(let item of merchant.inventory){
  64. if(item.ingredient.name.toLowerCase().includes(searchString)){
  65. this.items.push({
  66. id: item._id,
  67. name: item.ingredient.name,
  68. category: item.ingredient.category,
  69. quantity: item.quantity,
  70. unit: item.ingredient.unit
  71. });
  72. }
  73. }
  74. this.sortIngredients("name");
  75. },
  76. //Create input allowing for user edit of ingredient
  77. editIngredient: function(id, row){
  78. let quantity = row.children[2];
  79. let button = row.children[4].children[0];
  80. let originalQuantity = quantity.innerText;
  81. let quantityInput = document.createElement("input");
  82. quantityInput.type = "number";
  83. quantityInput.step = "0.01";
  84. quantityInput.value = quantity.innerText;
  85. quantity.innerText = "";
  86. quantity.appendChild(quantityInput);
  87. button.innerText = "Save";
  88. button.onclick = ()=>{this.updateOne(id, row, originalQuantity)};
  89. },
  90. //Save user input of ingredient
  91. //Update both page and database
  92. updateOne: function(id, row, originalQuantity){
  93. let quantityField = row.children[2];
  94. let quantity = quantityField.children[0].value;
  95. let button = row.children[4].children[0];
  96. quantityField.removeChild(quantityField.firstChild);
  97. if(validator.ingredient.quantity(quantity)){
  98. let updateIngredient = merchant.inventory.find(i => i._id === id);
  99. updateIngredient.quantity = quantity;
  100. axios.post("/merchant/ingredients/update", {ingredientId: id, quantityChange: quantity - originalQuantity})
  101. .then((merchant)=>{
  102. banner.createNotification("The ingredient has been successfully updated");
  103. })
  104. .catch((err)=>{
  105. banner.createError("There was an error and the ingredient was not updated");
  106. console.log(err);
  107. });
  108. quantityField.innerText = quantity;
  109. }else{
  110. quantityField.innerText = originalQuantity;
  111. }
  112. button.innerText = "Edit";
  113. button.onclick = ()=>{this.editIngredient(id, row)};
  114. },
  115. //Delete an ingredient from both the page and the database
  116. removeIngredient: function(id, row){
  117. axios.post("/merchant/ingredients/remove", {ingredientId: id})
  118. .then(()=>{
  119. for(let i = 0; i < merchant.inventory.length; i++){
  120. if(id === merchant.inventory[i]._id){
  121. merchant.inventory.splice(i, 1);
  122. break;
  123. }
  124. }
  125. row.parentNode.removeChild(row);
  126. banner.createNotification("The ingredient has been removed from your inventory");
  127. })
  128. .catch((err)=>{
  129. banner.createError("There was an error and the ingredient has not been removed from your inventory");
  130. console.log(err);
  131. });
  132. },
  133. }