EditAllowance.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import Page from "./Page.js";
  2. import Elem from "../Elem.js";
  3. export default class EditAllowance extends Page{
  4. constructor(allowance){
  5. super("EditAllowance", ["home", "back-viewAllowances", "logout"]);
  6. this.incomeTotal = user.account.incomeTotal();
  7. this.render(allowance);
  8. }
  9. submit(allowance){
  10. event.preventDefault();
  11. console.log("submitting");
  12. }
  13. updateAmount(allowance){
  14. let text, titleText, step;
  15. if(event.target.checked){
  16. text = "Amount (%)";
  17. titleText = "(Percent of Income)";
  18. step = "1";
  19. }else{
  20. text = "Amount ($)";
  21. titleText = "(Fixed Amount)";
  22. step = "0.01";
  23. }
  24. new Elem(this.container.querySelector("h2"))
  25. .text(titleText);
  26. new Elem(this.container.querySelector(".amountLabel"))
  27. .clear()
  28. .append(new Elem("p")
  29. .text(text)
  30. )
  31. .append(new Elem("input")
  32. .type("number")
  33. .addClass("amount")
  34. .value(allowance.displayAmount(this.incomeTotal))
  35. .min("0")
  36. .step(step)
  37. .required()
  38. );
  39. }
  40. archive(allowance){
  41. console.log("archiving");
  42. }
  43. render(allowance){
  44. new Elem("form")
  45. .addClass("standardForm")
  46. .onsubmit(()=>{this.submit(allowance)})
  47. .append(new Elem("h1")
  48. .text("Edit Allowance")
  49. )
  50. .append(new Elem("h2")
  51. .text(allowance.isPercent ? "(Percent of Income)" : "(Fixed Amount)")
  52. )
  53. .append(new Elem("label")
  54. .append(new Elem("p")
  55. .text("Name")
  56. )
  57. .append(new Elem("input")
  58. .type("text")
  59. .addClass("name")
  60. .value(allowance.name)
  61. .required()
  62. )
  63. )
  64. .append(new Elem("label")
  65. .addClass("switch")
  66. .append(new Elem("input")
  67. .type("checkbox")
  68. .addClass("isPercent")
  69. .checked(allowance.isPercent)
  70. .onchange(()=>{this.updateAmount(allowance)})
  71. )
  72. .append(new Elem("span")
  73. .addClass("slider")
  74. )
  75. )
  76. .append(new Elem("label")
  77. .addClass("amountLabel")
  78. .append(new Elem("p")
  79. .text(allowance.isPercent ? "Amount (%)" : "Amount ($)")
  80. )
  81. .append(new Elem("input")
  82. .type("number")
  83. .addClass("amount")
  84. .value(allowance.displayAmount(this.incomeTotal))
  85. .min("0")
  86. .step(allowance.isPercent ? "1" : "0.01")
  87. .required()
  88. )
  89. )
  90. .append(new Elem("button")
  91. .text("Update")
  92. )
  93. .append(new Elem("button")
  94. .text(allowance.active ? "Archive" : "Restore")
  95. .type("button")
  96. .onclick(()=>{this.archive(allowance)})
  97. )
  98. .appendTo(this.container);
  99. }
  100. }