recipe.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. window.recipeObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(recipe){
  5. clearScreen();
  6. document.querySelector("#recipeStrand").style.display = "flex";
  7. document.querySelector("strand-selector").setAttribute("strand", "recipe");
  8. if(!this.isPopulated){
  9. let date = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  10. document.querySelector("#recipeFrom").valueAsDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12);
  11. document.querySelector("#recipeTo").valueAsDate = new Date();
  12. let recipesDiv = document.querySelector("#recipeOptions");
  13. for(let recipe of data.merchant.recipes){
  14. let checkDiv = document.createElement("div");
  15. checkDiv.classList = "checkboxDiv";
  16. recipesDiv.appendChild(checkDiv);
  17. let checkbox = document.createElement("input");
  18. checkbox.type = "checkbox";
  19. checkbox.id = `${recipe.name}Checkbox`;
  20. checkbox._id = recipe._id;
  21. checkbox.name = recipe.name;
  22. checkbox.onchange = ()=>{
  23. if(checkbox.checked){
  24. let from = document.querySelector("#recipeFrom").valueAsDate;
  25. let to = document.querySelector("#recipeTo").valueAsDate;
  26. this.graph.addData(this.formatData(recipe._id, from, to), [from, to], recipe.name);
  27. }else{
  28. this.graph.removeData(recipe.name);
  29. }
  30. };
  31. checkDiv.appendChild(checkbox);
  32. let label = document.createElement("label");
  33. label.innerText = recipe.name;
  34. label.setAttribute("for", `${recipe.name}Checkbox`);
  35. checkDiv.appendChild(label);
  36. }
  37. this.graph = new LineGraph(
  38. document.querySelector("#recipeStrand canvas"),
  39. "Quantity",
  40. "Date"
  41. );
  42. this.isPopulated = true;
  43. }
  44. if(recipe){
  45. this.graph.clearData();
  46. let from = document.querySelector("#recipeFrom").valueAsDate;
  47. let to = document.querySelector("#recipeTo").valueAsDate;
  48. this.graph.addData(this.formatData(recipe.id, from, to), [from, to], recipe.name);
  49. for(let label of document.querySelector("#recipeOptions").children){
  50. if(label.innerText === ingredient.name){
  51. label.children[0].checked = true;
  52. }else{
  53. label.children[0].checked = false;
  54. }
  55. }
  56. }
  57. },
  58. formatData: function(id, from, to){
  59. let dateRange = Math.floor((Date.UTC(to.getFullYear(), to.getMonth(), to.getDate()) - Date.UTC(from.getFullYear(), from.getMonth(), from.getDate())) / (1000 * 60 * 60 * 24)) + 1;
  60. let dataList = new Array(Math.abs(dateRange)).fill(0);
  61. for(let transaction of data.transactions){
  62. let transDate = new Date(transaction.date);
  63. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(to.getFullYear(), to.getMonth(), to.getDate())) / (1000 * 60 * 60 * 24));
  64. if(diff <= 0){
  65. for(let recipe of transaction.recipes){
  66. if(recipe.recipe === id){
  67. dataList[dateRange - Math.abs(diff) - 1] += recipe.quantity;
  68. }
  69. }
  70. }
  71. }
  72. return dataList;
  73. },
  74. newDates: function(){
  75. let from = document.querySelector("#recipeFrom").value;
  76. let to = document.querySelector("#recipeTo").value;
  77. if(from === "" || to === ""){
  78. banner.createError("Invalid date");
  79. return;
  80. }else{
  81. from = new Date(from);
  82. to = new Date(to);
  83. from.setMinutes(from.getMinutes() + from.getTimezoneOffset());
  84. to.setMinutes(to.getMinutes() + to.getTimezoneOffset());
  85. }
  86. if(validator.transaction.date(from, to)){
  87. window.fetchData(from, to, ()=>{
  88. this.graph.clearData();
  89. let recipesDiv = document.querySelector("#recipeOptions");
  90. for(let div of recipesDiv.children){
  91. let checkbox = div.children[0];
  92. if(checkbox.checked){
  93. this.graph.addData(this.formatData(checkbox._id, from, to), [from, to], checkbox.name);
  94. }
  95. }
  96. })
  97. }
  98. }
  99. }