import { Component, Input, OnInit } from '@angular/core'; import { TranslationService } from '@core/services/translation.service'; import { ReferenceFieldAPI } from '@core/typings/api/reference-fields.typing'; import { ReferenceFieldsUI } from '@core/typings/ui/reference-fields.typing'; import { FileService } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; @Component({ selector: 'gc-convert-field-type-modal', templateUrl: './convert-field-type-modal.component.html', styleUrls: ['./convert-field-type-modal.component.scss'] }) export class ConvertFieldTypeModalComponent extends YCModalComponent implements OnInit { @Input() fieldName: string; @Input() validityInfo: ReferenceFieldAPI.ConvertFieldTypeValidationResponse|ReferenceFieldAPI.ConvertTextToDateValidationResponse; @Input() conversionType: ReferenceFieldsUI.RefFieldConversionTypes; ConversionTypes = ReferenceFieldsUI.RefFieldConversionTypes; invalidDates: ReferenceFieldAPI.InvalidDateRecord[] = []; headerText: string; bodyText: string; primaryActionText: string; unableToConvertFieldText = this.i18n.translate( 'FORMS:textUnableToConvertFieldTypeDueToResponses', {}, 'Due to existing form responses, we are unable to convert this field type' ); constructor ( private i18n: I18nService, private fileService: FileService, private analyticsService: AnalyticsService, private translationService: TranslationService ) { super(); } ngOnInit () { this.adaptModalToValidityState(); if (this.conversionType === ReferenceFieldsUI.RefFieldConversionTypes.TEXT_TO_DATE) { this.invalidDates = (this.validityInfo as ReferenceFieldAPI.ConvertTextToDateValidationResponse).invalidDateValues; } } adaptModalToValidityState () { if (this.validityInfo.canConvert) { switch (this.conversionType) { case ReferenceFieldsUI.RefFieldConversionTypes.NUMBER_TO_TEXT: this.headerText = this.i18n.translate( 'FORMS:hdrConvertNumberFieldToText', {}, 'Convert Number Field to Text' ); break; case ReferenceFieldsUI.RefFieldConversionTypes.TEXT_TO_NUMBER: this.headerText = this.i18n.translate( 'FORMS:hdrConvertTextFieldToNumber', {}, 'Convert Text Field to Number' ); break; case ReferenceFieldsUI.RefFieldConversionTypes.DATE_TO_TEXT: this.headerText = this.i18n.translate( 'common:hdrConvertDateFieldToText', {}, 'Convert Date Field to Text' ); break; case ReferenceFieldsUI.RefFieldConversionTypes.TEXT_TO_DATE: this.headerText = this.i18n.translate( 'common:hdrConvertTextFieldToDate', {}, 'Convert Text Field to Date' ); break; } this.primaryActionText = this.i18n.translate( 'common:textConvert', {}, 'Convert' ); } else { this.headerText = this.i18n.translate( 'FORMS:hdrUnableToConverFieldType', {}, 'Unable to Convert Field Type' ); this.bodyText = this.unableToConvertFieldText; this.primaryActionText = null; } } downloadAffectedAdHocFilters () { const csv = this.fileService.convertObjectArrayToCSVString(this.validityInfo.affectedAdHocFilters); this.fileService.downloadString(csv, 'text/csv', 'AffectedAdHocFilters.csv'); this.analyticsService.emitEvent({ eventName: 'Download affected ad hoc filters for field conversion', eventType: EventType.Click, extras: null }); } downloadAffectedAutomationRules () { const csv = this.fileService.convertObjectArrayToCSVString(this.validityInfo.affectedWflAutomationRules); this.fileService.downloadString(csv, 'text/csv', 'AffectedAutomationRules.csv'); this.analyticsService.emitEvent({ eventName: 'Download affected automation rules for field conversion', eventType: EventType.Click, extras: null }); } downloadInvalidDates () { const formTranslations = this.translationService.viewTranslations.FormTranslation; const invalidDates = this.invalidDates.map((date) => { return { [this.i18n.translate('common:textApplicationId', {}, 'Application ID')]: date.applicationId, [this.i18n.translate('common:textForms', {}, 'Forms')]: date.formIds.map((formId) => { return formTranslations[formId]?.Name; }).join(', '), [this.i18n.translate('common:textInvalidDate', {}, 'Invalid date')]: date.invalidDateValue }; }); const csv = this.fileService.convertObjectArrayToCSVString(invalidDates); const invalidDateText = this.i18n.translate('common:hdrInvalidDates', {}, 'Invalid Dates'); this.fileService.downloadString(csv, 'text/csv', `${invalidDateText}.csv`); this.analyticsService.emitEvent({ eventName: 'Download affected invalid dates for field conversion', eventType: EventType.Click, extras: null }); } }