User.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {getTemplateCab} from "./Templates.js";
  2. import {getTemplateRegistration} from "./Templates.js";
  3. import {bindEvents, dEvent} from "../libs/helper.js";
  4. import {f} from "../libs/helper.js"
  5. export default class User extends HTMLElement {
  6. constructor() {
  7. super();
  8. this.user = null;
  9. this.registrations = null;
  10. this.bind();
  11. this.rendered = false;
  12. this.data = {
  13. name:"",
  14. price:"",
  15. description:"",
  16. position:"",
  17. }
  18. this.service = null;
  19. this.render();
  20. }
  21. connectedCallback() {
  22. if (!this.rendered && this.user) {
  23. this.render();
  24. }
  25. }
  26. bind() {
  27. bindEvents("UserLogin", "user-login", (e) => {
  28. this.user = e.detail;
  29. this.render();
  30. })
  31. bindEvents("UserOut", "user-logout", (e) => {
  32. this.user = null;
  33. this.innerHTML = "";
  34. })
  35. bindEvents("UserCab", "user-cab", (e) => {
  36. this.getUserCab();
  37. })
  38. }
  39. async getUserCab() {
  40. this.innerHTML = "";
  41. let res = await f("servicerecord", "get", this.user.user_token);
  42. console.log(res)
  43. this.registrations = res.results;
  44. for (let reg of this.registrations) {
  45. this.innerHTML += (getTemplateCab(reg))
  46. }
  47. this.attachModel();
  48. }
  49. async render() {
  50. this.innerHTML = "";
  51. if (!this.user) return;
  52. this.rendered = true;
  53. let res = await f("servicerecord", "get", this.user.user_token);
  54. console.log(res)
  55. this.registrations = res.results;
  56. for (let reg of this.registrations) {
  57. this.innerHTML += (getTemplateCab(reg))
  58. }
  59. this.attachModel();
  60. }
  61. attachModel() {
  62. this.querySelectorAll("input")
  63. .forEach(el=>el.addEventListener("input",e =>this.inputText(e)))
  64. this.querySelectorAll('.button')
  65. .forEach(el => el.addEventListener('click', e => this.clickButton(e)))
  66. }
  67. inputText(e) {
  68. if (this.data[e.target.dataset.model] !== undefined) {
  69. this.data[e.target.dataset.model] = e.target.value;
  70. }
  71. }
  72. clickButton(e) {
  73. if (this[e.target.dataset.click]) {
  74. this[e.target.dataset.click](e);
  75. }
  76. }
  77. async restore(e) {
  78. let id = e.target.dataset.id;
  79. let res = await f(`servicerecord/${id}`, "delete", this.user.user_token, null);
  80. console.log(res)
  81. let el = document.getElementById(id);
  82. el.classList.add("disabled-block");
  83. }
  84. }