All files / src/app/extensions/order-templates/shared/basket-create-order-template basket-create-order-template.component.ts

72.22% Statements 13/18
64.28% Branches 9/14
50% Functions 2/4
72.22% Lines 13/18

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 531x 1x 1x 1x   1x   1x   1x                           1x     3x     3x 3x 3x                                   1x      
import { ChangeDetectionStrategy, Component, DestroyRef, Input, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Router } from '@angular/router';
import { take } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { LineItemView } from 'ish-core/models/line-item/line-item.model';
import { GenerateLazyComponent } from 'ish-core/utils/module-loader/generate-lazy-component.decorator';
 
import { OrderTemplatesFacade } from '../../facades/order-templates.facade';
import { OrderTemplate } from '../../models/order-template/order-template.model';
import { OrderTemplatePreferencesDialogComponent } from '../order-template-preferences-dialog/order-template-preferences-dialog.component';
 
@Component({
  selector: 'ish-basket-create-order-template',
  templateUrl: './basket-create-order-template.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
/**
 * The Basket Create Order Template displays a button which adds the current cart to to a new order template.
 */
 
@GenerateLazyComponent()
export class BasketCreateOrderTemplateComponent {
  @Input() products: LineItemView[];
  @Input() cssClass: string;
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private orderTemplatesFacade: OrderTemplatesFacade,
    private accountFacade: AccountFacade,
    private router: Router
  ) {}
  /**
   * if the user is not logged in display login dialog, else open select order template dialog
   */
  openModal(modal: OrderTemplatePreferencesDialogComponent) {
    this.accountFacade.isLoggedIn$.pipe(take(1), takeUntilDestroyed(this.destroyRef)).subscribe(isLoggedIn => {
      if (isLoggedIn) {
        modal.show();
      } else {
        // stay on the same page after login
        const queryParams = { returnUrl: this.router.routerState.snapshot.url, messageKey: 'ordertemplates' };
        this.router.navigate(['/login'], { queryParams });
      }
    });
  }
 
  createOrderTemplate(orderTemplate: OrderTemplate) {
    this.orderTemplatesFacade.createOrderTemplateFromLineItems(orderTemplate, this.products);
  }
}