inventory.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. let items = [];
  2. let tbody = document.querySelector("tbody");
  3. let renderIngredients = ()=>{
  4. while(tbody.hasChildNodes()){
  5. tbody.removeChild(tbody.firstChild);
  6. }
  7. for(let item of items){
  8. let row = document.createElement("tr");
  9. tbody.appendChild(row);
  10. let name = document.createElement("td");
  11. name.innerText = item.name;
  12. row.appendChild(name);
  13. let category = document.createElement("td");
  14. category.innerText = item.category;
  15. row.appendChild(category);
  16. let quantity = document.createElement("td");
  17. quantity.innerText = item.quantity;
  18. row.appendChild(quantity);
  19. let unit = document.createElement("td");
  20. unit.innerText = item.unit;
  21. row.appendChild(unit);
  22. let action = document.createElement("td");
  23. row.appendChild(action);
  24. let editBtn = document.createElement("button");
  25. editBtn.onclick = ()=>{editIngredient(item.id, row)};
  26. editBtn.innerText = "Edit";
  27. editBtn.className = "edit-button"
  28. action.appendChild(editBtn);
  29. let removeBtn = document.createElement("button");
  30. removeBtn.onclick = ()=>{removeIngredient(item.id, row)};
  31. removeBtn.innerText = "Remove";
  32. removeBtn.className = "edit-button";
  33. action.appendChild(removeBtn);
  34. }
  35. }
  36. let sortIngredients = (property)=>{
  37. items.sort((a, b) => (a[property] > b[property]) ? 1 : -1);
  38. renderIngredients();
  39. }
  40. let filter = ()=>{
  41. items = [];
  42. let searchString = document.querySelector("#filter").value.toLowerCase();
  43. for(let item of merchant.inventory){
  44. if(item.ingredient.name.toLowerCase().includes(searchString)){
  45. items.push({
  46. id: item.ingredient._id,
  47. name: item.ingredient.name,
  48. category: item.ingredient.category,
  49. quantity: item.quantity,
  50. unit: item.ingredient.unitType
  51. });
  52. }
  53. }
  54. sortIngredients("name");
  55. renderIngredients(items);
  56. }
  57. let editIngredient = (id, row)=>{
  58. let quantity = row.children[2];
  59. let button = row.children[4].children[0];
  60. let quantityInput = document.createElement("input");
  61. quantityInput.type = "number";
  62. quantityInput.step = "0.01";
  63. quantityInput.value = quantity.innerText;
  64. quantity.innerText = "";
  65. quantity.appendChild(quantityInput);
  66. button.innerText = "Save";
  67. button.onclick = ()=>{updateOne(id, row)};
  68. }
  69. let updateOne = (id, row)=>{
  70. let quantityField = row.children[2];
  71. let quantity = quantityField.children[0].value;
  72. let button = row.children[4].children[0];
  73. quantityField.removeChild(quantityField.firstChild);
  74. quantityField.innerText = quantity;
  75. button.innerText = "Edit";
  76. button.onclick = ()=>{editThis(item.id, row)};
  77. axios.post("/ingredients/update", {
  78. id: id,
  79. quantity: quantity
  80. })
  81. .then((ingredient)=>{
  82. banner.createNotification("The ingredient has been successfully updated");
  83. })
  84. .catch((err)=>{
  85. banner.createError("There was an error and the ingredient was not updated");
  86. console.log(err);
  87. });
  88. }
  89. let removeIngredient = (id, row)=>{
  90. axios.post("/ingredients/remove", {id: id})
  91. .then((merchant)=>{
  92. for(let i = 0; i < items.length; i++){
  93. if(id === items[i].id){
  94. items.splice(i, 1);
  95. }
  96. }
  97. banner.createNotification("The ingredient has been removed from your inventory");
  98. renderIngredients();
  99. })
  100. .catch((err)=>{
  101. banner.createError("There was an error and the ingredient has not been removed from your inventory");
  102. console.log(err);
  103. });
  104. }
  105. let displayAdd = ()=>{
  106. let modal = document.querySelector(".add-ingredient");
  107. let removeModal = (modal)=>{modal.style.visibility = "hidden";}
  108. modal.onclick = ()=>{removeModal(modal);}
  109. modal.style.visibility = "visible";
  110. }
  111. let addIngredient = ()=>{
  112. let content = document.querySelector(".modal-content");
  113. let newIngredient = {
  114. ingredient: {
  115. name: content.children[0].children[0].value,
  116. category: content.children[1].children[0].value,
  117. unitType: content.children[3].children[0].value
  118. },
  119. quantity: content.children[2].children[0].value
  120. }
  121. axios.post("/ingredients/createone", newIngredient)
  122. .then((ingredient)=>{
  123. items.push({
  124. id: ingredient._id,
  125. name: newIngredient.ingredient.name,
  126. category: newIngredient.ingredient.category,
  127. quantity: newIngredient.quantity,
  128. unit: newIngredient.ingredient.unitType
  129. })
  130. sortIngredients("name");
  131. renderIngredients();
  132. banner.createNotification("The new ingredient has been successfully added to your inventory");
  133. })
  134. .catch((err)=>{
  135. banner.createError("There was an error and the ingredient could not be added to your inventory");
  136. console.log(err);
  137. });
  138. let modal = document.querySelector(".add-ingredient");
  139. modal.style.visibility = "hidden";
  140. }
  141. filter();