recipeDetails.ejs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. <p></p>
  38. <input type="number" min="0" step="0.01" style="display: none;">
  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[1].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  59. ingredientList.appendChild(ingredientDiv);
  60. }
  61. let price = document.querySelector("#recipePrice");
  62. price.children[1].style.display = "block";
  63. price.children[2].style.display = "none";
  64. document.querySelector("#recipePrice p").innerText = `$${(recipe.price / 100).toFixed(2)}`;
  65. document.querySelector("#recipeUpdate").style.display = "none";
  66. },
  67. edit: function(){
  68. let ingredientDivs = document.querySelector("#recipeIngredientList");
  69. let name = document.querySelector("#recipeName");
  70. let nameIn = document.querySelector("#recipeNameIn");
  71. name.style.display = "none";
  72. nameIn.style.display = "block";
  73. nameIn.placeholder = name.innerText;
  74. for(let i = 0; i < ingredientDivs.children.length; i++){
  75. let div = ingredientDivs.children[i];
  76. div.children[1].style.display = "none";
  77. div.children[2].style.display = "block";
  78. div.children[2].placeholder = div.children[1].innerText;
  79. }
  80. let price = document.querySelector("#recipePrice");
  81. price.children[1].style.display = "none";
  82. price.children[2].style.display = "block";
  83. price.children[2].placeholder = price.children[1].innerText;
  84. document.querySelector("#recipeUpdate").style.display = "block";
  85. },
  86. remove: function(){
  87. fetch(`/merchant/recipes/remove/${this.recipe._id}`, {
  88. method: "DELETE"
  89. })
  90. .then((response) => response.json())
  91. .then((response)=>{
  92. if(typeof(response) === "string"){
  93. banner.createError(response);
  94. }else{
  95. updateRecipes(this.recipe, true);
  96. banner.createNotification("Recipe removed");
  97. }
  98. })
  99. .catch((err)=>{
  100. banner.createError("Something went wrong. Try refreshing the page");
  101. });
  102. }
  103. }
  104. </script>
  105. </div>