import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { ClassificationService } from '@core/services/classification.service'; import { SpinnerService } from '@core/services/spinner.service'; import { LocationState } from '@core/states/location.state'; import { ProgramService } from '@features/programs/program.service'; import { ALL_SKIP_FILTER, AutoTableRepositoryFactory, BulkAction, DebounceFactory, PaginationOptions, SelectOption, TableDataFactory, TopLevelFilter, TypeaheadSelectOption } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { InvitationService } from '../invitation.service'; import { AvailableApplicant } from '../invitation.typing'; @Component({ selector: 'gc-available-applicants-table', templateUrl: './available-applicants-table.component.html', styleUrls: ['./available-applicants-table.component.scss'] }) export class AvailableApplicantsTableComponent implements OnInit { @Input() id: number; @Output() onRecordCountChange = new EventEmitter(); topLevelFilters: TopLevelFilter[] = []; tableDataFactory: TableDataFactory; bulkActions: BulkAction[] = []; key: string; classificationOptions: TypeaheadSelectOption[] = []; programSelects = this.programService.allActiveManagerPrograms .map((prog) => { return { display: prog.grantProgramName, value: prog.grantProgramId }; }); programOptions = { selectOptions: [{ display: this.i18n.translate( 'common:textAllPrograms', {}, 'All program' ), value: ALL_SKIP_FILTER }, ...this.programSelects ] }; countryOptions: SelectOption[] = []; constructor ( private i18n: I18nService, private invitationService: InvitationService, private spinnerService: SpinnerService, private programService: ProgramService, private classificationService: ClassificationService, private locationState: LocationState, private autoTableFactory: AutoTableRepositoryFactory ) { } ngOnInit () { this.countryOptions = this.locationState.countries.map((country) => { return { display: country.name, value: country.code }; }); this.classificationOptions = this.classificationService.classificationOptions; this.key = this.invitationService.getAvailableApplicantsRepoKey(this.id); this.topLevelFilters = [ new TopLevelFilter( 'text', 'applicant.applicantName', '', this.i18n.translate( 'GLOBAL:textSearchByApplicantOrOrganization', {}, 'Search by applicant or organization' ), undefined, undefined, [{ column: 'applicant.applicantName', filterType: 'cn' }, { column: 'applicant.applicantEmail', filterType: 'cn' }, { column: 'organization.name', filterType: 'cn' }] ), new TopLevelFilter( 'typeaheadSingleEquals', 'grantProgramId', ALL_SKIP_FILTER, '', this.programOptions, this.i18n.translate( 'common:lblProgram', {}, 'Program' ) ) ]; this.bulkActions = [{ label: this.i18n.translate( 'PROGRAM:textIncludeInDistributionList', {}, 'Include in distribution list' ), disabled: () => { return false; }, exec: (items: AvailableApplicant[]) => { const ids = items.map((item) => item.applicant.applicantId); return this.addApplicants(ids); } }]; const repo = this.autoTableFactory.getRepository(this.key); if (repo) { repo.reset(); } this.tableDataFactory = DebounceFactory.createSimple( async (options: PaginationOptions) => { const response = this.invitationService.extractProgramIdFromOptions( options ); const data = await this.invitationService.getAvailableApplicants( this.id, response.options, response.programId ); if (data.recordCount || data.recordCount === 0) { this.onRecordCountChange.emit(data.recordCount); } return { success: true, data }; } ); } async addApplicants (applicantIds: number[]) { this.spinnerService.startSpinner(); const repo = this.autoTableFactory.getRepository(this.key); await this.invitationService.handleAddApplicantsToList( this.id, applicantIds ); repo.reset(); this.spinnerService.stopSpinner(); } }