import { HttpErrorResponse } from '@angular/common/http'; import { Component, Input, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { PortalDeterminationService } from '@core/services/portal-determination.service'; import { SpinnerService } from '@core/services/spinner.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 { AddressFormatterService, FileService, 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'; import * as parse from 'papaparse'; @Component({ selector: 'yc-personal-data-modal', templateUrl: './personal-data-modal.component.html', styleUrls: ['./personal-data-modal.component.scss'] }) export class PersonalDataModalComponent extends YCModalComponent implements OnInit { @Input() user: Applicant|User; formGroup: TypeSafeFormGroup<{ password: string }>; passwordVisible = false; displayAccountInfo = false; userIsSSO = false; downloadTest = this.i18n.translate( 'common:textDownload', {}, 'Download' ); viewText = this.i18n.translate( 'ACCOUNT:textViewAccountInfo', {}, 'View account information' ); cancelText = this.i18n.translate( 'common:btnCancel', {}, 'Cancel' ); closeText = this.i18n.translate( 'common:textClose', {}, 'Close' ); firstNameText = this.i18n.translate('common:lblFirstName', {}, 'First name'); lastNameText = this.i18n.translate('common:lblLastName', {}, 'Last name'); emailText = this.i18n.translate('common:textEmail', {}, 'Email'); phoneText = this.i18n.translate('common:textPhoneNumber', {}, 'Phone number'); addressText = this.i18n.translate('common:textAddress', {}, 'Address'); jobTitleText = this.i18n.translate('common:textJobTitle', {}, 'Job title'); constructor ( private formBuilder: TypeSafeFormBuilder, private fileService: FileService, private tokenService: TokenService, private determinationService: PortalDeterminationService, private notifier: NotifierService, private i18n: I18nService, private addressFormatter: AddressFormatterService, private analyticsService: AnalyticsService, private spinnerService: SpinnerService ) { super(); } get jobTitle () { return 'jobTitle' in this.user ? this.user.jobTitle : ''; } get phoneNumber () { return 'phoneNumber' in this.user ? this.user.phoneNumber : ''; } get isManager () { return this.determinationService.isManager; } get tokenCall () { return this.isManager ? 'getTokenForManager' : 'getTokenForPortal'; } get simpleAddress () { return { address: (this.user as Applicant).address, address2: (this.user as Applicant).address2, city: (this.user as Applicant).city, state: (this.user as Applicant).state, country: (this.user as Applicant).country, postalCode: (this.user as Applicant).postalCode }; } get usersAddress () { return this.addressFormatter.format({ address1: (this.user as Applicant).address, address2: (this.user as Applicant).address2, city: (this.user as Applicant).city, stateProvRegCode: (this.user as Applicant).state, country: (this.user as Applicant).country, postalCode: (this.user as Applicant).postalCode }, true); } ngOnInit () { this.formGroup = this.formBuilder.group({ password: ['', Validators.required] }); this.userIsSSO = this.user.isSso; this.displayAccountInfo = this.user.isSso; } togglePasswordVisible = () => { this.passwordVisible = !this.passwordVisible; }; handlePrimaryClick () { if (this.displayAccountInfo) { this.analyticsService.emitEvent({ eventName: 'Download personal data', eventType: EventType.Click, extras: null }); return this.isManager ? this.downloadForManager() : this.downloadForPortal(); } else { this.analyticsService.emitEvent({ eventName: 'View personal data', eventType: EventType.Click, extras: null }); return this.viewAccountInfo(); } } async viewAccountInfo () { this.spinnerService.startSpinner(); try { await this.tokenService.login(this.user.email, this.formGroup.value.password); this.displayAccountInfo = true; } catch (err) { const e = err as HttpErrorResponse; if (e?.error?.message === 'Incorrect email or password') { this.notifier.error(this.i18n.translate( 'ACCOUNT:textPasswordIncorrect', {}, 'Incorrect password' )); } else { this.displayAccountInfo = true; } } this.spinnerService.stopSpinner(); } downloadForPortal () { const applicant = [{ [this.firstNameText]: this.user.firstName, [this.lastNameText]: this.user.lastName, [this.emailText]: this.user.email, [this.phoneText]: this.phoneNumber, [this.addressText]: this.addressFormatter.formatSimpleGrantsAddressToSingleLine(this.simpleAddress) }]; const csv = parse.unparse(applicant); this.fileService.downloadCSV(csv); } downloadForManager () { const manager = [{ [this.firstNameText]: this.user.firstName, [this.lastNameText]: this.user.lastName, [this.emailText]: this.user.email, [this.jobTitleText]: this.jobTitle }]; const csv = parse.unparse(manager); this.fileService.downloadCSV(csv); } onCancel () { this.closeModal.emit(); } }