import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { Router } from '@angular/router'; import { GC_Feature_Flags } from '@core/services/feature-flags.service'; import { SpinnerService } from '@core/services/spinner.service'; import { TranslationService } from '@core/services/translation.service'; import { ProgramImport } from '@core/typings/program.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { ImportExportProgramsModalComponent } from '@features/programs/import-export-programs-modal/import-export-programs-modal.component'; import { Base64, Tab } from '@yourcause/common'; import { FeatureFlagService } from '@yourcause/common/feature-flag'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { ModalFactory } from '@yourcause/common/modals'; import { NotifierService } from '@yourcause/common/notifier'; import { CreateProgramModalComponent } from '../create-program-modal/create-program-modal.component'; import { ProgramAutomationBuilderModalComponent } from '../program-automation/program-automation-builder/program-automation-builder-modal/program-automation-builder-modal.component'; import { ProgramAutomationService } from '../program-automation/program-automation.service'; import { ProgramService } from '../program.service'; @Component({ selector: 'gc-programs-page', templateUrl: './programs-page-wrapper.component.html', styleUrls: ['./programs-page-wrapper.component.scss'] }) export class ProgramsPageWrapperComponent implements OnInit { @ViewChild('importFile') importFileButton: ElementRef; title = ''; description = ''; automationEnabled = this.featureFlagService.getFeatureFlagEnabled(GC_Feature_Flags.GrantProgramAutomation); tabs: Tab[] = [{ link: this.isNomination ? '/management/program-setup/nomination-programs/all' : '/management/program-setup/programs/all', labelKey: 'GLOBAL:textPrograms', label: 'Programs' }, !this.isNomination && this.automationEnabled ? { link: '/management/program-setup/programs/automation', labelKey: 'common:textAutomation', label: 'Automation' } : undefined].filter((item) => !!item); programImportAllowed = this.programService.programImportAllowed(); hasInternational = this.clientSettingsService.clientSettings.hasInternational; hasLangs = !this.clientSettingsService.noSelectedLanguages; constructor ( private logger: LogService, private spinnerService: SpinnerService, private router: Router, private notifier: NotifierService, private programService: ProgramService, private modalFactory: ModalFactory, private i18n: I18nService, private clientSettingsService: ClientSettingsService, private translationService: TranslationService, private programAutomationService: ProgramAutomationService, private featureFlagService: FeatureFlagService ) { } get isNomination () { return location.pathname.includes('nomination'); } ngOnInit () { this.title = this.i18n.translate( 'common:MANAGE_PROGRAMS' ); this.description = this.i18n.translate( 'PROGRAM:textProgramsDesc', {}, 'Create and manage your individual grant programs.' ); if (this.isNomination) { this.title = this.i18n.translate( 'common:textNominationPrograms' ); this.description = this.i18n.translate( 'PROGRAM:textNominationProgramsDesc', {}, 'Create and manage your individual nomination programs.' ); } } async createProgram () { const response = await this.modalFactory.open( CreateProgramModalComponent, { isNomination: this.isNomination } ); if (response) { this.spinnerService.startSpinner(); const id = await this.programService.handleCreateProgram( response, this.isNomination ); this.spinnerService.stopSpinner(); if (id) { const route = `/management/program-setup/${ this.isNomination ? 'nomination-programs' : 'programs' }/program/${id}`; this.router.navigate([route]); } } } async importPrograms () { const onChange = (changeEvent: any) => { const [file] = changeEvent.target.files; const fileReader = new FileReader(); fileReader.addEventListener('loadend', async (loadEvent: any) => { let programImport: ProgramImport; try { let stringToParse: string = loadEvent.target.result; if (!stringToParse.startsWith('{') && !stringToParse.endsWith('}')) { stringToParse = Base64.decode(stringToParse); } programImport = JSON.parse(stringToParse); } catch (err) { this.notifier.error(this.i18n.translate( 'PROGRAM:notificationErrorWithFile', {}, 'There was a problem with the file provided' )); this.logger.error(err); throw err; } const previouslyImportedData = await this.programService.checkExistingDataForImport( programImport ); const doImport = await this.modalFactory.open( ImportExportProgramsModalComponent, { previouslyImportedData, programImport, isImport: true, isNomination: this.isNomination } ); if (doImport) { this.spinnerService.startSpinner(); const success = await this.programService.handleProgramImport( programImport, this.isNomination ); if (success) { this.programImportAllowed = false; await this.translationService.resetViewTranslations(); } this.spinnerService.stopSpinner(); } }); fileReader.readAsText(file); }; this.importFileButton.nativeElement.onchange = onChange; this.importFileButton.nativeElement.click(); } translate (isAutomation = false) { const route = isAutomation ? '/management/program-setup/programs/translate-automation' : '/management/program-setup/programs/translate-details'; this.router.navigate([route]); } async createAutomationRule () { const needsUpdate = await this.modalFactory.open( ProgramAutomationBuilderModalComponent, { }, { class: 'modal-full-size' } ); if (needsUpdate) { this.programAutomationService.resetProgramAutomationTableRepo(); this.programAutomationService.resetProgramAutomationRules(); } } }