import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; import { PortalDeterminationService } from '@core/services/portal-determination.service'; import { ApplicantProfileData } from '@core/typings/applicant.typing'; import { Collaborator } from '@core/typings/collaboration.typing'; import { NonprofitProfileData, OrganizationForInfoPanel } from '@core/typings/organization.typing'; import { ApplicantManagerService } from '@features/applicant/applicant-manager.service'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { NonprofitService } from '@features/nonprofit/nonprofit.service'; import { UserService } from '@features/users/user.service'; import { I18nService } from '@yourcause/common/i18n'; import { Subscription } from 'rxjs'; import { CollaborationService } from '../collaboration.service'; @Component({ selector: 'gc-applicant-info-panel', templateUrl: './applicant-info-panel.component.html', styleUrls: ['./applicant-info-panel.component.scss'] }) export class ApplicantInfoPanelComponent implements OnInit, OnDestroy { @Input() organization: OrganizationForInfoPanel; @Input() applicationId: number; // For applications not created yet (in applicant portal) or where collaboration isn't allowed @Input() applicant: Collaborator; @Input() clientId: number; @Input() clientName: string; @Input() allowCollaboration: boolean; @Input() closedOnInit = false; @Input() isMasked = false; @Input() slim = false; @Input() updateApplicationViewMap = false; @Input() forNomination = false; @Input() getRouterLinkForApplicant: (collab: Collaborator) => string[]; @Input() routerLinkForNonprofit: string | string[]; @Output() onUpdateOrg = new EventEmitter(); @Output() onOrgClick = new EventEmitter(); collaborators: Collaborator[]; expandApplicantContainer = true; applicantMaskedText = this.i18n.translate( 'GLOBAL:textApplicantInfoIsMasked', {}, 'Applicant information is masked' ); nonprofitDetail: NonprofitProfileData; applicantDetail: ApplicantProfileData; hasNominations = this.userService.user?.clientHasNominations; defaultCurrency = this.clientSettingsService.defaultCurrency; sub = new Subscription(); isManager = this.portal.isManager; constructor ( private collaborationService: CollaborationService, private i18n: I18nService, private portal: PortalDeterminationService, private applicantManagerService: ApplicantManagerService, private nonprofitService: NonprofitService, private userService: UserService, private clientSettingsService: ClientSettingsService ) { } async ngOnInit () { this.expandApplicantContainer = !this.closedOnInit; if (this.forNomination) { this.applicantMaskedText = this.i18n.translate( 'GLOBAL:textNominatorInfoIsMasked', {}, 'Nominator information is masked' ); } if (this.isManager) { if (this.applicant?.id) { await this.setApplicantDetails(this.applicant.id); } if (this.organization?.guid || this.organization?.id) { this.setOrgDetails(); } } if (this.applicationId && !this.isMasked) { await this.collaborationService.fetchCollaborators( this.applicationId ); this.sub.add( this.collaborationService.changesTo$('collaboratorMap') .subscribe(async () => { const map = this.collaborationService.collaboratorMap; const allApplicants = (map[this.applicationId] || []).map((collab) => { if (this.getRouterLinkForApplicant) { collab.routerLink = this.getRouterLinkForApplicant(collab); } return collab; }); const primaryApplicant = allApplicants.find((app) => { return app.isApplicationOwner; }); if (primaryApplicant && this.isManager) { await this.setApplicantDetails(primaryApplicant.id); } else if (primaryApplicant.id !== this.applicant.id) { // this user is not the primary applicant and is a collaborator this.applicant = primaryApplicant; } this.collaborators = allApplicants.filter((applicant) => { // filter out primary applicant return !applicant.isApplicationOwner; }); }) ); } else if (this.applicant && this.isManager) { this.applicant.isApplicationOwner = true; await this.setApplicantDetails(this.applicant.id); } } async setOrgDetails () { if (this.organization?.guid) { await this.nonprofitService.setNonprofit(this.organization.guid); this.nonprofitDetail = this.nonprofitService.nonprofitMap[ this.organization.guid ]; } else if (this.organization?.id) { await this.nonprofitService.setCompanyOrgDetail(this.organization.id); this.nonprofitDetail = this.nonprofitService.nonprofitMap[ this.organization.id ]; } } async setApplicantDetails (applicantId: number) { await this.applicantManagerService.setApplicantProfileDetails( applicantId ); this.applicantDetail = this.applicantManagerService.applicantMap[ applicantId ]; } ngOnDestroy () { this.sub.unsubscribe(); } }