All files / src/app/extensions/order-templates/shared/order-template-preferences-dialog order-template-preferences-dialog.component.ts

86.95% Statements 20/23
83.33% Branches 5/6
75% Functions 3/4
86.36% Lines 19/22

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 974x 4x   4x     4x                               4x                   3x   3x         3x 3x 3x         3x                                           3x   3x 1x 1x 1x           2x 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 { OrderTemplate } from '../../models/order-template/order-template.model';
 
/**
 * The Order Templates Preferences Dialog shows the modal to create/edit a order template.
 *
 * @example
 * <ish-order-template-preferences-dialog
    (submitOrderTemplate)="createOrderTemplate($event)" />
 */
@Component({
  selector: 'ish-order-template-preferences-dialog',
  templateUrl: './order-template-preferences-dialog.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OrderTemplatePreferencesDialogComponent implements OnInit {
  /**
   * Predefined order template to fill the form with, if there is no order template a new order template will be created
   */
  @Input() orderTemplate: OrderTemplate;
 
  @Input() modalTitle: string;
  /**
   * Emits the data of the new order template to create.
   */
  @Output() readonly submitOrderTemplate = new EventEmitter<OrderTemplate>();
 
  orderTemplateForm = new FormGroup({});
  model: Partial<OrderTemplate>;
  fields: FormlyFieldConfig[];
 
  /** localization keys, default = for new */
  primaryButton = 'account.order_template.new_from_order.button.create.label';
  private orderTemplateTitle = 'account.order_template.new_order_template.text';
  modalHeader = 'account.order_template.list.button.add_template.label';
 
  @ViewChild('modal') modalDialog: ModalDialogComponent<unknown>;
 
  ngOnInit() {
    this.fields = [
      {
        key: 'title',
        type: 'ish-text-input-field',
        props: {
          required: true,
          label: 'account.order_template.form.name.label',
          hideRequiredMarker: true,
          maxLength: 35,
        },
        validators: {
          validation: [SpecialValidators.noHtmlTags],
        },
        validation: {
          messages: {
            required: 'account.order_template.form.name.error.required',
            noHtmlTags: 'account.name.error.forbidden.html.chars',
          },
        },
      },
    ];
 
    this.model = pick(this.orderTemplate, 'title');
 
    if (this.orderTemplate) {
      this.primaryButton = 'account.order_templates.edit_form.save_button.text';
      this.orderTemplateTitle = this.orderTemplate.title;
      this.modalHeader = 'account.order_template.edit.heading';
    }
  }
 
  /** Emits the order template data, when the form was valid. */
  submitOrderTemplateForm() {
    if (this.orderTemplateForm.valid) {
      this.submitOrderTemplate.emit({
        id: !this.orderTemplate ? this.model.title : this.orderTemplateTitle,
        title: this.model.title,
      });
 
      this.modalDialog.hide();
    }
  }
 
  /** Opens the modal. */
  show() {
    this.orderTemplateForm.reset();
    this.model = pick(this.orderTemplate, 'title');
    this.modalDialog.show();
  }
}