profile.component.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { Component, OnInit } from "@angular/core";
  2. import { Title } from "@angular/platform-browser";
  3. import { FormGroup, FormBuilder, Validators, FormArray } from "@angular/forms";
  4. import { UserService } from "@app/services/user.service";
  5. import { CatalogsService } from "@app/services/catalogs.service";
  6. import Swal from "sweetalert2";
  7. import { AuthService } from "@app/services/auth2.service";
  8. import {
  9. ValidatorComponent,
  10. PasswordStrengthValidator,
  11. } from "../plugins/validator/validator.component";
  12. @Component({
  13. selector: "app-profile",
  14. templateUrl: "./profile.component.html",
  15. styleUrls: ["./profile.component.scss"],
  16. })
  17. export class ProfileComponent implements OnInit {
  18. title = "Perfil del usuario";
  19. userForm: FormGroup;
  20. submitted: boolean = false;
  21. isLoadingOrganization: boolean;
  22. first_name: string = "Usuario";
  23. user: any;
  24. editProfile: boolean;
  25. distributor: any;
  26. codigo_tarifa: any;
  27. constructor(
  28. private titleService: Title,
  29. private userService: UserService,
  30. private catalogsService: CatalogsService,
  31. private formBuilder: FormBuilder,
  32. private authService: AuthService
  33. ) {}
  34. email: string;
  35. ngOnInit() {
  36. Swal.fire({
  37. allowOutsideClick: false,
  38. type: "info",
  39. text: "Espere por favor...",
  40. });
  41. Swal.showLoading();
  42. this.catalogsService.getCatalogByName("distribuidoras").subscribe((res) => {
  43. this.distributor = res["data"]["catalogo"]["records"];
  44. });
  45. this.catalogsService.getCatalogByName("tarifas").subscribe((res) => {
  46. this.codigo_tarifa = res["data"]["catalogo"]["records"];
  47. });
  48. this.userService
  49. .getUserById(this.authService.getUserId())
  50. .subscribe((ans) => {
  51. this.user = ans["data"]["user"];
  52. Swal.close();
  53. this.editProfile = true;
  54. this.first_name = this.user.first_name;
  55. this.userForm = this.formBuilder.group(
  56. {
  57. first_name: [this.user.first_name, Validators.required],
  58. last_name: [this.user.last_name, Validators.required],
  59. email: [this.user.email, Validators.required],
  60. phone: [
  61. this.user.phone_number == undefined ? "" : this.user.phone_number,
  62. Validators.required,
  63. ],
  64. distribuidora: [this.user.distribuidora],
  65. tarifa: [this.user.cod_tarifa],
  66. password: [
  67. "",
  68. [Validators.minLength(8), PasswordStrengthValidator],
  69. ],
  70. confirm_password: [""],
  71. },
  72. {
  73. validator: ValidatorComponent("password", "confirm_password"),
  74. }
  75. );
  76. });
  77. this.titleService.setTitle(this.title);
  78. }
  79. get f() {
  80. return this.userForm.controls;
  81. }
  82. editUser(form: any) {
  83. this.submitted = true;
  84. // stop here if form is invalid
  85. if (this.userForm.invalid) {
  86. return;
  87. }
  88. let user = {
  89. first_name: this.f.first_name.value,
  90. last_name: this.f.last_name.value,
  91. email: this.f.email.value,
  92. phone_number: this.f.phone.value,
  93. distribuidora: this.f.distribuidora.value,
  94. cod_tarifa: this.f.tarifa.value,
  95. };
  96. if (this.f.password.value != "" && this.f.confirm_password.value != "") {
  97. user["password"] = this.f.password.value;
  98. user["confirm_password"] = this.f.confirm_password.value;
  99. }
  100. Swal.fire({
  101. allowOutsideClick: false,
  102. type: "info",
  103. text: "Espere por favor...",
  104. });
  105. Swal.showLoading();
  106. this.userService.editCustomer(this.authService.getUserId(), user).subscribe(
  107. (success) => {
  108. if (success) {
  109. Swal.fire({
  110. allowOutsideClick: false,
  111. type: "success",
  112. showCancelButton: false,
  113. title: "Exito",
  114. confirmButtonText: "Se ha actualizado su perfil exitosamente",
  115. }).then((result) => {
  116. Swal.close();
  117. window.location.reload();
  118. });
  119. }
  120. },
  121. (err) => {
  122. Swal.fire({
  123. type: "error",
  124. title: "Error al guardar",
  125. text: err.message,
  126. });
  127. }
  128. );
  129. }
  130. helpModal() {
  131. Swal.fire({
  132. title: "<strong>¿Dónde encuentro mi tipo de tarifa?</strong>",
  133. type: "info",
  134. html:
  135. "<p style='text-align: left'>Si tu distribuidora de energía es del grupo de empresas AES, puedes verificar el tipo de tarifa en la siguiente imagen:</p>" +
  136. '<img alt="Factura AES" src="./assets/img/AESFactura.png" style="width: 100%;"/><br><br> ' +
  137. '<p style="text-align: left">Para más información puedes visitar el sitio oficial de <a href="http://www.aes-elsalvador.com/servicio-al-cliente/conoce-tu-factura/" target="_blank">AES</a></p>' +
  138. "<br><br>" +
  139. "<p style='text-align: left'>Si tu distribuidora de energía es DelSur, puedes verificar el tipo de tarifa en la siguiente imagen:</p>" +
  140. '<img alt="Factura AES" src="./assets/img/DelSurFactura2.png" style="width: 100%;"/>' +
  141. '<p style="text-align: left">Para más información puedes visitar el sitio oficial de <a href="https://www.delsur.com.sv/conoce-tu-factura/" target="_blank">DelSur</a></p>' +
  142. "<br><br>",
  143. showCloseButton: true,
  144. width: 600,
  145. confirmButtonText: '<i class="fa fa-thumbs-up"></i> Cerrar',
  146. confirmButtonAriaLabel: "Thumbs up, great!",
  147. });
  148. }
  149. }