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 { PortalDeterminationService } from '@core/services/portal-determination.service'; import { EmailExtensionValidator, PanelTypes, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; @Component({ selector: 'gc-resend-verification', templateUrl: './resend-verification.component.html', styleUrls: ['./resend-verification.component.scss'] }) export class ResendVerificationComponent implements OnInit { PanelTypes = PanelTypes; resendVerificationLinkForm: TypeSafeFormGroup<{ email: string }>; programGuid: string; constructor ( private logger: LogService, private router: Router, private activatedRoute: ActivatedRoute, private authService: AuthService, private portal: PortalDeterminationService, private notifier: NotifierService, private i18n: I18nService, fb: TypeSafeFormBuilder ) { this.resendVerificationLinkForm = fb.group({ email: ['', [ Validators.required, EmailExtensionValidator ]] }); this.resendEmail = this.resendEmail.bind(this); } get isManager () { return this.portal.isManager; } get isPlatform () { return this.portal.isPlatform; } ngOnInit () { this.programGuid = this.activatedRoute.snapshot.queryParamMap.get('grantProgramGuid'); } goToLogin () { this.router.navigate(['../'], { relativeTo: this.activatedRoute }); } notifySuccessEmailSent () { return this.notifier.success(this.i18n.translate( 'AUTH:emailVerificationLinkSent', {}, 'A verification link has been sent to ' ) + this.resendVerificationLinkForm.value.email); } throwVerificationError (e: any) { if (e.error && e.error.message === 'Email does not exist.') { this.notifier.error(this.i18n.translate( 'AUTH:textEmailDoesNotExist', {}, 'Email does not exist.' )); } else { this.logger.error(e); this.notifier.error(this.i18n.translate( 'AUTH:textErrorResendingVerification', {}, 'There was an error sending the verification link' )); throw e; } } async resendEmail () { if (this.isManager) { try { await this.authService.resendManagerVerification( this.resendVerificationLinkForm.value.email ); this.notifySuccessEmailSent(); } catch (e) { this.throwVerificationError(e); } } else if (this.isPlatform) { try { await this.authService.resendPlatformVerification( this.resendVerificationLinkForm.value.email ); this.notifySuccessEmailSent(); } catch (e) { this.throwVerificationError(e); } } else { try { await this.authService.resendApplicantVerification( this.resendVerificationLinkForm.value.email, this.programGuid ); this.notifySuccessEmailSent(); } catch (e) { this.throwVerificationError(e); } } } }