inventory.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.ingredient._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. axios.post("/ingredients/update", {
  85. id: id,
  86. quantity: quantity
  87. })
  88. .then((ingredient)=>{
  89. banner.createNotification("The ingredient has been successfully updated");
  90. })
  91. .catch((err)=>{
  92. banner.createError("There was an error and the ingredient was not updated");
  93. console.log(err);
  94. });
  95. quantityField.innerText = quantity;
  96. }else{
  97. quantityField.innerText = originalQuantity;
  98. }
  99. button.innerText = "Edit";
  100. button.onclick = ()=>{editIngredient(id, row)};
  101. }
  102. //Delete an ingredient from both the page and the database
  103. let removeIngredient = (id, row)=>{
  104. axios.post("/ingredients/remove", {id: id})
  105. .then((merchant)=>{
  106. for(let i = 0; i < items.length; i++){
  107. if(id === items[i].id){
  108. items.splice(i, 1);
  109. }
  110. }
  111. banner.createNotification("The ingredient has been removed from your inventory");
  112. renderIngredients();
  113. })
  114. .catch((err)=>{
  115. banner.createError("There was an error and the ingredient has not been removed from your inventory");
  116. console.log(err);
  117. });
  118. }
  119. //Display the modal to allow for adding a new ingredient
  120. let displayAdd = ()=>{
  121. let modal = document.querySelector(".add-ingredient");
  122. let removeModal = (modal)=>{modal.style.visibility = "hidden";}
  123. modal.onclick = ()=>{removeModal(modal);}
  124. modal.style.visibility = "visible";
  125. }
  126. //Update new ingredient on both the page and the database
  127. //Close the modal
  128. let addIngredient = ()=>{
  129. let content = document.querySelector(".modal-content");
  130. let newIngredient = {
  131. ingredient: {
  132. name: content.children[0].children[0].value,
  133. category: content.children[1].children[0].value,
  134. unitType: content.children[3].children[0].value
  135. },
  136. quantity: content.children[2].children[0].value
  137. }
  138. if(validator.ingredient.all(newIngredient.ingredient, newIngredient.quantity)){
  139. axios.post("/ingredients/createone", newIngredient)
  140. .then((ingredient)=>{
  141. items.push({
  142. id: ingredient._id,
  143. name: newIngredient.ingredient.name,
  144. category: newIngredient.ingredient.category,
  145. quantity: newIngredient.quantity,
  146. unit: newIngredient.ingredient.unitType
  147. })
  148. sortIngredients("name");
  149. renderIngredients();
  150. banner.createNotification("The new ingredient has been successfully added to your inventory");
  151. })
  152. .catch((err)=>{
  153. banner.createError("There was an error and the ingredient could not be added to your inventory");
  154. console.log(err);
  155. });
  156. }
  157. let modal = document.querySelector(".add-ingredient");
  158. modal.style.visibility = "hidden";
  159. }
  160. //Initial run
  161. filter();