Home.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Page from "./Page.js";
  2. import Elem from "../Elem.js";
  3. import Format from "../Format.js";
  4. import Notifier from "../Notifier.js";
  5. import Transaction from "../data/Transaction.js";
  6. export default class Home extends Page{
  7. constructor(){
  8. super("Home");
  9. const date = new Date();
  10. const month = date.toLocaleString("en-US", {month: "long"});
  11. let logoutSvg = '<svg width="24px" height="24px" stroke-width="2.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M12 12H19M19 12L16 15M19 12L16 9" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M19 6V5C19 3.89543 18.1046 3 17 3H7C5.89543 3 5 3.89543 5 5V19C5 20.1046 5.89543 21 7 21H17C18.1046 21 19 20.1046 19 19V18" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  12. let viewSvg = '<svg width="24px" height="24px" viewBox="0 0 24 24" stroke-width="2.5" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M3 13C6.6 5 17.4 5 21 13" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M12 17C10.3431 17 9 15.6569 9 14C9 12.3431 10.3431 11 12 11C13.6569 11 15 12.3431 15 14C15 15.6569 13.6569 17 12 17Z" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  13. if(!user.account.isPopulated){
  14. let from = new Date();
  15. from.setDate(1);
  16. from = Format.transactionDate(from);
  17. let to = new Date();
  18. to = Format.transactionDate(to);
  19. Transaction.fetch(user.account.id, from, to)
  20. .then((transactions)=>{
  21. user.account.addManyTransactions(transactions);
  22. user.account.isPopulated = true;
  23. })
  24. .catch((err)=>{
  25. new Notifier("error", "Unable to retrieve transactions");
  26. });
  27. }
  28. this.render(month, logoutSvg, viewSvg);
  29. }
  30. async logout(){
  31. let response;
  32. try{
  33. response = await fetch("/api/user/logout", {
  34. method: "GET",
  35. headers: {"Content-Type": "application/json"}
  36. });
  37. }catch(e){
  38. new Notifier("error", "Something went wrong, try refreshing the page");
  39. }
  40. if(!response.ok) return new Notifier("error", response.error.message);
  41. changePage("login");
  42. }
  43. render(month, logoutSvg, viewSvg){
  44. new Elem("div")
  45. .addClass("buttonBox")
  46. .append(new Elem("button")
  47. .text("+")
  48. .addClass("circleButton")
  49. .onclick(()=>{changePage("addMenu")})
  50. )
  51. .append(new Elem("button")
  52. .innerHtml(viewSvg)
  53. .addClass("circleButton")
  54. .onclick(()=>{changePage("viewMenu")})
  55. )
  56. .append(new Elem("button")
  57. .innerHtml(logoutSvg)
  58. .addClass("circleButton")
  59. .onclick(this.logout)
  60. )
  61. .appendTo(this.container);
  62. new Elem("h1")
  63. .text(month)
  64. .appendTo(this.container);
  65. new Elem("dl")
  66. .append(new Elem("dt")
  67. .text("Balance: ")
  68. )
  69. .append(new Elem("dd")
  70. .text(Format.currency(user.account.balance))
  71. )
  72. .appendTo(this.container);
  73. new Elem("dl")
  74. .append(new Elem("dt")
  75. .text("Income: ")
  76. )
  77. .append(new Elem("dd")
  78. .text(Format.currency(user.account.incomeTotal()))
  79. )
  80. .appendTo(this.container);
  81. new Elem("dl")
  82. .append(new Elem("dt")
  83. .text("Bills: ")
  84. )
  85. .append(new Elem("dd")
  86. .text(Format.currency(user.account.billsTotal()))
  87. )
  88. .appendTo(this.container);
  89. }
  90. }