File

projects/storefrontlib/cms-components/cart/cart-totals/cart-totals.component.ts

Implements

OnInit OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-cart-totals
templateUrl ./cart-totals.component.html

Index

Properties
Methods

Constructor

constructor(activeCartService: ActiveCartService, router?: Router)
Parameters :
Name Type Optional
activeCartService ActiveCartService No
router Router Yes

Methods

disableButtonWhileNavigation
disableButtonWhileNavigation()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

cart$
Type : Observable<Cart>
cartValidationInProgress
Default value : false
entries$
Type : Observable<OrderEntry[]>
Protected subscription
Default value : new Subscription()
import {
  ChangeDetectionStrategy,
  Component,
  OnInit,
  OnDestroy,
} from '@angular/core';
import { ActiveCartService, Cart, OrderEntry } from '@spartacus/core';
import { Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import {
  Event,
  NavigationCancel,
  NavigationEnd,
  Router,
} from '@angular/router';

@Component({
  selector: 'cx-cart-totals',
  templateUrl: './cart-totals.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CartTotalsComponent implements OnInit, OnDestroy {
  cart$: Observable<Cart>;
  entries$: Observable<OrderEntry[]>;
  cartValidationInProgress = false;

  protected subscription = new Subscription();

  /**
   * @deprecated since 4.2
   */
  constructor(activeCartService: ActiveCartService);

  constructor(
    protected activeCartService: ActiveCartService,
    protected router?: Router
  ) {}

  ngOnInit() {
    this.cart$ = this.activeCartService.getActive();
    this.entries$ = this.activeCartService
      .getEntries()
      .pipe(filter((entries) => entries.length > 0));

    this.subscription.add(
      this.router?.events.subscribe((event: Event) => {
        if (
          event instanceof NavigationEnd ||
          event instanceof NavigationCancel
        ) {
          this.cartValidationInProgress = false;
        }
      })
    );
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }

  disableButtonWhileNavigation(): void {
    this.cartValidationInProgress = true;
  }
}
<ng-container *ngIf="cart$ | async as cart">
  <ng-container *ngIf="entries$ | async as entries">
    <cx-order-summary [cart]="cart"></cx-order-summary>
    <ng-container *cxFeatureLevel="'!4.2'">
      <button
        [routerLink]="{ cxRoute: 'checkout' } | cxUrl"
        *ngIf="entries.length"
        class="btn btn-primary btn-block"
        type="button"
      >
        {{ 'cartDetails.proceedToCheckout' | cxTranslate }}
      </button>
    </ng-container>

    <ng-container *cxFeatureLevel="'4.2'">
      <cx-progress-button
        *ngIf="entries.length"
        [routerLink]="{ cxRoute: 'checkout' } | cxUrl"
        [loading]="cartValidationInProgress"
        [disabled]="cartValidationInProgress"
        [class]="'btn btn-primary btn-block'"
        (clikEvent)="disableButtonWhileNavigation()"
      >
        {{
          (!cartValidationInProgress ? 'cartDetails.proceedToCheckout' : '')
            | cxTranslate
        }}
      </cx-progress-button>
    </ng-container>
  </ng-container>
</ng-container>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""