import { Component, Input, OnInit } from '@angular/core'; import { ApplicantService } from '@core/services/auth-user/applicant.service'; import { ApplicantAdminUser } from '@core/typings/applicant.typing'; import { GrantManagerUser } from '@core/typings/grant-manager.typing'; import { GrantManagerService } from '@features/platform-admin/grant-managers/grant-manager.service'; import { TypeaheadSelectOption, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; import { NotifierService } from '@yourcause/common/notifier'; interface EmailLinkFormGroup { email: string; emailText: string; } @Component({ selector: 'gc-applicant-email-link-modal', templateUrl: './applicant-email-link-modal.component.html', styleUrls: ['./applicant-email-link-modal.component.scss'] }) export class ApplicantEmailLinkModalComponent extends YCModalComponent implements OnInit { @Input() user: ApplicantAdminUser | GrantManagerUser; @Input() modalType: string; formGroup: TypeSafeFormGroup; emailOptions: TypeaheadSelectOption[]; constructor ( private applicantService: ApplicantService, private grantManagerService: GrantManagerService, private formBuilder: TypeSafeFormBuilder, private notifier: NotifierService, private i18n: I18nService, private analyticsService: AnalyticsService ) { super(); } ngOnInit () { this.emailOptions = [{ label: this.i18n.translate( 'ADMIN:textPasswordReset', {}, 'Password reset' ), value: 'Reset Password Token Applicant' }]; this.formGroup = this.formBuilder.group({ email: 'Reset Password Token Applicant', emailText: '' }); this.createEmailText(); } async createEmailText () { const notificationType = this.formGroup.value.email; try { let response = null; if (this.modalType === 'applicant') { response = await this.applicantService.getEmailTextForApplicant( this.user.email, notificationType ); } else { response = await this.grantManagerService.passwordResetForGrantManager( this.user.email, notificationType ); } this.formGroup.get('emailText').setValue(response); } catch (error) { this.notifier.error(this.i18n.translate( 'ADMIN:errorLink', {}, 'There was an error getting the link' )); } } cancel () { this.closeModal.emit(); } submit () { this.closeModal.emit(this.formGroup.value.emailText); this.analyticsService.emitEvent({ eventName: 'Generate email link platform tool', eventType: EventType.Click, extras: null }); } }