import { Component, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { AddEditLevelResponse, UsersAndRoutesLevel, WorkflowLevelOptions } from '@core/typings/workflow.typing'; import { SelectOption, 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-add-edit-level-modal', templateUrl: './add-edit-level-modal.component.html', styleUrls: ['./add-edit-level-modal.component.scss'] }) export class AddEditLevelModalComponent extends YCModalComponent implements OnInit { level: UsersAndRoutesLevel; title = ''; isSubExisting = false; isSubAdding = false; formGroup: TypeSafeFormGroup; levelName: string; levelActions: SelectOption[] = [{ display: this.i18n.translate( 'WORKFLOW:textAllowApprovalAtLevel', {}, 'Allow approval at this level' ), value: WorkflowLevelOptions.ALLOW_APPROVAL }, { display: this.i18n.translate( 'WORKFLOW:textAllowDeclineAtLevel', {}, 'Allow declination at this level' ), value: WorkflowLevelOptions.ALLOW_DECLINE }, { display: this.i18n.translate( 'WORKFLOW:textAllowAwardActionsUpdated', {}, 'Allow user to create and edit awards and payments' ), value: WorkflowLevelOptions.ALLOW_AWARD_ACTIONS }, { display: this.i18n.translate( 'WORKFLOW:textAllowUserToRequestRevision', {}, 'Allow user to request revision' ), value: WorkflowLevelOptions.ALLOW_REQUEST_REVISION }, { display: this.i18n.translate( 'WORKFLOW:textAllowUsersToArchiveApplication', {}, 'Allow user to archive/unarchive application' ), value: WorkflowLevelOptions.ALLOW_ARCHIVE_APPLICATIONS }, { display: this.i18n.translate( 'WORKFLOW:textAllowUserToRecommendFunding', {}, 'Allow user to recommend funding' ), value: WorkflowLevelOptions.ALLOW_RECOMMEND_FUNDING }]; constructor ( private i18n: I18nService, private formBuilder: TypeSafeFormBuilder, private analyticsService: AnalyticsService ) { super(); } ngOnInit () { if (this.level) { this.levelName = this.level.name; if (this.isSubAdding) { this.title = this.i18n.translate( 'WORKFLOW:hdrCreateASubLevel', {}, 'Create a Sub-level' ); } else { this.title = this.i18n.translate( this.isSubExisting ? 'WORKFLOW:hdrEditSubLevel2' : 'WORKFLOW:hdrEditLevel2', {}, this.isSubExisting ? 'Edit Sub-level' : 'Edit Level' ); } } else { if (this.isSubAdding) { this.title = this.i18n.translate( 'WORKFLOW:hdrCreateASubLevel', {}, 'Create a Sub-Level' ); } else { this.title = this.i18n.translate( 'WORKFLOW:hdrCreateAWorkflowLevel', {}, 'Create a Workflow Level' ); } } this.formGroup = this.formBuilder.group({ name: [ this.level ? this.level.name : '', [Validators.required, Validators.maxLength(50)] ], description: [ this.level ? this.level.description : '', Validators.required ], levelActions: [this.getLevelActions()], showMaskedApplicantInfo : this.level?.showMaskedApplicantInfo ?? false, allowFormDueDateExtension: this.level?.allowFormDueDateExtension ?? false, showBudgetSummaryInfo: this.level?.showBudgetSummaryInfo ?? false, allowUserToViewAwardsAndPayments: this.level?.allowUserToViewAwardsAndPayments ?? false }); } getLevelActions () { if (this.level) { const canApprove = this.level.allowApproval; const canDecline = this.level.allowDeclination; const canRequestRevision = this.level.allowUserToRequestRevision; const canArchiveAndUnarchive = this.level.allowUserToArchiveAndUnarchive; const canAward = this.level.allowAward; const canRecommendFunding = this.level.allowRecommendedFunding; return [ canArchiveAndUnarchive ? WorkflowLevelOptions.ALLOW_ARCHIVE_APPLICATIONS : null, canApprove ? WorkflowLevelOptions.ALLOW_APPROVAL : null, canDecline ? WorkflowLevelOptions.ALLOW_DECLINE : null, canRequestRevision ? WorkflowLevelOptions.ALLOW_REQUEST_REVISION : null, canAward ? WorkflowLevelOptions.ALLOW_AWARD_ACTIONS : null, canRecommendFunding ? WorkflowLevelOptions.ALLOW_RECOMMEND_FUNDING : null ].filter((val) => !!val); } return []; } onCancel () { this.closeModal.emit(); } onSave () { this.closeModal.emit(this.formGroup.value); this.analyticsService.emitEvent({ eventName: 'Add edit wfl level save', eventType: EventType.Click, extras: null }); } }