File

feature-libs/cart/saved-cart/components/details/saved-cart-details-items/saved-cart-details-items.component.ts

Implements

OnInit OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-saved-cart-details-items
templateUrl ./saved-cart-details-items.component.html

Index

Properties
Methods

Constructor

constructor(savedCartDetailsService: SavedCartDetailsService, savedCartService: SavedCartFacade, eventSercvice: EventService, globalMessageService: GlobalMessageService, routingService: RoutingService)
Parameters :
Name Type Optional
savedCartDetailsService SavedCartDetailsService No
savedCartService SavedCartFacade No
eventSercvice EventService No
globalMessageService GlobalMessageService No
routingService RoutingService No

Methods

ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onDeleteComplete
onDeleteComplete(success: boolean)
Parameters :
Name Type Optional
success boolean No
Returns : void

Properties

cartLoaded$
Type : Observable<boolean>
Default value : this.savedCartDetailsService .getSavedCartId() .pipe(switchMap((cartId) => this.savedCartService.isStable(cartId)))
CartLocation
Default value : PromotionLocation
savedCart$
Type : Observable<Cart | undefined>
Default value : this.savedCartDetailsService .getCartDetails() .pipe( tap((cart) => { if ((cart?.entries ?? []).length <= 0 && !!cart?.code) { this.savedCartService.deleteSavedCart(cart.code); } }) )
Private subscription
Default value : new Subscription()
import {
  ChangeDetectionStrategy,
  Component,
  OnDestroy,
  OnInit,
} from '@angular/core';
import {
  DeleteSavedCartSuccessEvent,
  SavedCartFacade,
} from '@spartacus/cart/saved-cart/root';
import {
  Cart,
  EventService,
  GlobalMessageService,
  GlobalMessageType,
  PromotionLocation,
  RoutingService,
} from '@spartacus/core';
import { Observable, Subscription } from 'rxjs';
import { mapTo, switchMap, take, tap } from 'rxjs/operators';
import { SavedCartDetailsService } from '../saved-cart-details.service';

@Component({
  selector: 'cx-saved-cart-details-items',
  templateUrl: './saved-cart-details-items.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SavedCartDetailsItemsComponent implements OnInit, OnDestroy {
  private subscription = new Subscription();

  CartLocation = PromotionLocation;

  cartLoaded$: Observable<boolean> = this.savedCartDetailsService
    .getSavedCartId()
    .pipe(switchMap((cartId) => this.savedCartService.isStable(cartId)));

  savedCart$: Observable<Cart | undefined> = this.savedCartDetailsService
    .getCartDetails()
    .pipe(
      tap((cart) => {
        if ((cart?.entries ?? []).length <= 0 && !!cart?.code) {
          this.savedCartService.deleteSavedCart(cart.code);
        }
      })
    );

  constructor(
    protected savedCartDetailsService: SavedCartDetailsService,
    protected savedCartService: SavedCartFacade,
    protected eventSercvice: EventService,
    protected globalMessageService: GlobalMessageService,
    protected routingService: RoutingService
  ) {}

  ngOnInit(): void {
    this.subscription.add(
      this.eventSercvice
        .get(DeleteSavedCartSuccessEvent)
        .pipe(take(1), mapTo(true))
        .subscribe((success) => this.onDeleteComplete(success))
    );
  }

  onDeleteComplete(success: boolean): void {
    if (success) {
      this.routingService.go({ cxRoute: 'savedCarts' });
      this.globalMessageService.add(
        {
          key: 'savedCartDialog.deleteCartSuccess',
        },
        GlobalMessageType.MSG_TYPE_CONFIRMATION
      );
    }
  }

  ngOnDestroy(): void {
    this.subscription?.unsubscribe();
  }
}
<ng-container *ngIf="savedCart$ | async as cart">
  <ng-container *ngIf="cart?.entries?.length > 0; else emptyCartItems">
    <div class="cart-details-wrapper">
      <cx-cart-item-list
        [cartId]="cart.code"
        [cartIsLoading]="!(cartLoaded$ | async)"
        [items]="cart.entries"
        [promotionLocation]="CartLocation.SavedCart"
      ></cx-cart-item-list>
    </div>
  </ng-container>
</ng-container>
<ng-template #emptyCartItems>
  <div class="cx-spinner">
    <cx-spinner></cx-spinner>
  </div>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""