import { Component, Input, OnInit, Optional } from '@angular/core'; import { FormGroupDirective, ValidatorFn } from '@angular/forms'; import { ValidatorsService } from '@core/services/validators.service'; import { BaseApplication } from '@core/typings/application.typing'; import { FormDecisionTypes } from '@features/configure-forms/form.typing'; import { FormBuilderService } from '@features/formio/form-builder/services/form-builder/form-builder.service'; import { GcFormRendererComponent } from '@features/formio/form-renderer/gc-form-renderer/gc-form-renderer.component'; import { ComponentHelperService } from '@features/formio/services/component-helper/component-helper.service'; import { FormHelperService } from '@features/formio/services/form-helper/form-helper.service'; import { TypeaheadSelectOption, TypeSafeFormBuilder } from '@yourcause/common'; import { BaseFormioComponent } from '../../base/base.component'; @Component({ selector: 'gc-decision', templateUrl: './gc-decision.component.html' }) export class GCDecisionComponent extends BaseFormioComponent implements OnInit { @Input() truthyValue: string; @Input() falsyValue: string; @Input() recuseValue: string; @Input() allowRecused = false; options: TypeaheadSelectOption[]; constructor ( public formBuilder: TypeSafeFormBuilder, private formHelperService: FormHelperService, private validatorService: ValidatorsService, formBuilderService: FormBuilderService, public componentHelper: ComponentHelperService, @Optional() formGroupDir: FormGroupDirective, @Optional() renderer: GcFormRendererComponent ) { super(renderer, formGroupDir, formBuilder, formBuilderService, componentHelper); } ngOnInit () { super.ngOnInit(); this.ensureUpToDate(); let validators: ValidatorFn[] = []; if (!this.inFormBuilder) { validators = this.validatorService.getValidatorsForSimpleComponent( this.comp, false, false, false, this.translations ); } this.setFormGroup(this.data, validators); this.options = this.formHelperService.getDecisionOptions( this.allowRecused, this.recuseValue, this.truthyValue, this.falsyValue ); } ensureUpToDate () { const decision = this.data as any; if (decision === true) { this.decisionChange(FormDecisionTypes.Approve); } else if (decision === false) { this.decisionChange(FormDecisionTypes.Decline); } } decisionChange (value: FormDecisionTypes) { this.onValueChange.emit({ value, updateFormGroup: false }); } patchValue () { const control = this.formGroup?.get(this.compKey); if (control) { control.setValue(this.data); } } }