inventory.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. let items = []; //the ingredients to be displayed
  2. let tbody = document.querySelector("tbody");
  3. //Remove any existing ingredients in table
  4. //loop through items and create rows for the table
  5. let renderIngredients = ()=>{
  6. while(tbody.hasChildNodes()){
  7. tbody.removeChild(tbody.firstChild);
  8. }
  9. for(let item of items){
  10. let row = document.createElement("tr");
  11. tbody.appendChild(row);
  12. let name = document.createElement("td");
  13. name.innerText = item.name;
  14. row.appendChild(name);
  15. let category = document.createElement("td");
  16. category.innerText = item.category;
  17. row.appendChild(category);
  18. let quantity = document.createElement("td");
  19. quantity.innerText = item.quantity;
  20. row.appendChild(quantity);
  21. let unit = document.createElement("td");
  22. unit.innerText = item.unit;
  23. row.appendChild(unit);
  24. let action = document.createElement("td");
  25. row.appendChild(action);
  26. let editBtn = document.createElement("button");
  27. editBtn.onclick = ()=>{editIngredient(item.id, row)};
  28. editBtn.innerText = "Edit";
  29. editBtn.className = "edit-button"
  30. action.appendChild(editBtn);
  31. let removeBtn = document.createElement("button");
  32. removeBtn.onclick = ()=>{removeIngredient(item.id, row)};
  33. removeBtn.innerText = "Remove";
  34. removeBtn.className = "edit-button";
  35. action.appendChild(removeBtn);
  36. }
  37. }
  38. //sorts items by specified property
  39. let sortIngredients = (property)=>{
  40. items.sort((a, b) => (a[property] > b[property]) ? 1 : -1);
  41. renderIngredients();
  42. }
  43. //Empty items list
  44. //Add ingredients back to items list based on filter input
  45. let filter = ()=>{
  46. items = [];
  47. let searchString = document.querySelector("#filter").value.toLowerCase();
  48. for(let item of merchant.inventory){
  49. if(item.ingredient.name.toLowerCase().includes(searchString)){
  50. items.push({
  51. id: item._id,
  52. name: item.ingredient.name,
  53. category: item.ingredient.category,
  54. quantity: item.quantity,
  55. unit: item.ingredient.unitType
  56. });
  57. }
  58. }
  59. sortIngredients("name");
  60. renderIngredients(items);
  61. }
  62. //Create input allowing for user edit of ingredient
  63. let editIngredient = (id, row)=>{
  64. let quantity = row.children[2];
  65. let button = row.children[4].children[0];
  66. let originalQuantity = quantity.innerText;
  67. let quantityInput = document.createElement("input");
  68. quantityInput.type = "number";
  69. quantityInput.step = "0.01";
  70. quantityInput.value = quantity.innerText;
  71. quantity.innerText = "";
  72. quantity.appendChild(quantityInput);
  73. button.innerText = "Save";
  74. button.onclick = ()=>{updateOne(id, row, originalQuantity)};
  75. }
  76. //Save user input of ingredient
  77. //Update both page and database
  78. let updateOne = (id, row, originalQuantity)=>{
  79. let quantityField = row.children[2];
  80. let quantity = quantityField.children[0].value;
  81. let button = row.children[4].children[0];
  82. quantityField.removeChild(quantityField.firstChild);
  83. if(validator.ingredient.quantity(quantity)){
  84. let updateIngredient = merchant.inventory.find(i => i._id === id);
  85. updateIngredient.quantity = quantity;
  86. axios.post("/ingredients/update", {
  87. id: id,
  88. quantity: quantity
  89. })
  90. .then((ingredient)=>{
  91. banner.createNotification("The ingredient has been successfully updated");
  92. })
  93. .catch((err)=>{
  94. banner.createError("There was an error and the ingredient was not updated");
  95. console.log(err);
  96. });
  97. quantityField.innerText = quantity;
  98. }else{
  99. quantityField.innerText = originalQuantity;
  100. }
  101. button.innerText = "Edit";
  102. button.onclick = ()=>{editIngredient(id, row)};
  103. }
  104. //Delete an ingredient from both the page and the database
  105. let removeIngredient = (id, row)=>{
  106. for(let i = 0; i < merchant.inventory.length; i++){
  107. if(merchant.inventory[i]._id === id){
  108. merchant.inventory.splice(i, 1);
  109. break;
  110. }
  111. }
  112. axios.post("/merchant/update", merchant)
  113. .then((merchant)=>{
  114. for(let i = 0; i < items.length; i++){
  115. if(id === items[i].id){
  116. items.splice(i, 1);
  117. }
  118. }
  119. banner.createNotification("The ingredient has been removed from your inventory");
  120. renderIngredients();
  121. })
  122. .catch((err)=>{
  123. banner.createError("There was an error and the ingredient has not been removed from your inventory");
  124. console.log(err);
  125. });
  126. }
  127. //Display the modal to allow for adding a new ingredient
  128. let displayAdd = ()=>{
  129. let modal = document.querySelector(".add-ingredient");
  130. let removeModal = (modal)=>{modal.style.visibility = "hidden";}
  131. modal.onclick = ()=>{removeModal(modal);}
  132. modal.style.visibility = "visible";
  133. }
  134. //Update new ingredient on both the page and the database
  135. //Close the modal
  136. let addIngredient = ()=>{
  137. let content = document.querySelector(".modal-content");
  138. let newIngredient = {
  139. ingredient: {
  140. name: content.children[0].children[0].value,
  141. category: content.children[1].children[0].value,
  142. unitType: content.children[3].children[0].value
  143. },
  144. quantity: content.children[2].children[0].value
  145. }
  146. if(validator.ingredient.all(newIngredient.ingredient, newIngredient.quantity)){
  147. axios.post("/ingredients/createone", newIngredient)
  148. .then((ingredient)=>{
  149. items.push({
  150. id: ingredient._id,
  151. name: newIngredient.ingredient.name,
  152. category: newIngredient.ingredient.category,
  153. quantity: newIngredient.quantity,
  154. unit: newIngredient.ingredient.unitType
  155. })
  156. sortIngredients("name");
  157. renderIngredients();
  158. banner.createNotification("The new ingredient has been successfully added to your inventory");
  159. })
  160. .catch((err)=>{
  161. banner.createError("There was an error and the ingredient could not be added to your inventory");
  162. console.log(err);
  163. });
  164. }
  165. let modal = document.querySelector(".add-ingredient");
  166. modal.style.visibility = "hidden";
  167. }
  168. //Initial run
  169. filter();