addIngredients.ejs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <div id="addIngredients">
  2. <div class="sidebarIconButtons">
  3. <button class="iconButton" 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. <p></p>
  32. <input type="number" min="0" step="0.01">
  33. </div>
  34. </template>
  35. <script>
  36. let addIngredientsComp = {
  37. isPopulated: false,
  38. display: function(){
  39. let sidebar = document.querySelector("#addIngredients");
  40. if(!this.isPopulated){
  41. let addIngredientsDiv = document.getElementById("addIngredientList");
  42. let categoryTemplate = document.getElementById("addIngredientsCategory");
  43. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  44. fetch("/ingredients")
  45. .then((response) => response.json())
  46. .then((response)=>{
  47. if(typeof(response) === "string"){
  48. banner.createError(response);
  49. }else{
  50. for(let i = 0; i < merchant.inventory.length; i++){
  51. for(let j = 0; j < response.length; j++){
  52. if(merchant.inventory[i].ingredient._id === response[j]._id){
  53. response.splice(j, 1);
  54. break;
  55. }
  56. }
  57. }
  58. let categories = categorizeIngredientsFromDB(response);
  59. while(addIngredientsDiv.children.length > 0){
  60. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  61. }
  62. this.addIngredientsDiv = [];
  63. for(let i = 0; i < categories.length; i++){
  64. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  65. categoryDiv.children[0].children[0].innerText = categories[i].name;
  66. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  67. categoryDiv.children[1].style.display = "none";
  68. categoryDiv.children[0].children[1].children[1].style.display = "none";
  69. addIngredientsDiv.appendChild(categoryDiv);
  70. for(let j = 0; j < categories[i].ingredients.length; j++){
  71. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  72. ingredientDiv.children[1].innerText = categories[i].ingredients[j].name;
  73. ingredientDiv._id = categories[i].ingredients[j].id;
  74. ingredientDiv._name = categories[i].ingredients[j].name;
  75. ingredientDiv._unit = categories[i].ingredients[j].unit;
  76. ingredientDiv._category = categories[i].name;
  77. categoryDiv.children[1].appendChild(ingredientDiv);
  78. this.addIngredientsDiv.push(ingredientDiv);
  79. }
  80. }
  81. }
  82. })
  83. .catch((err)=>{
  84. banner.createError("Unable to retrieve data");
  85. });
  86. this.isPopulated = true;
  87. }
  88. openSidebar(sidebar);
  89. },
  90. toggleAddIngredient: function(categoryElement){
  91. let button = categoryElement.children[0].children[1];
  92. let ingredientDisplay = categoryElement.children[1];
  93. if(ingredientDisplay.style.display === "none"){
  94. ingredientDisplay.style.display = "flex";
  95. button.children[0].style.display = "none";
  96. button.children[1].style.display = "block";
  97. }else{
  98. ingredientDisplay.style.display = "none";
  99. button.children[0].style.display = "block";
  100. button.children[1].style.display = "none";
  101. }
  102. },
  103. submitAddIngredients: function(){
  104. let addIngredients = [];
  105. for(let i = 0; i < this.addIngredientsDiv.length; i++){
  106. let ingredient = this.addIngredientsDiv[i];
  107. if(ingredient.children[0].checked){
  108. if(!validator.ingredient.quantity(ingredient.children[2].value)){
  109. return;
  110. }
  111. addIngredients.push({
  112. ingredient: {
  113. _id: ingredient._id,
  114. name: ingredient._name,
  115. category: ingredient._category,
  116. unit: ingredient._unit
  117. },
  118. quantity: ingredient.children[2].value,
  119. });
  120. }
  121. }
  122. if(addIngredients.length > 0){
  123. fetch("/merchant/ingredients/add", {
  124. method: "PUT",
  125. headers: {
  126. "Content-Type": "application/json;charset=utf-8"
  127. },
  128. body: JSON.stringify(addIngredients)
  129. })
  130. .then((response)=>{
  131. if(typeof(response.data) === "string"){
  132. banner.createError(response.data);
  133. }else{
  134. banner.createNotification("Ingredients added");
  135. updateInventory(addIngredients);
  136. }
  137. })
  138. .catch((err)=>{
  139. banner.createError("Unable to update data. Please refresh the page");
  140. });
  141. }
  142. }
  143. }
  144. </script>
  145. </div>