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

72.22% Statements 26/36
38.46% Branches 5/13
66.66% Functions 4/6
72.22% Lines 26/36

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 1502x                   2x 2x   2x   2x 2x   2x                               2x                 3x   3x 3x   3x                 3x 3x 3x       3x     3x                                                                       3x                 2x 1x 1x 1x     1x           1x                               1x 1x                  
import {
  ChangeDetectionStrategy,
  Component,
  EventEmitter,
  Input,
  OnInit,
  Output,
  TemplateRef,
  ViewChild,
} from '@angular/core';
import { FormGroup } from '@angular/forms';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { pick } from 'lodash-es';
 
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
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)">
   </ish-account-wishlist-preferences-dialog>
 */
@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() submitWishlist = new EventEmitter<Wishlist>();
 
  wishListForm = new FormGroup({});
  model: Partial<Wishlist> = { preferred: false };
  fields: FormlyFieldConfig[];
  private submitted = false;
 
  /**
   *  A reference to the current modal.
   */
  modal: NgbModalRef;
 
  // 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') modalTemplate: TemplateRef<unknown>;
 
  constructor(private ngbModal: NgbModal) {}
 
  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',
          },
        },
      },
    ];
 
    Iif (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.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.wishListForm);
      return;
    }
 
    this.submitWishlist.emit({
      id: !this.wishlist ? this.model.title : this.wishlistTitle,
      preferred: this.model.preferred,
      title: this.model.title,
      public: false,
    });
    this.hide();
  }
 
  /** Opens the modal. */
  show() {
    this.wishListForm.reset();
    if (!this.wishlist) {
      this.model = { preferred: false };
    } else {
      this.model = pick(this.wishlist, 'title', 'preferred');
    }
    this.modal = this.ngbModal.open(this.modalTemplate);
  }
 
  /** Close the modal. */
  hide() {
    this.submitted = false;
    Iif (this.modal) {
      this.modal.close();
    }
  }
 
  get formDisabled() {
    return this.wishListForm.invalid && this.submitted;
  }
}