All files / src/app/extensions/wishlists/shared/product-add-to-wishlist product-add-to-wishlist.component.ts

79.16% Statements 19/24
70% Branches 14/20
60% Functions 3/5
79.16% Lines 19/24

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 701x 1x 1x   1x   1x 1x 1x   1x                                 1x 3x         3x     3x 3x 3x 3x       3x                                     2x 1x   1x        
import { ChangeDetectionStrategy, Component, DestroyRef, Input, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { GenerateLazyComponent } from 'ish-core/utils/module-loader/generate-lazy-component.decorator';
 
import { WishlistsFacade } from '../../facades/wishlists.facade';
import { SelectWishlistModalComponent } from '../select-wishlist-modal/select-wishlist-modal.component';
 
@Component({
  selector: 'ish-product-add-to-wishlist',
  templateUrl: './product-add-to-wishlist.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
/**
 * The Product Add To Wishlist Component adds a product to a wishlist.
 *
 * @example
 * <ish-product-add-to-wishlist
 *               displayType="icon"
 * ></ish-product-add-to-wishlist>
 */
@GenerateLazyComponent()
export class ProductAddToWishlistComponent implements OnInit {
  @Input() displayType: 'icon' | 'link' | 'animated' = 'link';
  @Input() cssClass: string;
 
  visible$: Observable<boolean>;
 
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private wishlistsFacade: WishlistsFacade,
    private accountFacade: AccountFacade,
    private router: Router,
    private context: ProductContextFacade
  ) {}
 
  ngOnInit() {
    this.visible$ = this.context.select('displayProperties', 'addToWishlist');
  }
 
  /**
   * if the user is not logged in display login dialog, else open select wishlist dialog
   */
  openModal(modal: SelectWishlistModalComponent) {
    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: 'wishlists' };
        this.router.navigate(['/login'], { queryParams });
      }
    });
  }
 
  addProductToWishlist(wishlist: { id: string; title: string }) {
    if (!wishlist.id) {
      this.wishlistsFacade.addProductToNewWishlist(wishlist.title, this.context.get('sku'));
    } else {
      this.wishlistsFacade.addProductToWishlist(wishlist.id, this.context.get('sku'));
    }
  }
}