All files / app/pages/new-password-page new-password-page.component.ts

59.25% Statements 48/81
100% Branches 3/3
66.66% Functions 2/3
59.25% Lines 48/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 821x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x                                                                   1x  
import { isPlatformBrowser, NgClass } from '@angular/common';
import { Component, inject, OnInit, PLATFORM_ID } from '@angular/core';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { HeaderElementComponent } from '@elements/header-element/header-element.component';
import { ValidationErrorsComponent } from '@elements/validation-errors/validation-errors.component';
import { NewPasswordForm } from '@forms/newpassword.form';
import { AnalyticsIntegrationService } from '@services/analyticsIntegration.service';
import { NetworkService } from '@services/network.service';
import Swal from 'sweetalert2';
 
@Component({
	selector: 'grt-new-password-page',
	templateUrl: './new-password-page.component.html',
	styleUrls: ['./new-password-page.component.scss'],
	imports: [HeaderElementComponent, ReactiveFormsModule, NgClass, ValidationErrorsComponent],
})
export class NewPasswordPageComponent implements OnInit {
	private network = inject(NetworkService);
	private route = inject(ActivatedRoute);
	private analyticsIntegrationService = inject(AnalyticsIntegrationService);
	protected router = inject(Router);
	formBuilder = inject(FormBuilder);
 
	form: NewPasswordForm;
	token: any;
	isBrowser: any;
 
	constructor() {
		const platformId = inject(PLATFORM_ID);
 
		this.form = new NewPasswordForm(this.formBuilder);
		this.isBrowser = isPlatformBrowser(platformId);
	}
 
	ngOnInit() {
		if (this.isBrowser) {
			this.analyticsIntegrationService.loadScript();
		}
		this.network.getIpCliente().then(() => {});
 
		this.route.queryParams.subscribe((params) => {
			this.token = params['reset_password_token'];
		});
	}
 
	changePassword(): void {
		if (this.form.validate()) {
			this.network
				.changePassword(this.form.formData.value, this.token)
				.then(() => {
					if (this.isBrowser) {
						Swal.fire({
							toast: true,
							position: 'top-end',
							showConfirmButton: false,
							showCloseButton: true,
							timerProgressBar: true,
							didOpen: (toast) => {
								toast.onmouseenter = Swal.stopTimer;
								toast.onmouseleave = Swal.resumeTimer;
							},
							icon: 'success',
							text: 'Password was changed and You have been successfully logged in.',
							background: '#51a351',
							color: '#fff',
							width: '28em',
							timer: 5000,
						});
					}
				})
				.catch((error) => {
					if (typeof error === 'string') {
						alert(error);
					} else {
						this.form.setErrors(error);
					}
				});
		}
	}
}