File

feature-libs/checkout/components/components/delivery-mode/delivery-mode.component.ts

Implements

OnInit OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-delivery-mode
templateUrl ./delivery-mode.component.html

Index

Properties
Methods
Accessors

Constructor

constructor(fb: FormBuilder, checkoutDeliveryService: CheckoutDeliveryFacade, checkoutConfigService: CheckoutConfigService, activatedRoute: ActivatedRoute, checkoutStepService: CheckoutStepService)
Parameters :
Name Type Optional
fb FormBuilder No
checkoutDeliveryService CheckoutDeliveryFacade No
checkoutConfigService CheckoutConfigService No
activatedRoute ActivatedRoute No
checkoutStepService CheckoutStepService No

Methods

back
back()
Returns : void
changeMode
changeMode(code: string)
Parameters :
Name Type Optional
code string No
Returns : void
next
next()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

backBtnText
Default value : this.checkoutStepService.getBackBntText(this.activatedRoute)
continueButtonPressed
Default value : false
deliveryModeSub
Type : Subscription
mode
Type : FormGroup
Default value : this.fb.group({ deliveryModeId: ['', Validators.required], })
selectedDeliveryMode$
Type : Observable<DeliveryMode>
supportedDeliveryModes$
Type : Observable<DeliveryMode[]>

Accessors

deliveryModeInvalid
getdeliveryModeInvalid()
import {
  ChangeDetectionStrategy,
  Component,
  OnDestroy,
  OnInit,
} from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { CheckoutDeliveryFacade } from '@spartacus/checkout/root';
import { DeliveryMode } from '@spartacus/core';
import { Observable, Subscription } from 'rxjs';
import {
  distinctUntilChanged,
  filter,
  map,
  takeWhile,
  withLatestFrom,
} from 'rxjs/operators';
import { CheckoutConfigService } from '../../services/checkout-config.service';
import { CheckoutStepService } from '../../services/checkout-step.service';

@Component({
  selector: 'cx-delivery-mode',
  templateUrl: './delivery-mode.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeliveryModeComponent implements OnInit, OnDestroy {
  supportedDeliveryModes$: Observable<DeliveryMode[]>;
  selectedDeliveryMode$: Observable<DeliveryMode>;
  continueButtonPressed = false;

  backBtnText = this.checkoutStepService.getBackBntText(this.activatedRoute);

  deliveryModeSub: Subscription;

  mode: FormGroup = this.fb.group({
    deliveryModeId: ['', Validators.required],
  });

  constructor(
    private fb: FormBuilder,
    private checkoutDeliveryService: CheckoutDeliveryFacade,
    private checkoutConfigService: CheckoutConfigService,
    private activatedRoute: ActivatedRoute,
    protected checkoutStepService: CheckoutStepService
  ) {}

  ngOnInit() {
    this.supportedDeliveryModes$ = this.checkoutDeliveryService
      .getSupportedDeliveryModes()
      .pipe(
        filter((deliveryModes: DeliveryMode[]) => !!deliveryModes?.length),
        distinctUntilChanged(
          (current: DeliveryMode[], previous: DeliveryMode[]) => {
            return JSON.stringify(current) === JSON.stringify(previous);
          }
        )
      );

    // Reload delivery modes on error
    this.checkoutDeliveryService
      .getLoadSupportedDeliveryModeProcess()
      .pipe(takeWhile((state) => state?.success === false))
      .subscribe((state) => {
        if (state.error && !state.loading) {
          this.checkoutDeliveryService.loadSupportedDeliveryModes();
        }
      });

    this.deliveryModeSub = this.supportedDeliveryModes$
      .pipe(
        withLatestFrom(
          this.checkoutDeliveryService
            .getSelectedDeliveryMode()
            .pipe(
              map(
                (deliveryMode: DeliveryMode | null | undefined) =>
                  deliveryMode?.code
              )
            )
        )
      )
      .subscribe(
        ([deliveryModes, code]: [DeliveryMode[], string | undefined]) => {
          if (
            !(
              code &&
              !!deliveryModes.find((deliveryMode) => deliveryMode.code === code)
            )
          ) {
            code =
              this.checkoutConfigService.getPreferredDeliveryMode(
                deliveryModes
              );
          }
          if (code) {
            this.mode.controls['deliveryModeId'].setValue(code);
            this.checkoutDeliveryService.setDeliveryMode(code);
          }
        }
      );
  }

  changeMode(code: string): void {
    this.checkoutDeliveryService.setDeliveryMode(code);
  }

  next(): void {
    if (this.mode.valid && this.mode.value) {
      this.continueButtonPressed = true;
      this.checkoutStepService.next(this.activatedRoute);
    }
  }

  back(): void {
    this.checkoutStepService.back(this.activatedRoute);
  }

  get deliveryModeInvalid(): boolean {
    return this.mode.controls['deliveryModeId'].invalid;
  }

  ngOnDestroy(): void {
    if (this.deliveryModeSub) {
      this.deliveryModeSub.unsubscribe();
    }
  }
}
<div [formGroup]="mode">
  <div class="row">
    <div class="col-md-12 col-lg-9">
      <h2 class="cx-checkout-title d-none d-lg-block d-xl-block">
        {{ 'checkoutShipping.shippingMethod' | cxTranslate }}
      </h2>

      <ng-container
        *ngIf="(supportedDeliveryModes$ | async)?.length; else loading"
      >
        <div
          class="form-check"
          *ngFor="let mode of supportedDeliveryModes$ | async"
        >
          <input
            class="form-check-input"
            role="radio"
            type="radio"
            id="deliveryMode-{{ mode.code }}"
            aria-checked="true"
            (change)="changeMode(mode.code)"
            [value]="mode.code"
            formControlName="deliveryModeId"
          />
          <label
            class="cx-delivery-label form-check-label form-radio-label"
            for="deliveryMode-{{ mode.code }}"
          >
            <div class="cx-delivery-mode">{{ mode.name }}</div>
            <div class="cx-delivery-price">
              {{ mode.deliveryCost.formattedValue }}
            </div>
            <div class="cx-delivery-details">{{ mode.description }}</div>
          </label>
        </div>
      </ng-container>
    </div>
  </div>

  <ng-container *ngIf="!continueButtonPressed; else loading">
    <div class="row cx-checkout-btns">
      <div class="col-md-12 col-lg-6">
        <button class="btn btn-block btn-action" (click)="back()">
          {{ backBtnText | cxTranslate }}
        </button>
      </div>
      <div class="col-md-12 col-lg-6">
        <button
          class="btn btn-block btn-primary"
          [disabled]="deliveryModeInvalid"
          (click)="next()"
        >
          {{ 'common.continue' | cxTranslate }}
        </button>
      </div>
    </div>
  </ng-container>
</div>

<ng-template #loading>
  <div class="cx-spinner">
    <cx-spinner></cx-spinner>
  </div>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""