All files / src/app/extensions/punchout/pages/account-punchout-configuration/oci-configuration-form oci-configuration-form.component.ts

81.81% Statements 27/33
53.84% Branches 7/13
77.77% Functions 7/9
81.25% Lines 26/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 1712x 2x 2x   2x 2x       2x 2x   2x               2x 4x 4x                         4x   4x     4x 4x 4x 4x   4x 4x 4x 4x                                         4x 4x                                                                                                                                                                               1x         1x       4x      
import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { TranslateService } from '@ngx-translate/core';
import { Observable, filter, map, shareReplay, take } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { SelectOption } from 'ish-core/models/select-option/select-option.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
import { PunchoutFacade } from '../../../facades/punchout.facade';
import { OciConfigurationItem } from '../../../models/oci-configuration-item/oci-configuration-item.model';
 
@Component({
  selector: 'ish-oci-configuration-form',
  templateUrl: './oci-configuration-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OciConfigurationFormComponent implements OnInit {
  form: FormGroup = new FormGroup({});
  private submitted = false;
 
  configItems$: Observable<OciConfigurationItem[]>;
  error$: Observable<HttpError>;
  loading$: Observable<boolean>;
 
  private formatterOptions$: Observable<SelectOption[]>;
  availablePlaceholders$: Observable<string[]>;
 
  model$: Observable<{ ociConfig: OciConfigurationItem[] }>;
  fields: FormlyFieldConfig[];
  fields$: Observable<FormlyFieldConfig[]>;
 
  private destroyRef = inject(DestroyRef);
 
  constructor(private punchoutFacade: PunchoutFacade, private translate: TranslateService) {}
 
  ngOnInit() {
    this.error$ = this.punchoutFacade.ociConfigurationError$;
    this.loading$ = this.punchoutFacade.ociConfigurationLoading$;
    this.formatterOptions$ = this.punchoutFacade.ociFormatterSelectOptions$;
    this.availablePlaceholders$ = this.punchoutFacade.ociPlaceholders$;
 
    this.fields$ = this.getFields();
    this.configItems$ = this.punchoutFacade.ociConfiguration$().pipe(shareReplay(1));
    this.model$ = this.configItems$.pipe(
      filter(configItems => configItems?.length > 0),
      take(1),
      map(this.getModel),
      takeUntilDestroyed(this.destroyRef)
    );
  }
 
  private getModel(items: OciConfigurationItem[]): { ociConfig: OciConfigurationItem[] } {
    const config: OciConfigurationItem[] = items?.map(
      item =>
        !item.mappings
          ? { ...item, mappings: [{ mapFromValue: '', mapToValue: '' }] }
          : { ...item, mappings: JSON.parse(JSON.stringify(item.mappings)) } // necessary to make the object extensible
    );
 
    return {
      ociConfig: config,
    };
  }
 
  private getFields(): Observable<FormlyFieldConfig[]> {
    return this.formatterOptions$.pipe(
      map(options => [
        {
          key: 'ociConfig',
          type: 'repeat-oci-config',
          fieldArray: {
            fieldGroupClassName: 'row list-item-row mb-0',
            fieldGroup: [
              {
                key: 'field',
                type: 'ish-plain-text-field',
                className: 'list-item col-md-3',
                props: {
                  fieldClass: 'col-md-11',
                  inputClass: 'col-form-label',
                },
              },
              {
                key: 'transform',
                type: 'ish-text-input-field',
                className: 'list-item col-md-3',
                props: {
                  fieldClass: 'col-md-11',
                  ariaLabel: this.translate.instant('account.punchout.oci.transform.aria_label'),
                },
              },
              {
                key: 'mappings',
                className: 'oci-configuration-mappings list-item col-md-3',
                type: 'repeat-oci-configuration-mapping',
                fieldArray: {
                  fieldGroupClassName: 'oci-configuration-mappings-group mb-3',
                  validators: {
                    validation: [
                      SpecialValidators.dependentlyRequired('mapToValue', 'mapFromValue'),
                      SpecialValidators.dependentlyRequired('mapFromValue', 'mapToValue'),
                    ],
                  },
                  fieldGroup: [
                    {
                      key: 'mapFromValue',
                      type: 'ish-text-input-field',
                      wrappers: ['oci-configuration-mapping-wrapper', 'validation'],
                      validation: {
                        messages: {
                          dependentlyRequired: 'account.punchout.configuration.form.mapping.from.error',
                        },
                      },
                      props: {
                        ariaLabel: this.translate.instant('account.punchout.oci.map_from.aria_label'),
                      },
                    },
                    {
                      key: 'mapToValue',
                      type: 'ish-text-input-field',
                      wrappers: ['oci-configuration-mapping-wrapper', 'validation'],
                      props: {
                        fieldClass: 'ml-1',
                        arrowRight: true,
                        ariaLabel: this.translate.instant('account.punchout.oci.map_to.aria_label'),
                      },
                      validation: {
                        messages: {
                          dependentlyRequired: 'account.punchout.configuration.form.mapping.to.error',
                        },
                      },
                    },
                  ],
                },
              },
 
              {
                key: 'formatter',
                type: 'ish-select-field',
                className: 'list-item col-md-3',
                props: {
                  fieldClass: 'col-12',
                  options,
                  ariaLabel: this.translate.instant('account.punchout.oci.formatter.aria_label'),
                },
              },
            ],
          },
        },
      ])
    );
  }
 
  submitForm() {
    Iif (this.form.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.form);
      return;
    }
    this.punchoutFacade.updateOciConfiguration(this.form.value.ociConfig);
  }
 
  get formDisabled() {
    return this.form.invalid && this.submitted;
  }
}