All files / src/app/extensions/wishlists/shared/select-wishlist-form select-wishlist-form.component.ts

100% Statements 17/17
80% Branches 8/10
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 1112x 2x   2x   2x     2x   2x             2x         13x           13x     13x     13x                                         13x   9x                     8x                                                                 28x                  
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 { WishlistsFacade } from '../../facades/wishlists.facade';
 
@Component({
  selector: 'ish-select-wishlist-form',
  templateUrl: './select-wishlist-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SelectWishlistFormComponent implements OnInit {
  @Input({ required: true }) formGroup: FormGroup;
  /**
   * changes the some logic and the translations keys between add or move a product (default: 'add')
   */
  @Input() addMoveProduct: 'add' | 'move' = 'add';
 
  singleFieldConfig: FormlyFieldConfig[];
  multipleFieldConfig$: Observable<FormlyFieldConfig[]>;
  wishlistOptions$: Observable<SelectOption[]>;
 
  constructor(private translate: TranslateService, private wishlistFacade: WishlistsFacade) {}
 
  ngOnInit() {
    this.wishlistOptions$ = this.wishlistFacade.wishlistSelectOptions$(this.addMoveProduct === 'move');
 
    // formly config for the single input field form (no or no other wishlists exist)
    this.singleFieldConfig = [
      {
        type: 'ish-text-input-field',
        key: 'newList',
        defaultValue: this.translate.instant('account.wishlists.choose_wishlist.new_wishlist_name.initial_value'),
        props: {
          required: true,
        },
        validators: {
          validation: [SpecialValidators.noHtmlTags],
        },
        validation: {
          messages: {
            required: 'account.wishlist.name.error.required',
            noHtmlTags: 'account.name.error.forbidden.html.chars',
          },
        },
      },
    ];
 
    // formly config for the radio button form (one or more other wishlists exist)
    this.multipleFieldConfig$ = this.wishlistOptions$.pipe(
      map(wishlistOptions =>
        wishlistOptions.map(option => ({
          type: 'ish-radio-field',
          key: 'wishlist',
          defaultValue: this.formGroup.get('wishlist')?.value || wishlistOptions[0].value,
          props: {
            fieldClass: ' ',
            value: option.value,
            label: option.label,
          },
        }))
      ),
      map(formlyConfig => [
        ...formlyConfig,
        {
          fieldGroupClassName: 'form-check d-flex',
          fieldGroup: [
            {
              type: 'ish-radio-field',
              key: 'wishlist',
              props: {
                inputClass: 'position-static',
                fieldClass: ' ',
                value: 'new',
              },
            },
            {
              type: 'ish-text-input-field',
              key: 'newList',
              className: 'w-75 position-relative validation-offset-0',
              wrappers: ['validation'],
              defaultValue: this.translate.instant('account.wishlists.choose_wishlist.new_wishlist_name.initial_value'),
              props: {
                required: true,
              },
              validators: {
                validation: [SpecialValidators.noHtmlTags],
              },
              validation: {
                messages: {
                  required: 'account.wishlist.name.error.required',
                  noHtmlTags: 'account.name.error.forbidden.html.chars',
                },
              },
              expressions: {
                'props.disabled': conf => conf.model.wishlist !== 'new',
              },
            },
          ],
        },
      ])
    );
  }
}