inventory.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. window.inventoryObj = {
  2. items: [],
  3. currentSort: "",
  4. isPopulated: false,
  5. display: function(){
  6. clearScreen();
  7. document.querySelector("#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. quantityInput.focus();
  95. },
  96. cancelEdit: function(id, row, amount){
  97. let quantityField = row.children[2];
  98. quantityField.removeChild(quantityField.firstChild);
  99. quantityField.innerText = amount;
  100. let saveButton = row.children[4].children[0];
  101. saveButton.innerText = "Edit";
  102. saveButton.onclick = ()=>{this.editIngredient(id, row)};
  103. let cancelButton = row.children[4].children[1];
  104. cancelButton.innerText = "Remove";
  105. cancelButton.onclick = ()=>{this.removeIngredient(id, row)};
  106. },
  107. //Save user input of ingredient
  108. //Update both page and database
  109. updateOne: function(id, row, originalQuantity){
  110. let quantityField = row.children[2];
  111. let quantity = quantityField.children[0].value;
  112. let saveButton = row.children[4].children[0];
  113. let cancelButton = row.children[4].children[1];
  114. quantityField.removeChild(quantityField.firstChild);
  115. if(validator.ingredient.quantity(quantity)){
  116. let updateIngredient = merchant.inventory.find(i => i.ingredient._id === id);
  117. updateIngredient.quantity = quantity;
  118. axios.post("/merchant/ingredients/update", {ingredientId: id, quantityChange: quantity - originalQuantity})
  119. .then((response)=>{
  120. if(typeof(response.data) === "string"){
  121. banner.createError(response.data);
  122. }else{
  123. banner.createNotification("The ingredient has been successfully updated");
  124. quantityField.innerText = quantity;
  125. }
  126. })
  127. .catch((err)=>{
  128. banner.createError("Error: The ingredient could not be updated");
  129. });
  130. }else{
  131. quantityField.innerText = originalQuantity;
  132. }
  133. saveButton.innerText = "Edit";
  134. saveButton.onclick = ()=>{this.editIngredient(id, row)};
  135. cancelButton.innerText = "Remove";
  136. cancelButton.onclick = ()=>{this.removeIngredient(id, row)};
  137. },
  138. //Delete an ingredient from both the page and the database
  139. removeIngredient: function(id, row){
  140. let canRemove = true;
  141. for(let recipe of merchant.recipes){
  142. for(let ingredient of recipe.ingredients){
  143. if(ingredient.ingredient._id === id){
  144. canRemove = false;
  145. break;
  146. }
  147. }
  148. }
  149. if(canRemove){
  150. axios.post("/merchant/ingredients/remove", {ingredientId: id})
  151. .then((result)=>{
  152. if(typeof(result.data) === "string"){
  153. banner.createError(result.data);
  154. }else{
  155. for(let i = 0; i < merchant.inventory.length; i++){
  156. if(id === merchant.inventory[i].ingredient._id){
  157. merchant.inventory.splice(i, 1);
  158. break;
  159. }
  160. }
  161. row.parentNode.removeChild(row);
  162. banner.createNotification("The ingredient has been removed from your inventory");
  163. }
  164. })
  165. .catch((err)=>{
  166. banner.createError("There was an error and the ingredient has not been removed from your inventory");
  167. });
  168. }else{
  169. banner.createError("You must remove this ingredient from all recipes before you can remove it from your inventory");
  170. }
  171. },
  172. }