ingredients.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. window.ingredientsStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. let categories = this.categorizeIngredients();
  6. let ingredientStrand = document.querySelector("#ingredientsStrand");
  7. for(let category of categories){
  8. let categoryDiv = document.createElement("div");
  9. categoryDiv.classList = "categoryDiv"
  10. ingredientStrand.appendChild(categoryDiv);
  11. let headerDiv = document.createElement("div");
  12. categoryDiv.appendChild(headerDiv);
  13. let headerTitle = document.createElement("p");
  14. headerTitle.innerText = category.name;
  15. headerDiv.appendChild(headerTitle);
  16. let ingredientsDiv = document.createElement("div");
  17. ingredientsDiv.classList = "ingredientsDiv";
  18. ingredientsDiv.style.display = "none";
  19. categoryDiv.appendChild(ingredientsDiv);
  20. let headerButton = document.createElement("button");
  21. headerButton.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  22. headerButton.onclick = ()=>{this.toggleCategory(ingredientsDiv, headerButton)};
  23. headerDiv.appendChild(headerButton);
  24. for(let ingredient of category.ingredients){
  25. let ingredientDiv = document.createElement("div");
  26. ingredientDiv.classList = "ingredient";
  27. ingredientDiv.onclick = ()=>{this.displayIngredient(ingredient, category)};
  28. ingredientsDiv.appendChild(ingredientDiv);
  29. let ingredientName = document.createElement("p");
  30. ingredientName.innerText = ingredient.name;
  31. ingredientDiv.appendChild(ingredientName);
  32. let spacer = document.createElement("p");
  33. spacer.innerText = "-".repeat(25);
  34. ingredientDiv.appendChild(spacer);
  35. let ingredientData = document.createElement("p");
  36. ingredientData.innerText = `${ingredient.quantity} ${ingredient.unit}`;
  37. ingredientDiv.appendChild(ingredientData);
  38. }
  39. }
  40. this.isPopulated = true;
  41. }
  42. },
  43. categorizeIngredients: function(){
  44. let ingredientsByCategory = [];
  45. for(let item of merchant.inventory){
  46. let categoryExists = false;
  47. for(let category of ingredientsByCategory){
  48. if(item.ingredient.category === category.name){
  49. category.ingredients.push({
  50. id: item.ingredient._id,
  51. name: item.ingredient.name,
  52. quantity: item.quantity,
  53. unit: item.ingredient.unit
  54. });
  55. categoryExists = true;
  56. break;
  57. }
  58. }
  59. if(!categoryExists){
  60. ingredientsByCategory.push({
  61. name: item.ingredient.category,
  62. ingredients: [{
  63. id: item.ingredient._id,
  64. name: item.ingredient.name,
  65. quantity: item.quantity,
  66. unit: item.ingredient.unit
  67. }]
  68. });
  69. }
  70. }
  71. return ingredientsByCategory;
  72. },
  73. toggleCategory: function(div, button){
  74. if(div.style.display === "none"){
  75. button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
  76. div.style.display = "flex";
  77. }else if(div.style.display === "flex"){
  78. button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  79. div.style.display = "none";
  80. }
  81. },
  82. toggleAddIngredient: function(){
  83. let sidebar = document.querySelector("#addIngredient");
  84. if(sidebar.classList.value === "sidebarHide"){
  85. sidebar.classList = "sidebar";
  86. }else{
  87. sidebar.classList = "sidebarHide";
  88. }
  89. },
  90. displayIngredient: function(ingredient, category){
  91. sidebar = document.querySelector("#ingredientDetails");
  92. sidebar.classList = "sidebar";
  93. document.querySelector("#ingredientDetails p").innerText = category.name;
  94. document.querySelector("#ingredientDetails h2").innerText = ingredient.name;
  95. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.unit}`;
  96. let start = performance.now();
  97. let quantities = [];
  98. let now = new Date();
  99. for(let i = 1; i < 31; i++){
  100. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  101. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  102. quantities.push(ingredientSold(dateIndices(startDay, endDay), ingredient.id));
  103. }
  104. let sum = 0;
  105. for(let quantity of quantities){
  106. sum += quantity;
  107. }
  108. let end = performance.now();
  109. console.log(`${end-start} ms`);
  110. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.unit}`;
  111. }
  112. }