import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { ReferenceFieldsUI } from '@core/typings/ui/reference-fields.typing'; import { FormsService } from '@features/configure-forms/services/forms/forms.service'; import { ReferenceFieldsService } from '@features/reference-fields/services/reference-fields.service'; import { ALL_SKIP_FILTER, AutoTableRepositoryFactory, DebounceFactory, PaginationOptions, TableDataFactory, TopLevelFilter, TypeToken } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { StandardProductConfigurationService } from '../standard-product-configuration.service'; import { StandardProductDashboard, StandardProductForm, StandardProductFormField, StandardProductRecordType, StandardProductReport, StandardProductTypes, STANDARD_PRODUCT_DASHBOARDS_KEY, STANDARD_PRODUCT_FIELDS_KEY, STANDARD_PRODUCT_FORMS_KEY, STANDARD_PRODUCT_REPORTS_KEY } from '../standard-product-configuration.typing'; @Component({ selector: 'gc-standard-product-table', templateUrl: './standard-product-table.component.html', styleUrls: ['./standard-product-table.component.scss'] }) export class StandardProductTableComponent implements OnInit, OnChanges { @Input() type: StandardProductTypes; // Used in Push to Prod Modal only. Else, use activated Route to determine page type @Input() fromPushModal = false; tableDataFactory: TableDataFactory; topLevelFilters: TopLevelFilter[] = []; refTypeMap = this.referenceFieldService.getReferenceFieldTypeToLabelMap(); PageTypes = StandardProductTypes; tableKey: string; afterInit = false; hideTable: boolean; $refType = new TypeToken(); formTypeOptions = this.formService.getFormTypeOptions(); constructor ( private standardProductConfigurationService: StandardProductConfigurationService, private activatedRoute: ActivatedRoute, private i18n: I18nService, private referenceFieldService: ReferenceFieldsService, private autoTableRepository: AutoTableRepositoryFactory, private formService: FormsService ) { } get pageType (): StandardProductTypes { return this.type || this.activatedRoute.snapshot.data.type; } ngOnInit () { this.setTopLevelFilters(); this.setTableDataFactory(); this.afterInit = true; } ngOnChanges (changes: SimpleChanges) { if (changes.type && this.afterInit) { this.hideTable = true; setTimeout(() => { this.setTableDataFactory(); this.hideTable = false; }); } } setTopLevelFilters () { if (!this.fromPushModal) { this.topLevelFilters = [ new TopLevelFilter( 'text', 'name', '', this.i18n.translate( 'common:textSearchByName', {}, 'Search by name' ) ) ]; switch (this.pageType) { case StandardProductTypes.FORMS: case StandardProductTypes.REPORTS: case StandardProductTypes.DASHBOARDS: break; case StandardProductTypes.FORM_FIELDS: this.topLevelFilters = [ ...this.topLevelFilters, new TopLevelFilter( 'typeaheadSingleEquals', 'referenceFieldType', ALL_SKIP_FILTER, undefined, { selectOptions: this.referenceFieldService.getTypeSelectOptionsForFilters() }, this.i18n.translate( 'GLOBAL:textFormFieldType', {}, 'Form field type' ) ) ]; break; } this.topLevelFilters = [ ...this.topLevelFilters, new TopLevelFilter( 'typeaheadSingleEquals', 'returnOnlyRecordsToBeStaged', // this filter will be outside the model / pagination options ALL_SKIP_FILTER, undefined, { selectOptions: [{ label: this.i18n.translate( 'common:lblAllCap', {}, 'All' ), value: ALL_SKIP_FILTER }, { label: this.i18n.translate( 'common:textRecordsToBeStaged', {}, 'Records to be staged' ), value: true }] }, this.i18n.translate( 'common:textRecordsToBeStaged', {}, 'Records to be staged' ) ) ]; } } getExistingTableDataFactory (tableKey: string) { const existingRepo = this.autoTableRepository.getRepository(tableKey); return existingRepo?.tableDataFactory; } setTableDataFactory () { let existingTableDataFactory: TableDataFactory; switch (this.pageType) { case StandardProductTypes.FORMS: this.tableKey = this.fromPushModal ? 'STANDARD_PRODUCT_FORMS_TO_PUSH' : STANDARD_PRODUCT_FORMS_KEY; existingTableDataFactory = this.getExistingTableDataFactory( this.tableKey ); this.tableDataFactory = existingTableDataFactory as TableDataFactory || DebounceFactory.createSimple( (options: PaginationOptions) => { return this.standardProductConfigurationService.getFormsForPlatformTool( options, this.fromPushModal ); } ); break; case StandardProductTypes.FORM_FIELDS: this.tableKey = this.fromPushModal ? 'STANDARD_PRODUCT_FORM_FIELDS_TO_PUSH' : STANDARD_PRODUCT_FIELDS_KEY; existingTableDataFactory = this.getExistingTableDataFactory( this.tableKey ); this.tableDataFactory = existingTableDataFactory as TableDataFactory || DebounceFactory.createSimple( (options: PaginationOptions) => { return this.standardProductConfigurationService.getFieldsForPlatformTool( options, this.fromPushModal ); } ); break; case StandardProductTypes.REPORTS: this.tableKey = this.fromPushModal ? 'STANDARD_PRODUCT_REPORTS_TO_PUSH' : STANDARD_PRODUCT_REPORTS_KEY; existingTableDataFactory = this.getExistingTableDataFactory( this.tableKey ); this.tableDataFactory = existingTableDataFactory as TableDataFactory || DebounceFactory.createSimple( (options: PaginationOptions) => { return this.standardProductConfigurationService.getReportsForPlatformTool( options, this.fromPushModal ); } ); break; case StandardProductTypes.DASHBOARDS: this.tableKey = this.fromPushModal ? 'STANDARD_PRODUCT_DASHBOARDS_TO_PUSH' : STANDARD_PRODUCT_DASHBOARDS_KEY; existingTableDataFactory = this.getExistingTableDataFactory( this.tableKey ); this.tableDataFactory = existingTableDataFactory as TableDataFactory || DebounceFactory.createSimple( (options: PaginationOptions) => { return this.standardProductConfigurationService.getDashboardsForPlatformTool( options, this.fromPushModal ); } ); break; } } }