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

84.61% Statements 22/26
46.15% Branches 6/13
91.66% Functions 11/12
87.5% Lines 21/24

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  2x 2x         2x   2x               2x     3x       3x     3x     2x 2x 2x       3x       2x                                                     2x 2x                 1x           1x         1x       2x       1x       3x      
/* eslint-disable unicorn/no-null */
import { ChangeDetectionStrategy, Component, Input, OnDestroy, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { Observable } from 'rxjs';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
import { PunchoutFacade } from '../../../facades/punchout.facade';
import { CxmlConfiguration } from '../../../models/cxml-configuration/cxml-configuration.model';
 
@Component({
  selector: 'ish-cxml-configuration-form',
  templateUrl: './cxml-configuration-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CxmlConfigurationFormComponent implements OnDestroy, OnInit {
  @Input({ required: true }) cxmlConfiguration: CxmlConfiguration[];
 
  form: FormGroup = new FormGroup({});
  model: { [key: string]: string };
  fields: FormlyFieldConfig[];
 
  private submitted = false;
  cxmlConfigurationError$: Observable<HttpError>;
 
  constructor(private punchoutFacade: PunchoutFacade) {}
 
  ngOnInit() {
    this.cxmlConfigurationError$ = this.punchoutFacade.cxmlConfigurationError$;
    this.fields = this.getFields();
    this.model = this.getModel();
  }
 
  ngOnDestroy(): void {
    this.resetConfiguration();
  }
 
  private getFields() {
    return this.cxmlConfiguration.map(cxmlConfig => ({
      fieldGroupClassName: 'row list-item-row mb-0',
      fieldGroup: [
        {
          key: cxmlConfig.name.replaceAll('.', ':'),
          type: cxmlConfig.inputType === 'text-long' ? 'ish-textarea-field' : 'ish-text-input-field',
          wrappers: ['cxml-help-text', 'form-field-horizontal', 'description'],
          className: 'list-item col-md-12',
          props: {
            rows: 3,
            label: cxmlConfig.name,
            labelNoTranslate: true,
            placeholder: cxmlConfig.defaultValue,
            helpText: cxmlConfig.description,
            customDescription: {
              key: 'account.punchout.cxml.configuration.default.description',
              args: { defaultValue: cxmlConfig.defaultValue },
            },
            fieldClass: 'col-sm-7 pl-md-0',
            labelClass: 'col-sm-5 mr-sm-0 pl-5',
          },
        },
      ],
    }));
  }
 
  private getModel() {
    return this.cxmlConfiguration.reduce(
      (acc, config) => ({
        ...acc,
        [config.name.replaceAll('.', ':')]: config.value === config.defaultValue ? null : config.value,
      }),
      {}
    );
  }
 
  submitForm() {
    Iif (this.form.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.form);
      return;
    }
 
    const updateData = Object.entries(this.form.value).map(([name, value]) => ({
      name: name.replaceAll(':', '.'),
      value: value ? value : null,
    }));
 
    this.updateConfiguration(updateData as CxmlConfiguration[]);
  }
 
  get formDisabled() {
    return this.form.invalid && this.submitted;
  }
 
  private updateConfiguration(cxmlConfig: CxmlConfiguration[]) {
    this.punchoutFacade.updateCxmlConfiguration(cxmlConfig);
  }
 
  private resetConfiguration() {
    this.punchoutFacade.resetCxmlConfiguration();
  }
}