ingredient.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. window.ingredientObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(type, ingredient){
  5. clearScreen();
  6. document.querySelector("#ingredientStrand").style.display = "flex";
  7. document.querySelector("strand-selector").setAttribute("strand", "ingredient");
  8. if(!this.isPopulated){
  9. let ingredientsDiv = document.querySelector("#ingredientOptions");
  10. for(let item of data.merchant.inventory){
  11. let checkDiv = document.createElement("div");
  12. checkDiv.classList = "checkboxDiv";
  13. ingredientsDiv.appendChild(checkDiv);
  14. let checkbox = document.createElement("input");
  15. checkbox.type = "checkbox";
  16. checkbox.id = `${item.ingredient.name}Checkbox`;
  17. checkbox.onchange = ()=>{
  18. if(checkbox.checked){
  19. this.graph.addData(this.formatData("ingredient", item.ingredient._id));
  20. }else{
  21. this.graph.removeData(item.ingredient._id);
  22. }
  23. };
  24. checkDiv.appendChild(checkbox);
  25. let label = document.createElement("label");
  26. label.innerText = item.ingredient.name;
  27. label.setAttribute("for", `${item.ingredient.name}Checkbox`);
  28. checkDiv.appendChild(label);
  29. }
  30. let startDate = new Date();
  31. startDate.setFullYear(new Date().getFullYear() - 1);
  32. this.graph = new LineGraph(
  33. document.querySelector("#ingredientStrand canvas"),
  34. "Quantity",
  35. "Date",
  36. {
  37. type: "date",
  38. start: startDate,
  39. end: new Date()
  40. }
  41. );
  42. this.isPopulated = true;
  43. }
  44. if(ingredient){
  45. this.graph.clear();
  46. let d = new Date();
  47. d.setDate(d.getDate() - 20);
  48. this.graph.addData(this.formatData(type, ingredient.id, d, new Date()));
  49. for(let label of document.querySelector("#ingredientOptions").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(type, id, startDate, endDate){
  59. let dataList;
  60. if(type === "ingredient"){
  61. let dateRange = Math.floor((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()) - Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (1000 * 60 * 60 * 24));
  62. dataList = new Array(Math.abs(dateRange)).fill(0);
  63. for(let transaction of data.transactions){
  64. let transDate = new Date(transaction.date);
  65. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) / (1000 * 60 * 60 * 24));
  66. if(diff <= 0){
  67. for(let recipe of transaction.recipes){
  68. for(let merchRecipe of data.merchant.recipes){
  69. if(merchRecipe._id === recipe.recipe){
  70. for(let ingredient of merchRecipe.ingredients){
  71. if(ingredient.ingredient === id){
  72. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  73. }
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. return {id: id, set: dataList};
  82. }
  83. }
  84. }