All files / src/app/extensions/order-templates/shared/select-order-template-form select-order-template-form.component.ts

100% Statements 17/17
75% Branches 6/8
100% Functions 6/6
100% Lines 15/15

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 1152x 2x   2x   2x     2x   2x             2x           11x             11x     11x         11x                                         11x   9x                     9x                                                                 30x                  
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { SelectOption } from 'ish-core/models/select-option/select-option.model';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
import { OrderTemplatesFacade } from '../../facades/order-templates.facade';
 
@Component({
  selector: 'ish-select-order-template-form',
  templateUrl: './select-order-template-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SelectOrderTemplateFormComponent implements OnInit {
  @Input() formGroup: FormGroup;
 
  /**
   * changes the some logic and the translations keys between add or move a product (default: 'add')
   */
  @Input() addMoveProduct: 'add' | 'move' = 'add';
 
  orderTemplatesOptions$: Observable<SelectOption[]>;
 
  singleFieldConfig: FormlyFieldConfig[];
  multipleFieldConfig$: Observable<FormlyFieldConfig[]>;
 
  constructor(private orderTemplatesFacade: OrderTemplatesFacade, private translate: TranslateService) {}
 
  ngOnInit(): void {
    this.orderTemplatesOptions$ = this.orderTemplatesFacade.orderTemplatesSelectOptions$(
      this.addMoveProduct === 'move'
    );
 
    // formly config for the single input field form (no or no other order templates exist)
    this.singleFieldConfig = [
      {
        type: 'ish-text-input-field',
        key: 'newOrderTemplate',
        defaultValue: this.translate.instant('account.order_template.new_order_template.text'),
        props: {
          required: true,
        },
        validators: {
          validation: [SpecialValidators.noHtmlTags],
        },
        validation: {
          messages: {
            required: 'account.order_template.name.error.required',
            noHtmlTags: 'account.name.error.forbidden.html.chars',
          },
        },
      },
    ];
 
    // formly config for the radio button form (one or more other order templates exist)
    this.multipleFieldConfig$ = this.orderTemplatesOptions$.pipe(
      map(orderTemplateOptions =>
        orderTemplateOptions.map(option => ({
          type: 'ish-radio-field',
          key: 'orderTemplate',
          defaultValue: orderTemplateOptions[0].value,
          props: {
            fieldClass: ' ',
            value: option.value,
            label: option.label,
          },
        }))
      ),
      map(formlyConfig => [
        ...formlyConfig,
        {
          fieldGroupClassName: 'form-check d-flex',
          fieldGroup: [
            {
              type: 'ish-radio-field',
              key: 'orderTemplate',
              props: {
                inputClass: 'position-static',
                fieldClass: ' ',
                value: 'new',
              },
            },
            {
              type: 'ish-text-input-field',
              key: 'newOrderTemplate',
              className: 'w-75 position-relative validation-offset-0',
              wrappers: ['validation'],
              defaultValue: this.translate.instant('account.order_template.new_order_template.text'),
              props: {
                required: true,
              },
              validators: {
                validation: [SpecialValidators.noHtmlTags],
              },
              validation: {
                messages: {
                  required: 'account.order_template.name.error.required',
                  noHtmlTags: 'account.name.error.forbidden.html.chars',
                },
              },
              expressions: {
                'props.disabled': conf => conf.model.orderTemplate !== 'new',
              },
            },
          ],
        },
      ])
    );
  }
}