import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { ApplicantService } from '@core/services/auth-user/applicant.service'; import { ApplicantFromSearch } from '@core/typings/applicant.typing'; import { AddApplicantModalComponent } from '@features/application-manager/add-applicant-modal/add-applicant-modal.component'; import { I18nService } from '@yourcause/common/i18n'; import { ModalFactory } from '@yourcause/common/modals'; @Component({ selector: 'gc-applicant', templateUrl: './applicant.component.html', styleUrls: ['./applicant.component.scss'] }) export class ApplicantComponent implements OnInit { @Input() applicationId: number; @Input() selectedApplicant: ApplicantFromSearch; @Input() isMasked: boolean; @Input() canViewMaskedApplicantInfo: boolean; @Input() notifyApplicant: boolean; @Output() onNotifyApplicantChange = new EventEmitter(); @Output() onApplicantChange = new EventEmitter(); @Output() onApplicantModalOpenOrClose = new EventEmitter(); optionLabel: string; updateNominatorText = this.i18n.translate( 'APPLY:NotifyNominatorOfNominationChangesUpdate', {}, 'Nominator should receive emails regarding this nomination' ); updateApplicantText = this.i18n.translate( 'APPLY:textNotifyApplicantOfChangesUpdate', {}, 'Applicant should receive emails regarding this application' ); constructor ( private modalFactory: ModalFactory, private i18n: I18nService, private applicantService: ApplicantService ) { } get isNomination () { return location.pathname.includes('nomination'); } async ngOnInit () { if (this.isNomination) { this.optionLabel = this.updateNominatorText; } else { this.optionLabel = this.updateApplicantText; } } notifyApplicantChange (notifyApplicant: boolean) { this.notifyApplicant = notifyApplicant; this.onNotifyApplicantChange.emit(this.notifyApplicant); } applicantCanReceiveEmails () { if (this.selectedApplicant?.email) { if (this.selectedApplicant.email.toLowerCase().includes('deactivateduser')) { return false; } else { return true; } } return false; } async addApplicant () { this.onApplicantModalOpenOrClose.emit(true); const modalReturn = await this.modalFactory.open( AddApplicantModalComponent, { isNomination: this.isNomination, showAddressOption: true } ); this.onApplicantModalOpenOrClose.emit(false); if (modalReturn) { let applicant: ApplicantFromSearch; const existing = await this.applicantService.exactMatchSearchApplicants( modalReturn.email ); if (existing) { applicant = { ...existing, notifyApplicant: modalReturn.notifyApplicant }; } else { applicant = { ...modalReturn, phoneNumber: null, address: { ...modalReturn.address, address: modalReturn.address.address, state: modalReturn.address.stateProvRegCode, country: modalReturn.address.countryCode } }; } this.onApplicantSelect(applicant); this.notifyApplicantChange(applicant.notifyApplicant); } } async onApplicantSelect (applicant?: ApplicantFromSearch) { this.selectedApplicant = applicant; this.onApplicantChange.emit(applicant); } }