import { Component, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { AuthService } from '@core/services/auth.service'; import { DeepLinkingService } from '@core/services/deep-linking.service'; import { PortalDeterminationService } from '@core/services/portal-determination.service'; import { SpinnerService } from '@core/services/spinner.service'; import { User } from '@core/typings/client-user.typing'; import { ClientUserService } from '@features/client-user/client-user.service'; import { PanelTypes, PasswordService, PasswordValidator, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { LogService } from '@yourcause/common/logging'; interface ResetGroup { password: string; } @Component({ selector: 'gc-reset-password', templateUrl: './reset-password.component.html', styleUrls: ['./reset-password.component.scss'] }) export class ResetPasswordComponent implements OnInit { PanelTypes = PanelTypes; user: User; formGroup: TypeSafeFormGroup; passwordVisible = false; submitDisabled = false; token: string; appId: string; formId: string; programGuid: string; inviteId: string; isExpired = false; errorOnSubmit = false; containsUserInfo = false; isPreviousPassword = false; constructor ( private logger: LogService, private portal: PortalDeterminationService, private clientUserService: ClientUserService, private formBuilder: TypeSafeFormBuilder, private router: Router, private activatedRoute: ActivatedRoute, private authService: AuthService, private spinnerService: SpinnerService, private passwordService: PasswordService, private deepLinkingService: DeepLinkingService ) { this.onSubmit = this.onSubmit.bind(this); } get isNewUser () { return this.appId || this.formId || this.inviteId; } async ngOnInit () { this.token = decodeURIComponent( this.activatedRoute.snapshot.queryParamMap.get('prt') ); this.appId = this.activatedRoute.snapshot.queryParamMap.get('appId'); this.formId = this.activatedRoute.snapshot.queryParamMap.get('formId'); this.inviteId = this.activatedRoute.snapshot.queryParamMap.get('invite'); this.programGuid = this.activatedRoute.snapshot.queryParamMap.get('programGuid'); if (this.inviteId && this.programGuid) { this.deepLinkingService.setAttemptedRouteApplicant( `/apply/programs/${this.programGuid}?invite=${this.inviteId}` ); } await this.checkIfTokenExpired(); } async checkIfTokenExpired () { const token = encodeURIComponent(this.token); try { const func = this.portal.isManager ? 'checkIfTokenExpiredManager' : this.portal.isPlatform ? 'checkIfTokenExpiredPlatform' : 'checkIfTokenExpiredApplicant'; const response = await this.authService[func](token, true); if (!response.isValid) { this.isExpired = true; } else { this.setFormGroup(); } } catch (e) { this.logger.error(e); this.isExpired = true; } } passwordChanged () { this.errorOnSubmit = false; this.containsUserInfo = false; this.isPreviousPassword = false; } setFormGroup () { this.formGroup = this.formBuilder.group({ password: [ '', [ Validators.required, PasswordValidator(this.passwordService) ] ] }); } goToResendVerification () { const routeSlice = this.portal.isManager ? 'management' : 'apply'; this.router.navigate([`/${routeSlice}/auth/resend-verification`]); } async onSubmit () { this.submitDisabled = true; const payload = { token: this.token, password: this.formGroup.value.password, confirmPassword: this.formGroup.value.password }; this.spinnerService.startSpinner(); const response = await this.clientUserService.handleResetPassword( payload, this.appId, this.formId ); this.spinnerService.stopSpinner(); this.containsUserInfo = response.containsUserInfo; this.isPreviousPassword = response.passwordPreviouslyUsed; this.errorOnSubmit = this.containsUserInfo || this.isPreviousPassword; } togglePasswordVisible = () => { this.passwordVisible = !this.passwordVisible; }; }