All files / src/app/extensions/wishlists/shared/wishlist-preferences-dialog wishlist-preferences-dialog.component.ts

96.15% Statements 25/26
70% Branches 7/10
100% Functions 4/4
96% Lines 24/25

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 1202x 2x   2x     2x                                 2x                 3x   3x 3x               3x 3x 3x         3x                                                                       3x 1x 1x 1x           2x 1x           1x 1x             1x 1x     1x   1x      
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { pick } from 'lodash-es';
 
import { ModalDialogComponent } from 'ish-shared/components/common/modal-dialog/modal-dialog.component';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
import { Wishlist } from '../../models/wishlist/wishlist.model';
 
/**
 * The Wishlist Preferences Dialog shows the modal to create/edit a wishlist.
 *
 * @example
 * <ish-account-wishlist-preferences-dialog
    [wishlist]="wishlist"
    (submitWishlist)="createWishlist($event)" />
 */
@Component({
  selector: 'ish-wishlist-preferences-dialog',
  templateUrl: './wishlist-preferences-dialog.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class WishlistPreferencesDialogComponent implements OnInit {
  /**
   * Predefined wishlist to fill the form with, if there is no wishlist a new wishlist will be created
   */
  @Input() wishlist: Wishlist;
 
  /**
   * Emits the data of the new wishlist to create.
   */
  @Output() readonly submitWishlist = new EventEmitter<Wishlist>();
 
  wishListForm = new FormGroup({});
  model: Partial<Wishlist> = { preferred: false };
  fields: FormlyFieldConfig[];
 
  /**
   *  A reference to the current modal.
   */
  // localization keys, default = for new
 
  primaryButton = 'account.wishlists.new_wishlist_form.create_button.text';
  private wishlistTitle = 'account.wishlists.choose_wishlist.new_wishlist_name.initial_value';
  modalHeader = 'account.wishlists.new_wishlist_dialog.header';
 
  @ViewChild('modal') modalDialog: ModalDialogComponent<unknown>;
 
  ngOnInit() {
    this.fields = [
      {
        key: 'title',
        type: 'ish-text-input-field',
        props: {
          required: true,
          label: 'account.wishlists.wishlist_form.name.label',
          hideRequiredMarker: true,
          maxLength: 35,
        },
        validators: {
          validation: [SpecialValidators.noHtmlTags],
        },
        validation: {
          messages: {
            required: 'account.wishlists.wishlist_form.name.error.required',
            noHtmlTags: 'account.name.error.forbidden.html.chars',
          },
        },
      },
      {
        key: 'preferred',
        type: 'ish-checkbox-field',
        props: {
          label: 'account.wishlists.wishlist_form.preferred.label',
          fieldClass: 'offset-md-4 col-md-8 d-flex align-items-center',
          tooltip: {
            link: 'account.wishlists.wishlist_form.preferred.tooltip.linktext',
            text: 'account.wishlists.wishlist_form.preferred.tooltip.content',
            title: 'account.wishlists.wishlist_form.preferred.tooltip.headline',
            class: 'details-tooltip',
          },
        },
      },
    ];
 
    if (this.wishlist) {
      this.primaryButton = 'account.wishlists.edit_wishlist_form.save_button.text';
      this.wishlistTitle = this.wishlist.title;
      this.modalHeader = 'account.wishlist.list.edit';
    }
  }
 
  /** Emits the wishlist data, when the form was valid. */
  submitWishlistForm() {
    if (this.wishListForm.valid) {
      this.submitWishlist.emit({
        id: !this.wishlist ? this.model.title : this.wishlistTitle,
        preferred: this.model.preferred,
        title: this.model.title,
        public: false,
      });
      Eif (this.modalDialog) {
        this.modalDialog.hide();
      }
    }
  }
 
  /** Opens the modal. */
  show() {
    this.wishListForm.reset();
    Iif (!this.wishlist) {
      this.model = { preferred: false };
    } else {
      this.model = pick(this.wishlist, 'title', 'preferred');
    }
    this.modalDialog.show();
  }
}