addIngredients.ejs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <div id="addIngredients">
  2. <div class="sidebarIconButtons">
  3. <button onclick="closeSidebar()">
  4. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  5. <line x1="5" y1="12" x2="19" y2="12"></line>
  6. <polyline points="12 5 19 12 12 19"></polyline>
  7. </svg>
  8. </button>
  9. </div>
  10. <h1>Add New Ingredients</h1>
  11. <div id="addIngredientList"></div>
  12. <button class="button addIngredientsBtn" onclick="addIngredientsComp.submitAddIngredients()">Add</button>
  13. <template id="addIngredientsCategory">
  14. <div class="addIngredientsCategory">
  15. <div class="categoryHeader">
  16. <h5></h5>
  17. <button>
  18. <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  19. <polyline points="6 9 12 15 18 9"></polyline>
  20. </svg>
  21. <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  22. <polyline points="18 15 12 9 6 15"></polyline>
  23. </svg>
  24. </button>
  25. </div>
  26. <div class="addIngredientsIngredients"></div>
  27. </div>
  28. </template>
  29. <template id="addIngredientsIngredient">
  30. <div class="addIngredientsIngredient">
  31. <input type="checkbox">
  32. <p></p>
  33. <input type="number" min="0" step="0.01">
  34. </div>
  35. </template>
  36. <script>
  37. let addIngredientsComp = {
  38. isPopulated: false,
  39. display: function(){
  40. let sidebar = document.querySelector("#addIngredients");
  41. if(!this.isPopulated){
  42. let addIngredientsDiv = document.getElementById("addIngredientList");
  43. let categoryTemplate = document.getElementById("addIngredientsCategory");
  44. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  45. fetch("/ingredients")
  46. .then((response) => response.json())
  47. .then((response)=>{
  48. if(typeof(response) === "string"){
  49. banner.createError(response);
  50. }else{
  51. for(let i = 0; i < merchant.inventory.length; i++){
  52. for(let j = 0; j < response.length; j++){
  53. if(merchant.inventory[i].ingredient._id === response[j]._id){
  54. response.splice(j, 1);
  55. break;
  56. }
  57. }
  58. }
  59. let categories = categorizeIngredientsFromDB(response);
  60. while(addIngredientsDiv.children.length > 0){
  61. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  62. }
  63. this.addIngredientsDiv = [];
  64. for(let i = 0; i < categories.length; i++){
  65. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  66. categoryDiv.children[0].children[0].innerText = categories[i].name;
  67. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  68. categoryDiv.children[1].style.display = "none";
  69. categoryDiv.children[0].children[1].children[1].style.display = "none";
  70. addIngredientsDiv.appendChild(categoryDiv);
  71. for(let j = 0; j < categories[i].ingredients.length; j++){
  72. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  73. ingredientDiv.children[1].innerText = categories[i].ingredients[j].name;
  74. ingredientDiv._id = categories[i].ingredients[j].id;
  75. ingredientDiv._name = categories[i].ingredients[j].name;
  76. ingredientDiv._unit = categories[i].ingredients[j].unit;
  77. ingredientDiv._category = categories[i].name;
  78. categoryDiv.children[1].appendChild(ingredientDiv);
  79. this.addIngredientsDiv.push(ingredientDiv);
  80. }
  81. }
  82. }
  83. })
  84. .catch((err)=>{
  85. console.log(err);
  86. banner.createError("Unable to retrieve data");
  87. });
  88. this.isPopulated = true;
  89. }
  90. openSidebar(sidebar);
  91. },
  92. toggleAddIngredient: function(categoryElement){
  93. let button = categoryElement.children[0].children[1];
  94. let ingredientDisplay = categoryElement.children[1];
  95. if(ingredientDisplay.style.display === "none"){
  96. ingredientDisplay.style.display = "flex";
  97. button.children[0].style.display = "none";
  98. button.children[1].style.display = "block";
  99. }else{
  100. ingredientDisplay.style.display = "none";
  101. button.children[0].style.display = "block";
  102. button.children[1].style.display = "none";
  103. }
  104. },
  105. submitAddIngredients: function(){
  106. let addIngredients = [];
  107. for(let i = 0; i < this.addIngredientsDiv.length; i++){
  108. let ingredient = this.addIngredientsDiv[i];
  109. if(ingredient.children[0].checked){
  110. if(!validator.ingredient.quantity(ingredient.children[2].value)){
  111. return;
  112. }
  113. addIngredients.push({
  114. ingredient: {
  115. _id: ingredient._id,
  116. name: ingredient._name,
  117. category: ingredient._category,
  118. unit: ingredient._unit
  119. },
  120. quantity: ingredient.children[2].value,
  121. });
  122. }
  123. }
  124. if(addIngredients.length > 0){
  125. fetch("/merchant/ingredients/add", {
  126. method: "PUT",
  127. headers: {
  128. "Content-Type": "application/json;charset=utf-8"
  129. },
  130. body: JSON.stringify(addIngredients)
  131. })
  132. .then((response)=>{
  133. if(typeof(response.data) === "string"){
  134. banner.createError(response.data);
  135. }else{
  136. banner.createNotification("Ingredients added");
  137. updateInventory(addIngredients);
  138. }
  139. })
  140. .catch((err)=>{
  141. console.log(err);
  142. banner.createError("Unable to update data. Please refresh the page");
  143. });
  144. }
  145. }
  146. }
  147. </script>
  148. </div>