inventory.js 6.4 KB

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