import { Component, OnDestroy, OnInit } from '@angular/core'; import { SpinnerService } from '@core/services/spinner.service'; import { APIDataImport } from '@core/typings/api/data-import.typing'; import { AdminClientService } from '@features/platform-admin/clients/admin-client.service'; import { CreateBatchModalComponent } from '@features/platform-admin/historical-imports/create-batch-modal/create-batch-modal.component'; import { DataHubReportStatus } from '@features/reporting/data-hub/data-hub.typing'; import { DebounceFactory, PollingService, TableDataFactory, TopLevelCustomLink, TopLevelFilter } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { ConfirmationModalComponent, ModalFactory } from '@yourcause/common/modals'; import { Subscription } from 'rxjs'; import { HistoricalImportService } from '../historical-import.service'; @Component({ selector: 'gc-view-batches', templateUrl: './view-batches.component.html', styleUrls: ['./view-batches.component.scss'] }) export class ViewBatchesComponent implements OnInit, OnDestroy { numberPending: number; topLevelCustomLinks: TopLevelCustomLink[]; BatchStatus = DataHubReportStatus; tableDataFactory: TableDataFactory; activeClientId = this.adminClientService.activeClientId; topLevelFilters: TopLevelFilter[] = []; sub = new Subscription(); constructor ( private spinnerService: SpinnerService, private historicalImportService: HistoricalImportService, private modalFactory: ModalFactory, private i18n: I18nService, private adminClientService: AdminClientService, private pollingService: PollingService ) { this.sub.add(this.adminClientService.changesTo$('activeClientId') .subscribe((clientId: number) => { if (this.activeClientId !== clientId) { this.refreshTable(); this.activeClientId = this.adminClientService.activeClientId; this.handlePolling(); } })); } async ngOnInit () { this.tableDataFactory = DebounceFactory.createSimple(async (options) => { const results = await this.historicalImportService.getBatches( options, this.activeClientId ); return { success: true, data: { records: results.records, recordCount: results.recordCount } }; }); this.topLevelFilters = [ new TopLevelFilter( 'text', 'name', '', this.i18n.translate( 'common:textSearchByName', {}, 'Search by name' ) ) ]; await this.handlePolling(); } refreshTable () { this.tableDataFactory.reset.emit(); } async handlePolling () { const response = await this.pollingService.handlePolling( this.numberPending, this.getPendingJobsForActiveTable.bind(this), this.getPendingJobsText.bind(this) ); this.numberPending = response.numberPending; this.topLevelCustomLinks = response.topLevelCustomLinks; if (response.doRefresh) { this.refreshTable(); } if (this.numberPending !== 0) { setTimeout(async () => { await this.handlePolling(); }, 10000); } } getPendingJobsForActiveTable () { return this.historicalImportService.getNumberOfPendingBatches( this.activeClientId ); } getPendingJobsText (numberPending: number) { if (numberPending > 0) { return this.i18n.translate( 'common:textDynamicNumberOfJobs2', { numberOfReports: numberPending }, 'Jobs pending: __numberOfReports__' ); } else { return this.i18n.translate( 'common:textNoPendingJobs', {}, 'No pending jobs' ); } } async createNewBatch () { const res = await this.modalFactory.open(CreateBatchModalComponent, {}); if (res) { this.spinnerService.startSpinner(); await this.historicalImportService.handleCreateBatch(res); this.tableDataFactory.reset.emit(); this.spinnerService.stopSpinner(); } } async deleteBatch (batch: APIDataImport.DataImportBatchModel) { const deps = { modalHeader: this.i18n.translate( 'common:textDeleteBatch', {}, 'Delete Batch' ), modalSubHeader: batch.name, confirmButtonText: this.i18n.translate( 'common:btnDelete', {}, 'Delete' ), confirmText: this.i18n.translate( 'MANAGE:textConfirmAreYouSureDeleteTheBatch', {}, 'Are you sure you want to delete the batch? This action cannot be undone.' ) }; const response = await this.modalFactory.open( ConfirmationModalComponent, deps ); if (response) { this.spinnerService.startSpinner(); await this.historicalImportService.handleDeleteBatch(batch); this.tableDataFactory.reset.emit(); this.spinnerService.stopSpinner(); } } ngOnDestroy () { this.sub.unsubscribe(); } }