ingredient.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. this.graph.addData(this.formatData(type, ingredient.id));
  47. for(let label of document.querySelector("#ingredientOptions").children){
  48. if(label.innerText === ingredient.name){
  49. label.children[0].checked = true;
  50. }else{
  51. label.children[0].checked = false;
  52. }
  53. }
  54. }
  55. },
  56. formatData: function(type, id){
  57. dataList = new Array(365).fill(0);
  58. let today = new Date();
  59. if(type === "ingredient"){
  60. let dataLastDate = new Date(data.transactions[0].date);
  61. let dateRange = Math.floor((Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()) - Date.UTC(dataLastDate.getFullYear(), dataLastDate.getMonth(), dataLastDate.getDate())) / (1000 * 60 * 60 * 24));
  62. for(let transaction of data.transactions){
  63. let transDate = new Date(transaction.date);
  64. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(today.getFullYear(), today.getMonth(), today.getDate())) / (1000 * 60 * 60 * 24));
  65. if(diff <= 0){
  66. for(let recipe of transaction.recipes){
  67. for(let merchRecipe of data.merchant.recipes){
  68. if(merchRecipe._id === recipe.recipe){
  69. for(let ingredient of merchRecipe.ingredients){
  70. if(ingredient.ingredient === id){
  71. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  72. }
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return {id: id, set: dataList};
  81. }
  82. }
  83. }