addIngredients.ejs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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" 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. let categories = categorizeIngredientsFromDB(response);
  50. while(addIngredientsDiv.children.length > 0){
  51. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  52. }
  53. this.addIngredientsDiv = [];
  54. for(let i = 0; i < categories.length; i++){
  55. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  56. categoryDiv.children[0].children[0].innerText = categories[i].name;
  57. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  58. categoryDiv.children[1].style.display = "none";
  59. categoryDiv.children[0].children[1].children[1].style.display = "none";
  60. addIngredientsDiv.appendChild(categoryDiv);
  61. for(let j = 0; j < categories[i].ingredients.length; j++){
  62. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  63. ingredientDiv.children[1].innerText = categories[i].ingredients[j].name;
  64. ingredientDiv._id = categories[i].ingredients[j].id;
  65. ingredientDiv._name = categories[i].ingredients[j].name;
  66. ingredientDiv._unit = categories[i].ingredients[j].unit;
  67. ingredientDiv._category = categories[i].name;
  68. categoryDiv.children[1].appendChild(ingredientDiv);
  69. this.addIngredientsDiv.push(ingredientDiv);
  70. }
  71. }
  72. }
  73. })
  74. .catch((err)=>{
  75. banner.createError("Unable to retrieve data");
  76. });
  77. this.isPopulated = true;
  78. }
  79. openSidebar(sidebar);
  80. },
  81. toggleAddIngredient: function(categoryElement){
  82. let button = categoryElement.children[0].children[1];
  83. let ingredientDisplay = categoryElement.children[1];
  84. if(ingredientDisplay.style.display === "none"){
  85. ingredientDisplay.style.display = "flex";
  86. button.children[0].style.display = "none";
  87. button.children[1].style.display = "block";
  88. }else{
  89. ingredientDisplay.style.display = "none";
  90. button.children[0].style.display = "block";
  91. button.children[1].style.display = "none";
  92. }
  93. },
  94. submitAddIngredients: function(){
  95. let addIngredients = [];
  96. for(let i = 0; i < this.addIngredientsDiv.length; i++){
  97. let ingredient = this.addIngredientsDiv[i];
  98. if(ingredient.children[0].checked){
  99. if(!validator.ingredient.quantity(ingredient.children[2].value)){
  100. return;
  101. }
  102. addIngredients.push({
  103. ingredient: {
  104. _id: ingredient._id,
  105. name: ingredient._name,
  106. category: ingredient._category,
  107. unit: ingredient._unit
  108. },
  109. quantity: ingredient.children[2].value,
  110. });
  111. }
  112. }
  113. fetch("/merchant/ingredients/add", {
  114. method: "PUT",
  115. headers: {
  116. "Content-Type": "application/json;charset=utf-8"
  117. },
  118. body: JSON.stringify(addIngredients)
  119. })
  120. .then((response)=>{
  121. if(typeof(response.data) === "string"){
  122. banner.createError(response.data);
  123. }else{
  124. banner.createNotification("Ingredients added");
  125. updateInventory(addIngredients);
  126. }
  127. })
  128. .catch((err)=>{
  129. console.log(err);
  130. banner.createError("Unable to update data. Please refresh the page");
  131. });
  132. }
  133. }
  134. </script>
  135. </div>