CreateAllowance.js 2.9 KB

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