File

projects/storefrontlib/cms-components/cart/add-to-wishlist/add-to-wish-list.component.ts

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-add-to-wishlist
templateUrl ./add-to-wish-list.component.html

Index

Properties
Methods

Constructor

constructor(wishListService: WishListService, currentProductService: CurrentProductService, authService: AuthService)
Parameters :
Name Type Optional
wishListService WishListService No
currentProductService CurrentProductService No
authService AuthService No

Methods

add
add(product: Product)
Parameters :
Name Type Optional
product Product No
Returns : void
getProductInWishList
getProductInWishList(product: Product, entries: OrderEntry[])
Parameters :
Name Type Optional
product Product No
entries OrderEntry[] No
Returns : OrderEntry
remove
remove(entry: OrderEntry)
Parameters :
Name Type Optional
entry OrderEntry No
Returns : void
Protected setStockInfo
setStockInfo(product: Product)
Parameters :
Name Type Optional
product Product No
Returns : void

Properties

hasStock
Default value : false
iconTypes
Default value : ICON_TYPE
loading$
Type : Observable<boolean>
Default value : this.wishListService.getWishListLoading()
product$
Type : Observable<Product>
Default value : this.currentProductService.getProduct().pipe( filter(isNotNullable), tap((product) => this.setStockInfo(product)) )
userLoggedIn$
Type : Observable<boolean>
Default value : this.authService.isUserLoggedIn()
wishListEntries$
Type : Observable<OrderEntry[]>
Default value : this.wishListService .getWishList() .pipe( filter((wishlist) => Boolean(wishlist)), map((wishList) => wishList.entries) )
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  AuthService,
  isNotNullable,
  OrderEntry,
  Product,
  WishListService,
} from '@spartacus/core';
import { Observable } from 'rxjs';
import { filter, map, tap } from 'rxjs/operators';
import { ICON_TYPE } from '../../../cms-components/misc/icon/icon.model';
import { CurrentProductService } from '../../product/current-product.service';

@Component({
  selector: 'cx-add-to-wishlist',
  templateUrl: './add-to-wish-list.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AddToWishListComponent {
  product$: Observable<Product> = this.currentProductService.getProduct().pipe(
    filter(isNotNullable),
    tap((product) => this.setStockInfo(product))
  );

  wishListEntries$: Observable<OrderEntry[]> = this.wishListService
    .getWishList()
    .pipe(
      filter((wishlist) => Boolean(wishlist)),
      map((wishList) => wishList.entries)
    );

  userLoggedIn$: Observable<boolean> = this.authService.isUserLoggedIn();
  loading$: Observable<boolean> = this.wishListService.getWishListLoading();

  hasStock = false;
  iconTypes = ICON_TYPE;

  constructor(
    protected wishListService: WishListService,
    protected currentProductService: CurrentProductService,
    protected authService: AuthService
  ) {}

  add(product: Product): void {
    this.wishListService.addEntry(product.code);
  }

  remove(entry: OrderEntry): void {
    this.wishListService.removeEntry(entry);
  }

  getProductInWishList(product: Product, entries: OrderEntry[]): OrderEntry {
    const item = entries.find((entry) => entry.product.code === product.code);
    return item;
  }

  protected setStockInfo(product: Product): void {
    this.hasStock = Boolean(
      product.stock && product.stock.stockLevelStatus !== 'outOfStock'
    );
  }
}
<ng-container *ngIf="product$ | async as product">
  <ng-container *ngIf="userLoggedIn$ | async; else loginPrompt">
    <ng-container *ngIf="wishListEntries$ | async as entries">
      <ng-container *ngIf="hasStock">
        <div
          *ngIf="getProductInWishList(product, entries) as entry; else addItem"
        >
          <button
            class="btn btn-link button-remove cx-action-link"
            (click)="remove(entry)"
            [disabled]="loading$ | async"
          >
            <cx-icon [type]="iconTypes.HEART"></cx-icon>
            <span class="button-text">{{
              'addToWishList.remove' | cxTranslate
            }}</span>
          </button>
        </div>
        <ng-template #addItem>
          <button
            class="btn btn-link button-add cx-action-link"
            (click)="add(product)"
            [disabled]="loading$ | async"
          >
            <cx-icon [type]="iconTypes.EMPTY_HEART"></cx-icon>
            <span class="button-text">{{
              'addToWishList.add' | cxTranslate
            }}</span>
          </button>
        </ng-template>
      </ng-container>
    </ng-container>
  </ng-container>
</ng-container>

<ng-template #loginPrompt>
  <ng-container *ngIf="hasStock">
    <a
      class="btn btn-link button-add-link cx-action-link"
      [routerLink]="{ cxRoute: 'login' } | cxUrl"
    >
      <cx-icon [type]="iconTypes.EMPTY_HEART"></cx-icon>
      <span class="button-text">{{
        'addToWishList.anonymous' | cxTranslate
      }}</span>
    </a>
  </ng-container>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""