ingredientDetails.ejs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <div id="ingredientDetails">
  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. <button onclick="ingredientDetailsComp.edit()">
  10. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  11. <path d="M12 20h9"></path>
  12. <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
  13. </svg>
  14. </button>
  15. <button onclick="ingredientDetailsComp.remove()">
  16. <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  17. <polyline points="3 6 5 6 21 6"></polyline>
  18. <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>
  19. </svg>
  20. </button>
  21. </div>
  22. <p></p>
  23. <h1></h1>
  24. <div class="lineBorder"></div>
  25. <label>Current Stock
  26. <p id="ingredientStock"></p>
  27. <input id="ingredientInput" type="number" min="0" step="0.01" style="display: none;">
  28. </label>
  29. <div class="lineBorder"></div>
  30. <label>Average Daily Use (30 days)
  31. <p id="dailyUse"></p>
  32. </label>
  33. <button id="editSubmitButton" class="button" onclick="ingredientDetailsComp.editSubmit()" style="display: none;">Save Changes</button>
  34. <script>
  35. let ingredientDetailsComp = {
  36. ingredient: {},
  37. display: function(ingredient, category){
  38. this.ingredient = ingredient;
  39. sidebar = document.querySelector("#ingredientDetails");
  40. openSidebar(sidebar);
  41. document.querySelector("#ingredientDetails p").innerText = category.name;
  42. document.querySelector("#ingredientDetails h1").innerText = ingredient.name;
  43. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.unit}`;
  44. document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.unit}`;
  45. let quantities = [];
  46. let now = new Date();
  47. for(let i = 1; i < 31; i++){
  48. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  49. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  50. quantities.push(ingredientSold(dateIndices(startDay, endDay), ingredient.id));
  51. }
  52. let sum = 0;
  53. for(let quantity of quantities){
  54. sum += quantity;
  55. }
  56. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.unit}`;
  57. },
  58. remove: function(){
  59. for(let i = 0; i < merchant.recipes.length; i++){
  60. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  61. if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
  62. banner.createError("Must remove ingredient from all recipes before removing");
  63. return;
  64. }
  65. }
  66. }
  67. fetch(`/merchant/ingredients/remove/${this.ingredient.id}`, {
  68. method: "DELETE",
  69. })
  70. .then((response) => response.json())
  71. .then((response)=>{
  72. if(typeof(response) === "string"){
  73. banner.createError(response);
  74. }else{
  75. banner.createNotification("Ingredient removed");
  76. updateInventory([this.ingredient], true);
  77. }
  78. })
  79. .catch((err)=>{});
  80. },
  81. edit: function(){
  82. document.querySelector("#ingredientStock").style.display = "none";
  83. document.querySelector("#ingredientInput").style.display = "block";
  84. document.querySelector("#editSubmitButton").style.display = "block";
  85. },
  86. editSubmit: function(){
  87. let data = [{
  88. id: this.ingredient.id,
  89. quantity: Number(document.querySelector("#ingredientInput").value)
  90. }];
  91. if(validator.ingredient.quantity(data[0].quantity)){
  92. fetch("/merchant/ingredients/update", {
  93. method: "PUT",
  94. headers: {
  95. "Content-Type": "application/json;charset=utf-8"
  96. },
  97. body: JSON.stringify(data)
  98. })
  99. .then((response) => response.json())
  100. .then((response)=>{
  101. if(typeof(response) === "string"){
  102. banner.createError(response);
  103. }else{
  104. updateInventory(data);
  105. banner.createNotification("Ingredient updated");
  106. }
  107. })
  108. .catch((err)=>{
  109. banner.createError("Something went wrong, try refreshing the page");
  110. });
  111. }
  112. }
  113. }
  114. </script>
  115. </div>