import { Component, Input, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { SpinnerService } from '@core/services/spinner.service'; import { CreateProgramModalResponse } from '@core/typings/program.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { FormTypes } from '@features/configure-forms/form.typing'; import { FormsResolver } from '@features/configure-forms/resolvers/forms.resolver'; import { FormsService } from '@features/configure-forms/services/forms/forms.service'; import { WorkflowResolver } from '@features/workflow/resolvers/workflow.resolver'; import { WorkflowService } from '@features/workflow/workflow.service'; import { ArrayHelpersService, TypeaheadSelectOption, TypeSafeFormBuilder, TypeSafeFormGroup } 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-create-program-modal', templateUrl: './create-program-modal.component.html', styleUrls: ['./create-program-modal.component.scss'] }) export class CreateProgramModalComponent extends YCModalComponent implements OnInit { @Input() isNomination = false; formGroup: TypeSafeFormGroup; hasInternational = this.clientSettingsService.clientSettings.hasInternational; langKeys: string[]; modalHeader: string; workflowOptions: TypeaheadSelectOption[] = []; formOptions: TypeaheadSelectOption[] = []; constructor ( private clientSettingsService: ClientSettingsService, private formBuilder: TypeSafeFormBuilder, private i18n: I18nService, private workflowService: WorkflowService, private workflowResolver: WorkflowResolver, private arrayHelper: ArrayHelpersService, private formService: FormsService, private formsResolver: FormsResolver, private analyticsService: AnalyticsService, private spinnerService: SpinnerService ) { super(); } async ngOnInit () { this.spinnerService.startSpinner(); await Promise.all([ this.workflowResolver.resolve(), this.formsResolver.resolve() ]); this.spinnerService.stopSpinner(); this.workflowOptions = this.arrayHelper.sort( this.workflowService.workflows.map((workflow) => { return { label: workflow.name, value: workflow.id }; }), 'label' ); this.setFormOptions(); this.modalHeader = this.isNomination ? this.i18n.translate( 'PROGRAM:hdrCreateNominationProgram', {}, 'Create Nomination Program' ) : this.i18n.translate( 'PROGRAM:hdrCreateGrantProgram', {}, 'Create Grant Program' ); if (this.hasInternational) { let langKeys = this.clientSettingsService.get('selectedLanguages'); if (langKeys.length === 0) { langKeys = [this.clientSettingsService.defaultLanguage]; } this.langKeys = langKeys; } const defaultLang = this.clientSettingsService.defaultLanguage; this.formGroup = this.formBuilder.group({ name: [ '', [ Validators.required, Validators.maxLength(50) ] ], defaultLang: this.hasInternational ? [defaultLang, Validators.required] : [defaultLang], workflowId: [null, Validators.required], defaultFormId: [null, Validators.required] }); } setFormOptions () { const forms = this.formService.published; this.formOptions = this.arrayHelper.sort( forms.filter((form) => { return this.isNomination ? form.formType === FormTypes.NOMINATION : form.formType === FormTypes.REQUEST; }).map((form) => { return { label: form.name, value: form.formId }; }), 'label' ); } handlePrimaryClick () { this.closeModal.emit(this.formGroup.value); this.analyticsService.emitEvent({ eventName: 'Create program modal save', eventType: EventType.Click, extras: null }); } }