addIngredients.ejs 7.8 KB

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