inventory.js 5.5 KB

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