recipe.js 4.4 KB

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