import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { PolicyService } from '@core/services/policy.service'; import { SpinnerService } from '@core/services/spinner.service'; import { AppliedOrganization } from '@core/typings/organization.typing'; import { APConfigService } from '@features/ap-config/ap-config.service'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { SystemTagsService } from '@features/system-tags/system-tags.service'; import { SystemTags } from '@features/system-tags/typings/system-tags.typing'; import { Address, DebounceFactory, PaginationOptions, TableDataFactory, TopLevelFilter } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { ModalFactory } from '@yourcause/common/modals'; import { uniqBy } from 'lodash'; import { EditVendorIdModalComponent } from '../edit-vendor-id-modal/edit-vendor-id-modal.component'; import { ApplicationManagerResources } from '../resources/application-manager.resources'; @Component({ selector: 'gc-organizations-page', templateUrl: './organizations-page.component.html', styleUrls: ['./organizations-page.component.scss'] }) export class OrganizationsPageComponent implements OnInit { searchPlaceholder = this.i18n.translate( 'common:textSearchByOrganizationNameOrIdentification', {}, 'Search by organization name or identification' ); topLevelFilters = [ new TopLevelFilter( 'text', 'name', '', this.searchPlaceholder, undefined, this.searchPlaceholder, [{ column: 'name', filterType: 'cn' }, { column: 'orgIdentification', filterType: 'cn' }] ) ]; tagOptions = uniqBy( this.systemTagsService.getTagsForBucket( SystemTags.Buckets.NonprofitProfile, true ), 'id' ).map(tag => { return { label: tag.name, value: tag.id }; }); tableDataFactory: TableDataFactory; hasMaskedPermission = false; showVendorId = this.clientSettingsService.clientSettings.apEnabled && this.policyService.system.canManageClientSettings(); constructor ( private i18n: I18nService, private applicationManagerResources: ApplicationManagerResources, private router: Router, private policyService: PolicyService, private systemTagsService: SystemTagsService, private modalFactory: ModalFactory, private spinnerService: SpinnerService, private apConfigService: APConfigService, private clientSettingsService: ClientSettingsService ) { } get isNomination () { return location.pathname.includes('nomination'); } ngOnInit () { this.hasMaskedPermission = this.policyService.grantApplication .canSeeMaskedApplicants(); this.setTableDataFactory(); } async setTableDataFactory () { this.tableDataFactory = DebounceFactory.createSimple( async (options: PaginationOptions) => { options = this.systemTagsService.formatPaginationOptions(options); // Org Identification is stored without the "-", so remove if search term has it options = { ...options, orFilterColumns: options.orFilterColumns.map((col) => { if (col.columnName === 'orgIdentification') { col.filters.forEach((filter) => { filter.filterValue = (filter.filterValue + '').replace('-', ''); }); } return col; }) }; const result = await this.applicationManagerResources.getOrganizations( options, this.isNomination ); return { success: true, data: { recordCount: result.recordCount, records: result.records } }; } ); } goToOrg (row: AppliedOrganization) { this.router.navigate([`/management/manage-${ this.isNomination ? 'nominations' : 'applications' }/nonprofit/${ row.nonprofitGuid || row.id }/profile`]); } getAddress (row: AppliedOrganization): Address { return { address1: row.address, address2: row.address2, city: row.city, stateProvRegCode: row.state, countryCode: row.country, postalCode: row.postalCode }; } async editVendorIdModal (row: AppliedOrganization) { const response = await this.modalFactory.open( EditVendorIdModalComponent, { existingVendorId: row.vendorId } ); if (response) { this.spinnerService.startSpinner(); await this.apConfigService.updateVendorId( row.id, response.vendorId, false ); this.tableDataFactory.reset.emit(); this.spinnerService.stopSpinner(); } } }