inventory.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. name.classList = "truncateLong";
  24. row.appendChild(name);
  25. let category = document.createElement("td");
  26. category.innerText = item.category;
  27. row.appendChild(category);
  28. let quantity = document.createElement("td");
  29. quantity.innerText = item.quantity;
  30. row.appendChild(quantity);
  31. let unit = document.createElement("td");
  32. unit.innerText = item.unit;
  33. row.appendChild(unit);
  34. let action = document.createElement("td");
  35. row.appendChild(action);
  36. let editBtn = document.createElement("button");
  37. editBtn.onclick = ()=>{this.editIngredient(item.id, row)};
  38. editBtn.innerText = "Edit";
  39. editBtn.className = "button-small"
  40. action.appendChild(editBtn);
  41. let removeBtn = document.createElement("button");
  42. removeBtn.onclick = ()=>{this.removeIngredient(item.id, row)};
  43. removeBtn.innerText = "Remove";
  44. removeBtn.className = "button-small";
  45. action.appendChild(removeBtn);
  46. }
  47. },
  48. //sorts this.items by specified property
  49. sortIngredients: function(property){
  50. if(this.currentSort === property){
  51. this.items.sort((a, b) => (a[property] > b[property]) ? -1 : 1);
  52. this.currentSort = "";
  53. }else{
  54. this.items.sort((a, b) => (a[property] > b[property]) ? 1 : -1);
  55. this.currentSort = property;
  56. }
  57. this.populateIngredients();
  58. },
  59. //Empty this.items list
  60. //Add ingredients back to this.items list based on this.filter input
  61. filter: function(){
  62. this.items = [];
  63. let searchString = document.querySelector("#filter").value.toLowerCase();
  64. for(let item of merchant.inventory){
  65. if(item.ingredient.name.toLowerCase().includes(searchString)){
  66. this.items.push({
  67. id: item.ingredient._id,
  68. name: item.ingredient.name,
  69. category: item.ingredient.category,
  70. quantity: item.quantity,
  71. unit: item.ingredient.unit
  72. });
  73. }
  74. }
  75. this.sortIngredients("name");
  76. },
  77. //Create input allowing for user edit of ingredient
  78. editIngredient: function(id, row){
  79. let quantity = row.children[2];
  80. let editButton = row.children[4].children[0];
  81. let removeButton = row.children[4].children[1];
  82. let originalQuantity = quantity.innerText;
  83. let quantityInput = document.createElement("input");
  84. quantityInput.type = "number";
  85. quantityInput.step = "0.01";
  86. quantityInput.onkeypress = (event)=>{if(event.keyCode===13) this.updateOne(id, row, originalQuantity)};
  87. quantityInput.value = quantity.innerText;
  88. quantity.innerText = "";
  89. quantity.appendChild(quantityInput);
  90. editButton.innerText = "Save";
  91. editButton.onclick = ()=>{this.updateOne(id, row, originalQuantity)};
  92. removeButton.innerText = "Cancel";
  93. removeButton.onclick = ()=>{this.cancelEdit(id, row, originalQuantity)};
  94. },
  95. cancelEdit: function(id, row, amount){
  96. let quantityField = row.children[2];
  97. quantityField.removeChild(quantityField.firstChild);
  98. quantityField.innerText = amount;
  99. let saveButton = row.children[4].children[0];
  100. saveButton.innerText = "Edit";
  101. saveButton.onclick = ()=>{this.editIngredient(id, row)};
  102. let cancelButton = row.children[4].children[1];
  103. cancelButton.innerText = "Remove";
  104. cancelButton.onclick = ()=>{this.removeIngredient(id, row)};
  105. },
  106. //Save user input of ingredient
  107. //Update both page and database
  108. updateOne: function(id, row, originalQuantity){
  109. let quantityField = row.children[2];
  110. let quantity = quantityField.children[0].value;
  111. let button = row.children[4].children[0];
  112. quantityField.removeChild(quantityField.firstChild);
  113. if(validator.ingredient.quantity(quantity)){
  114. let updateIngredient = merchant.inventory.find(i => i.ingredient._id === id);
  115. updateIngredient.quantity = quantity;
  116. axios.post("/merchant/ingredients/update", {ingredientId: id, quantityChange: quantity - originalQuantity})
  117. .then((response)=>{
  118. if(typeof(response.data) === "string"){
  119. banner.createError(response.data);
  120. }else{
  121. banner.createNotification("The ingredient has been successfully updated");
  122. quantityField.innerText = quantity;
  123. }
  124. })
  125. .catch((err)=>{
  126. banner.createError("Error: The ingredient could not be updated");
  127. });
  128. }else{
  129. quantityField.innerText = originalQuantity;
  130. }
  131. button.innerText = "Edit";
  132. button.onclick = ()=>{this.editIngredient(id, row)};
  133. },
  134. //Delete an ingredient from both the page and the database
  135. removeIngredient: function(id, row){
  136. let canRemove = true;
  137. for(let recipe of merchant.recipes){
  138. for(let ingredient of recipe.ingredients){
  139. if(ingredient.ingredient._id === id){
  140. canRemove = false;
  141. break;
  142. }
  143. }
  144. }
  145. if(canRemove){
  146. axios.post("/merchant/ingredients/remove", {ingredientId: id})
  147. .then((result)=>{
  148. if(typeof(result.data) === "string"){
  149. banner.createError(result.data);
  150. }else{
  151. for(let i = 0; i < merchant.inventory.length; i++){
  152. if(id === merchant.inventory[i].ingredient._id){
  153. merchant.inventory.splice(i, 1);
  154. break;
  155. }
  156. }
  157. row.parentNode.removeChild(row);
  158. banner.createNotification("The ingredient has been removed from your inventory");
  159. }
  160. })
  161. .catch((err)=>{
  162. banner.createError("There was an error and the ingredient has not been removed from your inventory");
  163. console.log(err);
  164. });
  165. }else{
  166. banner.createError("You must remove this ingredient from all recipes before you can remove it from your inventory");
  167. }
  168. },
  169. }