ingredient.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. window.ingredientObj = {
  2. display: function(type, ingredient){
  3. clearScreen();
  4. document.querySelector("#ingredientStrand").style.display = "flex";
  5. document.querySelector("strand-selector").setAttribute("strand", "ingredient");
  6. if(ingredient){
  7. document.querySelector("#ingredientStrand h1").innerText = `${ingredient.name} (${ingredient.unit})`;
  8. graph.line(document.querySelector("#ingredientStrand canvas"), this.formatData(type, ingredient.id));
  9. }
  10. },
  11. formatData: function(type, id){
  12. dataList = new Array(365).fill(0);
  13. let today = new Date();
  14. if(type === "ingredient"){
  15. for(let transaction of data.transactions){
  16. let transDate = new Date(transaction.date);
  17. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(today.getFullYear(), today.getMonth(), today.getDate())) / (1000 * 60 * 60 * 24));
  18. if(diff <= 0){
  19. for(let recipe of transaction.recipes){
  20. for(let merchRecipe of data.merchant.recipes){
  21. if(merchRecipe._id === recipe.recipe){
  22. for(let ingredient of merchRecipe.ingredients){
  23. if(ingredient.ingredient === id){
  24. dataList[Math.abs(diff)] += ingredient.quantity;
  25. }
  26. }
  27. break;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. return dataList;
  34. }
  35. }
  36. }