TransactionDetails.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. export default class TransactionDetails extends Page{
  6. constructor(transaction){
  7. super("TransactionDetails", ["home", "back-viewTransactions", "logout"]);
  8. this.render(transaction);
  9. }
  10. displayCategory(category){
  11. if(category.name === "Discretionary") return category.name;
  12. return `${category.name} (${category.type})`;
  13. }
  14. transactionColor(transaction){
  15. if(transaction.rawCategory === "income"){
  16. if(transaction.amount < 0){
  17. return "red";
  18. }else{
  19. return "green";
  20. }
  21. }else{
  22. if(transaction.amount < 0){
  23. return "green";
  24. }else{
  25. return "red";
  26. }
  27. }
  28. }
  29. closeModal(){
  30. const modal = this.container.querySelector(".deleteModal");
  31. modal.parentElement.removeChild(modal);
  32. }
  33. async delete(transaction){
  34. try{
  35. await transaction.delete();
  36. }catch(e){
  37. if(e.error){
  38. new Notifier("error", e.error.message);
  39. }else{
  40. new Notifier("error", "Something went wrong, try refreshing the page");
  41. }
  42. }
  43. changePage("viewTransactions");
  44. }
  45. confirmDelete(transaction){
  46. new Elem("div")
  47. .addClass("deleteModal")
  48. .append(new Elem("p")
  49. .text("Deleting is permanent and cannot be undone, are you sure you want to delete this transaction?")
  50. )
  51. .append(new Elem("button")
  52. .text("Delete")
  53. .addClass("button")
  54. .onclick(()=>{this.delete(transaction)})
  55. )
  56. .append(new Elem("button")
  57. .text("Cancel")
  58. .addClass("button")
  59. .onclick(this.closeModal.bind(this))
  60. )
  61. .appendTo(this.container);
  62. }
  63. render(transaction){
  64. new Elem("div")
  65. .addClass("dataContainer")
  66. .append(new Elem("h1")
  67. .text("Transaction")
  68. )
  69. .append(new Elem("dl")
  70. .append(new Elem("dt")
  71. .text("Date")
  72. )
  73. .append(new Elem("dd")
  74. .text(Format.dateFromTransaction(transaction.date, true))
  75. )
  76. )
  77. .append(new Elem("dl")
  78. .append(new Elem("dt")
  79. .text("Amount")
  80. )
  81. .append(new Elem("dd")
  82. .text(Format.currency(transaction.amount))
  83. .addClass(this.transactionColor(transaction))
  84. )
  85. )
  86. .append(new Elem("dl")
  87. .append(new Elem("dt")
  88. .text("Location ")
  89. )
  90. .append(new Elem("dd")
  91. .text(transaction.location)
  92. )
  93. )
  94. .append(new Elem("dl")
  95. .append(new Elem("dt")
  96. .text("Category")
  97. )
  98. .append(new Elem("dd")
  99. .text(this.displayCategory(transaction.category))
  100. )
  101. )
  102. .append(new Elem("dl")
  103. .append(new Elem("dt")
  104. .text("Tags")
  105. )
  106. .append(new Elem("dd")
  107. .text(transaction.tags.join(", "))
  108. )
  109. )
  110. .append(new Elem("dl")
  111. .append(new Elem("dt")
  112. .text("Note")
  113. )
  114. .append(new Elem("dd")
  115. .text(transaction.note)
  116. )
  117. )
  118. .append(new Elem("button")
  119. .text("Edit")
  120. .addClass("button")
  121. )
  122. .append(new Elem("button")
  123. .text("Delete")
  124. .addClass("button")
  125. .onclick(()=>{this.confirmDelete(transaction)})
  126. )
  127. .appendTo(this.container);
  128. }
  129. }