import { Component, Input, OnInit } from '@angular/core'; import { ReferenceFieldAPI } from '@core/typings/api/reference-fields.typing'; import { ReferenceFieldsService } from '@features/reference-fields/services/reference-fields.service'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; import { CustomDataTablesService } from '../custom-data-table.service'; import { CustomDataTable, DeleteCDTModalScenario } from '../custom-data-tables.typing'; @Component({ selector: 'gc-delete-data-table-modal', templateUrl: './delete-data-table-modal.component.html', styleUrls: ['./delete-data-table-modal.component.scss'] }) export class DeleteDataTableModalComponent extends YCModalComponent<{ picklistId: number; }> implements OnInit { @Input() customDataTable: CustomDataTable; modalText: string; formFieldRows: ReferenceFieldAPI.ReferenceFieldDisplayModel[]; childDataTableRows: CustomDataTable[]; scenario: DeleteCDTModalScenario; DeleteCDTModalScenario = DeleteCDTModalScenario; constructor ( private refFieldsService: ReferenceFieldsService, private i18n: I18nService, private customDataTableService: CustomDataTablesService, private analyticsService: AnalyticsService ) { super(); } ngOnInit () { // check to see if picklist is in use // set rows and set scenario based on rows.length // use rows for table this.setTableAndScenario(); } setTableAndScenario () { // validation is tied to whether this CDT is in use by a reference field (form field) // and if this CDT is a parent for another CDT (dependent picklists) this.formFieldRows = this.refFieldsService.allReferenceFields.filter((refField) => { return refField.customDataTableGuid === this.customDataTable.guid; }); this.childDataTableRows = this.customDataTableService.customDataTables.filter((cdt) => { return cdt.parentPicklistId === this.customDataTable.id; }); // set senario based on use in form fields // // check for parent/child scenario after since they will // likely resolve this (unless this CDT is used by multiple form fields) // while resolving the use on form field if (this.formFieldRows.length > 0) { this.scenario = DeleteCDTModalScenario.IsUsedByFormFields; } else if (this.childDataTableRows.length > 0) { this.scenario = DeleteCDTModalScenario.IsAParentFormField; } else { this.scenario = DeleteCDTModalScenario.IsAbleToDelete; } // set text based on scenario switch (this.scenario) { case (DeleteCDTModalScenario.IsUsedByFormFields): this.modalText = this.i18n.translate( 'FORMS:textUnableToDeleteUsedByFormFields', {}, 'The selected custom data table cannot be deleted at this time because it is being used on one or more form fields. See below for a list of form fields using the selected data table.' ); break; case (DeleteCDTModalScenario.IsAParentFormField): this.modalText = this.i18n.translate( 'FORMS:textUnableToDeleteISAParentFormField', {}, 'The selected custom data table cannot be deleted at this time because it is the parent for one or more child data tables. See below for a list of data tables using the selected data table as the parent.' ); break; default: case (DeleteCDTModalScenario.IsAbleToDelete): this.modalText = this.i18n.translate( 'FORMS:textDeleteCustomDataTableText', {}, 'The selected custom data table will be deleted. This action cannot be undone. Are you sure you want to delete this data table?' ); break; } } onCancel () { this.closeModal.emit(); } onSubmit () { this.closeModal.emit({ picklistId: this.customDataTable.id }); this.analyticsService.emitEvent({ eventName: 'Delete data table modal submit', eventType: EventType.Click, extras: null }); } }