import { HttpErrorResponse } from '@angular/common/http'; import { Component, Input, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { ApplicantService } from '@core/services/auth-user/applicant.service'; import { PortalDeterminationService } from '@core/services/portal-determination.service'; import { TokenService } from '@core/services/token/token.service'; import { Applicant } from '@core/typings/applicant.typing'; import { User } from '@core/typings/client-user.typing'; import { ClientUserService } from '@features/client-user/client-user.service'; import { TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { YCModalComponent } from '@yourcause/common/modals'; import { NotifierService } from '@yourcause/common/notifier'; @Component({ selector: 'yc-delete-account-modal', templateUrl: './delete-account-modal.component.html', styleUrls: ['./delete-account-modal.component.scss'] }) export class DeleteAccountModalComponent extends YCModalComponent implements OnInit { @Input() user: Applicant|User; formGroup: TypeSafeFormGroup<{ password: string }>; passwordVisible = false; email: string; isUserSSO = false; constructor ( private logger: LogService, private formBuilder: TypeSafeFormBuilder, private determinationService: PortalDeterminationService, private tokenService: TokenService, private notifier: NotifierService, private i18n: I18nService, private clientUserService: ClientUserService, private applicantService: ApplicantService, private analyticsService: AnalyticsService ) { super(); } get isManager () { return this.determinationService.isManager; } get service () { return this.isManager ? this.clientUserService : this.applicantService; } get tokenCall () { return this.isManager ? 'getTokenForManager' : 'getTokenForPortal'; } ngOnInit () { this.email = this.user.email; this.isUserSSO = this.user.isSso; this.formGroup = this.formBuilder.group({ password: ['', Validators.required] }); } togglePasswordVisible = () => { this.passwordVisible = !this.passwordVisible; }; async deleteMyAccount () { if (!this.isUserSSO) { try { await this.tokenService.login(this.email, this.formGroup.value.password); } catch (err) { const e = err as HttpErrorResponse; this.logger.error(e); if (e?.error?.message === 'Incorrect email or password') { this.notifier.error(this.i18n.translate( 'ACCOUNT:textPasswordIncorrect', {}, 'Incorrect password' )); throw e; } } } try { await this.service.deactivateAccount(); this.closeModal.emit(true); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'ACCOUNT:textErrorDeletingAccount', {}, 'There was an error deleting your account' )); throw e; } this.analyticsService.emitEvent({ eventName: 'Delete account submit', eventType: EventType.Click, extras: null }); } onCancel () { this.closeModal.emit(); } }