All files / src/app/extensions/order-templates/shared/select-order-template-modal select-order-template-modal.component.ts

90% Statements 54/60
77.77% Branches 28/36
84% Functions 21/25
89.65% Lines 52/58

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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 1903x                       3x 3x 3x 3x 3x 3x     3x   3x                   3x       11x         11x       11x               11x         11x 11x 11x       11x         6x 6x 2x 2x       4x 2x   2x         2x   2x 2x         2x       2x       2x       2x                     10x 10x     10x   10x 8x         8x 8x                           2x 2x 1x   1x 1x 1x               2x   2x 1x 1x       1x           76x             52x                        
import {
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  EventEmitter,
  Input,
  OnInit,
  Output,
  TemplateRef,
  ViewChild,
  inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, FormGroup } from '@angular/forms';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { TranslateService } from '@ngx-translate/core';
import { Observable, of } from 'rxjs';
import { filter, map, take } from 'rxjs/operators';
 
import { SelectOption } from 'ish-core/models/select-option/select-option.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
import { OrderTemplatesFacade } from '../../facades/order-templates.facade';
 
/**
 * The order template select modal displays a list of order templates. The user can select one order template  or enter a name for a new order template  in order to add or move an item to the selected order template .
 */
@Component({
  selector: 'ish-select-order-template-modal',
  templateUrl: './select-order-template-modal.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SelectOrderTemplateModalComponent implements OnInit {
  /**
   * changes the some logic and the translations keys between add or move a product (default: 'add')
   */
  @Input() addMoveProduct: 'add' | 'move' = 'add';
 
  /**
   * submit success event
   */
  @Output() submitEmitter = new EventEmitter<{ id: string; title: string }>();
 
  private orderTemplateOptions$: Observable<SelectOption[]>;
 
  formGroup: FormGroup = new FormGroup({
    orderTemplate: new FormControl(''),
    newOrderTemplate: new FormControl(''),
  });
 
  showForm: boolean;
  modal: NgbModalRef;
 
  private destroyRef = inject(DestroyRef);
 
  @ViewChild('modal', { static: false }) modalTemplate: TemplateRef<unknown>;
 
  constructor(
    private ngbModal: NgbModal,
    private orderTemplatesFacade: OrderTemplatesFacade,
    private translate: TranslateService
  ) {}
 
  ngOnInit() {
    this.orderTemplateOptions$ = this.orderTemplatesFacade.orderTemplatesSelectOptions$(this.addMoveProduct === 'move');
  }
 
  /** emit results when the form is valid */
  submitForm() {
    const radioButtons = this.formGroup.value;
    if (radioButtons?.orderTemplate && radioButtons.orderTemplate !== 'new') {
      if (this.formGroup.valid) {
        this.submitExisting(radioButtons.orderTemplate);
      } else E{
        markAsDirtyRecursive(this.formGroup);
      }
    } else if (radioButtons.newOrderTemplate && this.formGroup.valid) {
      this.submitNew(radioButtons.newOrderTemplate);
    } else {
      markAsDirtyRecursive(this.formGroup);
    }
  }
 
  private submitExisting(orderTemplateId: string) {
    this.orderTemplateOptions$
      .pipe(
        filter(options => options.length > 0),
        map(options => options.find(option => option.value === orderTemplateId).label),
        take(1),
        takeUntilDestroyed(this.destroyRef)
      )
      .subscribe(label => {
        this.submitEmitter.emit({
          id: orderTemplateId,
          title: label,
        });
        this.showForm = false;
      });
  }
  private submitNew(newOrderTemplate: string) {
    this.submitEmitter.emit({
      id: undefined,
      title: newOrderTemplate,
    });
    this.showForm = false;
  }
 
  /** close modal */
  hide() {
    this.modal.close();
    this.formGroup.reset();
  }
 
  /** open modal */
  show() {
    this.showForm = true;
    this.modal = this.ngbModal.open(this.modalTemplate);
 
    // set default values after init (for example after closing and reopening the modal)
    this.orderTemplateOptions$
      .pipe(
        filter(opts => opts.length > 0),
        map(options => options[0]),
        take(1),
        takeUntilDestroyed(this.destroyRef)
      )
      .subscribe(option => {
        const defaultValue = this.translate.instant('account.order_template.new_order_template.text');
        this.formGroup.patchValue({ orderTemplate: option.value, newOrderTemplate: defaultValue });
      });
  }
 
  /**
   * Callback function to hide modal dialog (used with ishServerHtml). - is needed for closing the dialog after the user clicks a message link
   */
  get callbackHideDialogModal() {
    return () => {
      this.hide();
    };
  }
 
  get selectedOrderTemplateTitle$(): Observable<string> {
    const selectedValue = this.formGroup.value.orderTemplate;
    if (selectedValue === 'new' || !selectedValue) {
      return of(this.formGroup.value.newOrderTemplate);
    } else {
      return this.orderTemplateOptions$.pipe(
        filter(options => options.length > 0),
        map(options => options.find(opt => opt.value === selectedValue).label),
        take(1)
      );
    }
  }
 
  /** returns the route to the selected order template */
  get selectedOrderTemplateRoute$(): Observable<string> {
    const selectedValue = this.formGroup.value.orderTemplate;
 
    if (selectedValue === 'new' || !selectedValue) {
      return this.orderTemplatesFacade.currentOrderTemplate$.pipe(
        map(currentOrderTemplate => `route://account/order-templates/${currentOrderTemplate?.id}`),
        take(1)
      );
    } else {
      return of(`route://account/order-templates/${selectedValue}`);
    }
  }
 
  /** translation key for the modal header */
  get headerTranslationKey() {
    return this.addMoveProduct === 'add'
      ? 'account.order_template.add_to_template.button.add_to_template.label'
      : 'account.order_template.table.options.move_to_template';
  }
 
  /** translation key for the submit button */
  get submitButtonTranslationKey() {
    return this.addMoveProduct === 'add'
      ? 'account.order_template.add_to_template.button.add_to_template.label'
      : 'account.order_template.table.options.move_to_template';
  }
 
  /** translation key for the success text */
  get successTranslationKey() {
    return this.addMoveProduct === 'add'
      ? 'account.order_template.added.confirmation'
      : 'account.order_template.move.added.text';
  }
}