recipeDetails.ejs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <div id="recipeDetails">
  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. <% if(merchant.pos === "none"){ %>
  10. <button class="iconButton" onclick="recipeDetailsComp.edit()">
  11. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  12. <path d="M12 20h9"></path>
  13. <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
  14. </svg>
  15. </button>
  16. <button class="iconButton" onclick="recipeDetailsComp.remove()">
  17. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  18. <polyline points="3 6 5 6 21 6"></polyline>
  19. <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
  20. </svg>
  21. </button>
  22. <% } %>
  23. </div>
  24. <h1 id="recipeName"></h1>
  25. <input id="recipeNameIn" type="text" style="display: none;">
  26. <div id="recipeIngredientList"></div>
  27. <div class="lineBorder"></div>
  28. <div id="recipePrice">
  29. <h3>Price</h3>
  30. <p></p>
  31. <input type="number" min="0" step="0.01" style="display: none;">
  32. </div>
  33. <button id="recipeUpdate" onclick="recipeDetailsComp.update()" class="button" style="display: none;">Update</button>
  34. <template id="recipeIngredient">
  35. <div class="recipeIngredient">
  36. <p></p>
  37. <input type="number" min="0" step="0.01" style="display: none;">
  38. <p></p>
  39. <button class="iconButton" style="display: none;">
  40. <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  41. <polyline points="3 6 5 6 21 6"></polyline>
  42. <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
  43. </svg>
  44. </button>
  45. </div>
  46. </template>
  47. <script>
  48. let recipeDetailsComp = {
  49. recipe: {},
  50. display: function(recipe){
  51. this.recipe = recipe;
  52. openSidebar(document.querySelector("#recipeDetails"));
  53. document.querySelector("#recipeName").style.display = "block";
  54. document.querySelector("#recipeNameIn").style.display = "none";
  55. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  56. let ingredientList = document.querySelector("#recipeIngredientList");
  57. while(ingredientList.children.length > 0){
  58. ingredientList.removeChild(ingredientList.firstChild);
  59. }
  60. let template = document.querySelector("#recipeIngredient").content.children[0];
  61. for(let i = 0; i < recipe.ingredients.length; i++){
  62. ingredientDiv = template.cloneNode(true);
  63. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  64. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  65. ingredientDiv._id = recipe.ingredients[i].ingredient._id;
  66. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  67. ingredientList.appendChild(ingredientDiv);
  68. }
  69. let price = document.querySelector("#recipePrice");
  70. price.children[1].style.display = "block";
  71. price.children[2].style.display = "none";
  72. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  73. document.querySelector("#recipeUpdate").style.display = "none";
  74. },
  75. edit: function(){
  76. let ingredientDivs = document.querySelector("#recipeIngredientList");
  77. let name = document.querySelector("#recipeName");
  78. let nameIn = document.querySelector("#recipeNameIn");
  79. name.style.display = "none";
  80. nameIn.style.display = "block";
  81. nameIn.placeholder = name.innerText;
  82. for(let i = 0; i < ingredientDivs.children.length; i++){
  83. let div = ingredientDivs.children[i];
  84. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  85. div.children[1].style.display = "block";
  86. div.children[1].placeholder = this.recipe.ingredients[i].quantity;
  87. div.children[3].style.display = "block";
  88. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  89. }
  90. let price = document.querySelector("#recipePrice");
  91. price.children[1].style.display = "none";
  92. price.children[2].style.display = "block";
  93. price.children[2].placeholder = price.children[1].innerText;
  94. document.querySelector("#recipeUpdate").style.display = "block";
  95. },
  96. update: function(){
  97. let updatedRecipe = {
  98. _id: this.recipe._id,
  99. name: document.querySelector("#recipeNameIn").value || this.recipe.name,
  100. price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
  101. ingredients: []
  102. }
  103. let divs = document.querySelector("#recipeIngredientList").children;
  104. for(let i = 0; i < divs.length; i++){
  105. updatedRecipe.ingredients.push({
  106. ingredient: divs[i]._id,
  107. quantity: divs[i].children[1].value || divs[i].children[1].placeholder
  108. });
  109. }
  110. if(validator.recipe(updatedRecipe)){
  111. fetch("/recipe/update", {
  112. method: "PUT",
  113. headers: {
  114. "Content-Type": "application/json;charset=utf-8"
  115. },
  116. body: JSON.stringify(updatedRecipe)
  117. })
  118. .then((response) => response.json())
  119. .then((response)=>{
  120. if(typeof(response) === "string"){
  121. banner.createError(response);
  122. }else{
  123. updateRecipes(updatedRecipe);
  124. banner.createNotification("Recipe successfully updated");
  125. }
  126. })
  127. .catch((err)=>{
  128. banner.createError("Something went wrong. Please refresh the page");
  129. })
  130. }
  131. },
  132. remove: function(){
  133. fetch(`/merchant/recipes/remove/${this.recipe._id}`, {
  134. method: "DELETE"
  135. })
  136. .then((response) => response.json())
  137. .then((response)=>{
  138. if(typeof(response) === "string"){
  139. banner.createError(response);
  140. }else{
  141. updateRecipes(this.recipe, true);
  142. banner.createNotification("Recipe removed");
  143. }
  144. })
  145. .catch((err)=>{
  146. banner.createError("Something went wrong. Try refreshing the page");
  147. });
  148. }
  149. }
  150. </script>
  151. </div>