User.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  20. connectedCallback() {
  21. if (!this.rendered) {
  22. this.render();
  23. }
  24. }
  25. bind() {
  26. bindEvents("UserLogin", "user-login", (e) => {
  27. this.user = e.detail;
  28. this.render();
  29. })
  30. bindEvents("UserOut", "user-logout", (e) => {
  31. this.user = null;
  32. })
  33. }
  34. async render() {
  35. if (!this.user) return;
  36. this.rendered = true;
  37. let res = await f("servicerecord", "get", this.user.user_token);
  38. console.log(res)
  39. this.registrations = res.results;
  40. for (let reg of this.registrations) {
  41. this.innerHTML += (getTemplateCab(reg))
  42. }
  43. this.attachModel();
  44. }
  45. attachModel() {
  46. this.querySelectorAll("input")
  47. .forEach(el=>el.addEventListener("input",e =>this.inputText(e)))
  48. this.querySelectorAll('.button')
  49. .forEach(el => el.addEventListener('click', e => this.clickButton(e)))
  50. }
  51. inputText(e) {
  52. if (this.data[e.target.dataset.model] !== undefined) {
  53. this.data[e.target.dataset.model] = e.target.value;
  54. }
  55. }
  56. clickButton(e) {
  57. if (this[e.target.dataset.click]) {
  58. this[e.target.dataset.click](e);
  59. }
  60. }
  61. async restore(e) {
  62. let id = e.target.dataset.id;
  63. let res = await f(`servicerecord/${id}`, "delete", this.user.user_token, null);
  64. console.log(res)
  65. let el = document.getElementById(id);
  66. el.classList.add("disabled-block");
  67. }
  68. }