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

67.64% Statements 23/34
36.36% Branches 4/11
66.66% Functions 4/6
67.64% Lines 23/34

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 1325x                   5x 5x   5x   5x 5x   5x                             5x                   3x   3x     3x               3x 3x 3x       3x     3x                                           3x   3x                 2x           2x         2x                       2x 2x                  
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 { 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)">
   </ish-order-template-preferences-dialog>
 */
@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() submitOrderTemplate = new EventEmitter<OrderTemplate>();
 
  orderTemplateForm = new FormGroup({});
  model: Partial<OrderTemplate>;
  fields: FormlyFieldConfig[];
  private submitted = false;
 
  /**
   *  A reference to the current modal.
   */
  modal: NgbModalRef;
 
  /** 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', { static: false }) modalTemplate: TemplateRef<unknown>;
 
  constructor(private ngbModal: NgbModal) {}
 
  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');
 
    Iif (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() {
    Iif (this.orderTemplateForm.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.orderTemplateForm);
      return;
    }
 
    this.submitOrderTemplate.emit({
      id: !this.orderTemplate ? this.model.title : this.orderTemplateTitle,
      title: this.model.title,
    });
 
    this.hide();
  }
 
  /** Opens the modal. */
  show() {
    this.orderTemplateForm.reset();
    this.model = pick(this.orderTemplate, 'title');
    this.modal = this.ngbModal.open(this.modalTemplate);
  }
 
  /** Close the modal. */
  hide() {
    this.submitted = false;
    Iif (this.modal) {
      this.modal.close();
    }
  }
 
  get formDisabled() {
    return this.orderTemplateForm.invalid && this.submitted;
  }
}