addIngredients.ejs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. <button class="button addIngredientsBtn" onclick="newIngredientComp.display()">Create New</button>
  14. <template id="addIngredientsCategory">
  15. <div class="addIngredientsCategory">
  16. <div class="categoryHeader">
  17. <h5></h5>
  18. <button>
  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="6 9 12 15 18 9"></polyline>
  21. </svg>
  22. <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  23. <polyline points="18 15 12 9 6 15"></polyline>
  24. </svg>
  25. </button>
  26. </div>
  27. <div class="addIngredientsIngredients"></div>
  28. </div>
  29. </template>
  30. <template id="addIngredientsIngredient">
  31. <div class="addIngredientsIngredient">
  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. banner.createError("Unable to retrieve data");
  86. });
  87. this.isPopulated = true;
  88. }
  89. openSidebar(sidebar);
  90. },
  91. toggleAddIngredient: function(categoryElement){
  92. let button = categoryElement.children[0].children[1];
  93. let ingredientDisplay = categoryElement.children[1];
  94. if(ingredientDisplay.style.display === "none"){
  95. ingredientDisplay.style.display = "flex";
  96. button.children[0].style.display = "none";
  97. button.children[1].style.display = "block";
  98. }else{
  99. ingredientDisplay.style.display = "none";
  100. button.children[0].style.display = "block";
  101. button.children[1].style.display = "none";
  102. }
  103. },
  104. submitAddIngredients: function(){
  105. let addIngredients = [];
  106. for(let i = 0; i < this.addIngredientsDiv.length; i++){
  107. let ingredient = this.addIngredientsDiv[i];
  108. if(ingredient.children[0].checked){
  109. if(!validator.ingredientQuantity(ingredient.children[2].value)){
  110. return;
  111. }
  112. addIngredients.push({
  113. ingredient: {
  114. _id: ingredient._id,
  115. name: ingredient._name,
  116. category: ingredient._category,
  117. unit: ingredient._unit
  118. },
  119. quantity: ingredient.children[2].value,
  120. });
  121. }
  122. }
  123. if(addIngredients.length > 0){
  124. fetch("/merchant/ingredients/add", {
  125. method: "PUT",
  126. headers: {
  127. "Content-Type": "application/json;charset=utf-8"
  128. },
  129. body: JSON.stringify(addIngredients)
  130. })
  131. .then((response)=>{
  132. if(typeof(response.data) === "string"){
  133. banner.createError(response.data);
  134. }else{
  135. banner.createNotification("Ingredients added");
  136. updateInventory(addIngredients);
  137. }
  138. })
  139. .catch((err)=>{
  140. banner.createError("Unable to update data. Please refresh the page");
  141. });
  142. }
  143. }
  144. }
  145. </script>
  146. </div>