import { Component, Input, OnChanges, OnInit } from '@angular/core'; import { PolicyService } from '@core/services/policy.service'; import { TranslationService } from '@core/services/translation.service'; import { AwardService } from '@features/awards/award.service'; import { AwardForDash, AwardPaymentInfo } from '@features/awards/typings/award.typing'; import { APIResult, DebounceFactory, PaginationOptions, SimpleDebounceFactory, TableCSVFactory, TableDataFactory, TopLevelCustomLink, TopLevelFilter } from '@yourcause/common'; import { CachedAttr, CACHE_TYPES } from '@yourcause/common/cache'; import { I18nService } from '@yourcause/common/i18n'; @Component({ selector: 'gc-award-list', templateUrl: './award-list.component.html', styleUrls: ['./award-list.component.scss'] }) export class AwardListComponent implements OnInit, OnChanges { @Input() cycleIds: number[]; @Input() key = ''; @Input() isSingleProgram = false; awardListFilters: TopLevelFilter[] = []; awardListFactory: TableDataFactory; awardListCsvFactory: TableCSVFactory; canViewProgram = ( this.policyService.grantApplication.canAccessApplicationManager() ) && this.policyService.insights.canViewPrograms(); drillDownClosed = this.i18n.translate( 'AWARDS:textShowPaymentsDrillDown', {}, 'Show payments' ); drillDownOpen = this.i18n.translate( 'AWARDS:textHidePaymentsDrillDown', {}, 'Hide payments' ); viewTranslations = this.translationService.viewTranslations; programTranslationMap = this.viewTranslations.Grant_Program; cycleTranslationMap = this.viewTranslations.Grant_Program_Cycle; hasMaskedAppsOnPage = false; showMaskedApplicantsIcon = 'user-shield'; hideMaskedApplicantsIcon = 'user-times'; hideMaskedApplicantsTooltip = this.i18n.translate( 'MANAGE:textShowApplicantInformation', {}, 'Show applicant information' ); showMaskedApplicantsTooltip = this.i18n.translate( 'MANAGE:textHideApplicantInformation', {}, 'Hide applicant information' ); topLevelCustomLinks: TopLevelCustomLink[] = []; @CachedAttr( CACHE_TYPES.LOCALSTORAGE, false ) showMaskedApplicants: boolean; constructor ( private i18n: I18nService, private policyService: PolicyService, private translationService: TranslationService, private awardService: AwardService ) { } ngOnInit () { this.awardListFilters = [ new TopLevelFilter( 'text', 'applicant.fullName', '', this.i18n.translate( 'AWARDS:textSearchByAwardName', {}, 'Search by award amount, award ID, program, applicant, organization, or application ID' ), undefined, undefined, [{ column: 'applicant.fullName', filterType: 'cn' }, { column: 'program.name', filterType: 'cn' }, { column: 'organization.name', filterType: 'cn' }, { column: 'award.amount', filterType: 'eq' }, { column: 'award.id', filterType: 'eq' }, { column: 'program.id', filterType: 'eq' }, { column: 'award.associatedApplicationId', filterType: 'eq' }] ) ]; this.awardListFactory = this.getAwardListFactory(false); this.awardListCsvFactory = this.getAwardListFactory(true); } getAwardListFactory ( isCsv: C ): SimpleDebounceFactory< PaginationOptions, C extends true ? string : APIResult> { return DebounceFactory.createSimple( async (options: PaginationOptions) => { options.rowsPerPage = 10; const result = await this.awardService.getAwardListForInsights( options, this.cycleIds, isCsv ); if (!isCsv) { this.handleMasking((result as APIResult).data.records); } return result; } ); } handleMasking (records: AwardForDash[]) { let hasMaskedApps = false; records.forEach((row) => { if (row.isMasked && row.canViewMaskedApplicantInfo) { hasMaskedApps = true; } }); this.hasMaskedAppsOnPage = hasMaskedApps; if (this.hasMaskedAppsOnPage) { this.setMaskedIconAndTooltip(); } } toggleShowMaskedApplicants () { this.showMaskedApplicants = !this.showMaskedApplicants; this.setMaskedIconAndTooltip(); } setMaskedIconAndTooltip () { const tooltip = this.showMaskedApplicants ? this.showMaskedApplicantsTooltip : this.hideMaskedApplicantsTooltip; const item = { name: tooltip, tooltip, icon: this.showMaskedApplicants ? this.hideMaskedApplicantsIcon : this.showMaskedApplicantsIcon, isIcon: true, onClick: () => { this.toggleShowMaskedApplicants(); } }; if (this.topLevelCustomLinks.length === 0) { this.topLevelCustomLinks = [item]; } else { this.topLevelCustomLinks[0] = item; } } getPaymentSum (payments: AwardPaymentInfo[]) { let paymentSum = 0; payments.forEach(payment => { paymentSum += payment.amount; }); return paymentSum; } ngOnChanges () { if (this.awardListFactory) { this.awardListFactory.reset.emit(); } } }