recipeDetails.ejs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <div id="recipeDetails">
  2. <div class="sidebarIconButtons">
  3. <button 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 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 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. </div>
  40. </template>
  41. <script>
  42. let recipeDetailsComp = {
  43. recipe: {},
  44. display: function(recipe){
  45. this.recipe = recipe;
  46. openSidebar(document.querySelector("#recipeDetails"));
  47. document.querySelector("#recipeName").style.display = "block";
  48. document.querySelector("#recipeNameIn").style.display = "none";
  49. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  50. let ingredientList = document.querySelector("#recipeIngredientList");
  51. while(ingredientList.children.length > 0){
  52. ingredientList.removeChild(ingredientList.firstChild);
  53. }
  54. let template = document.querySelector("#recipeIngredient").content.children[0];
  55. for(let i = 0; i < recipe.ingredients.length; i++){
  56. ingredientDiv = template.cloneNode(true);
  57. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  58. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  59. ingredientDiv._id = recipe.ingredients[i].ingredient._id;
  60. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  61. ingredientList.appendChild(ingredientDiv);
  62. }
  63. let price = document.querySelector("#recipePrice");
  64. price.children[1].style.display = "block";
  65. price.children[2].style.display = "none";
  66. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  67. document.querySelector("#recipeUpdate").style.display = "none";
  68. },
  69. edit: function(){
  70. let ingredientDivs = document.querySelector("#recipeIngredientList");
  71. let name = document.querySelector("#recipeName");
  72. let nameIn = document.querySelector("#recipeNameIn");
  73. name.style.display = "none";
  74. nameIn.style.display = "block";
  75. nameIn.placeholder = name.innerText;
  76. for(let i = 0; i < ingredientDivs.children.length; i++){
  77. let div = ingredientDivs.children[i];
  78. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  79. div.children[1].style.display = "block";
  80. div.children[1].placeholder = this.recipe.ingredients[i].quantity;
  81. }
  82. let price = document.querySelector("#recipePrice");
  83. price.children[1].style.display = "none";
  84. price.children[2].style.display = "block";
  85. price.children[2].placeholder = price.children[1].innerText;
  86. document.querySelector("#recipeUpdate").style.display = "block";
  87. },
  88. update: function(){
  89. let updatedRecipe = {
  90. _id: this.recipe._id,
  91. name: document.querySelector("#recipeNameIn").value || this.recipe.name,
  92. price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
  93. ingredients: []
  94. }
  95. let divs = document.querySelector("#recipeIngredientList").children;
  96. for(let i = 0; i < divs.length; i++){
  97. updatedRecipe.ingredients.push({
  98. ingredient: divs[i]._id,
  99. quantity: divs[i].children[1].value || divs[i].children[1].placeholder
  100. });
  101. }
  102. if(validator.recipe(updatedRecipe)){
  103. fetch("/recipe/update", {
  104. method: "PUT",
  105. headers: {
  106. "Content-Type": "application/json;charset=utf-8"
  107. },
  108. body: JSON.stringify(updatedRecipe)
  109. })
  110. .then((response) => response.json())
  111. .then((response)=>{
  112. if(typeof(response) === "string"){
  113. banner.createError(response);
  114. }else{
  115. updateRecipes(updatedRecipe);
  116. banner.createNotification("Recipe successfully updated");
  117. }
  118. })
  119. .catch((err)=>{
  120. banner.createError("Something went wrong. Please refresh the page");
  121. })
  122. }
  123. },
  124. remove: function(){
  125. fetch(`/merchant/recipes/remove/${this.recipe._id}`, {
  126. method: "DELETE"
  127. })
  128. .then((response) => response.json())
  129. .then((response)=>{
  130. if(typeof(response) === "string"){
  131. banner.createError(response);
  132. }else{
  133. updateRecipes(this.recipe, true);
  134. banner.createNotification("Recipe removed");
  135. }
  136. })
  137. .catch((err)=>{
  138. banner.createError("Something went wrong. Try refreshing the page");
  139. });
  140. }
  141. }
  142. </script>
  143. </div>