File

feature-libs/checkout/components/services/checkout-step.service.ts

Index

Properties
Methods

Constructor

constructor(routingService: RoutingService, checkoutConfig: CheckoutConfig, routingConfigService: RoutingConfigService)
Parameters :
Name Type Optional
routingService RoutingService No
checkoutConfig CheckoutConfig No
routingConfigService RoutingConfigService No

Methods

back
back(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : void
disableEnableStep
disableEnableStep(currentStepType: CheckoutStepType, disabled: boolean)
Parameters :
Name Type Optional
currentStepType CheckoutStepType No
disabled boolean No
Returns : void
getBackBntText
getBackBntText(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : string
getCheckoutStep
getCheckoutStep(currentStepType: CheckoutStepType)
Parameters :
Name Type Optional
currentStepType CheckoutStepType No
Private getCheckoutStepIndex
getCheckoutStepIndex(key: string, value: any)
Parameters :
Name Type Optional
key string No
value any No
Returns : number | null
getCheckoutStepRoute
getCheckoutStepRoute(currentStepType: CheckoutStepType)
Parameters :
Name Type Optional
currentStepType CheckoutStepType No
Returns : string | undefined
getCurrentStepIndex
getCurrentStepIndex(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : number | null
getFirstCheckoutStepRoute
getFirstCheckoutStepRoute()
Returns : string
getNextCheckoutStepUrl
getNextCheckoutStepUrl(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : string | null
getPreviousCheckoutStepUrl
getPreviousCheckoutStepUrl(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : string | null
Private getStepUrlFromActivatedRoute
getStepUrlFromActivatedRoute(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : string | null
Private getStepUrlFromStepRoute
getStepUrlFromStepRoute(stepRoute: string)
Parameters :
Name Type Optional
stepRoute string No
Returns : string | null
goToStepWithIndex
goToStepWithIndex(stepIndex: number)
Parameters :
Name Type Optional
stepIndex number No
Returns : void
next
next(activatedRoute: ActivatedRoute)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
Returns : void
resetSteps
resetSteps()
Returns : void

Properties

Readonly activeStepIndex$
Type : Observable<number>
Default value : this.routingService .getRouterState() .pipe( switchMap((router) => { const activeStepUrl = router.state.context.id; return this.steps$.pipe( map((steps) => { let activeIndex: number = 0; steps.forEach((step, index) => { const routeUrl = `/${ this.routingConfigService.getRouteConfig(step.routeName) .paths?.[0] }`; if (routeUrl === activeStepUrl) { activeIndex = index; } }); return activeIndex; }) ); }) )
allSteps
Type : CheckoutStep[]
Readonly steps$
Type : BehaviorSubject<CheckoutStep[]>
Default value : new BehaviorSubject< CheckoutStep[] >([])
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {
  CheckoutConfig,
  CheckoutStep,
  CheckoutStepType,
} from '@spartacus/checkout/root';
import { RoutingConfigService, RoutingService } from '@spartacus/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
export class CheckoutStepService {
  // initial enabled steps
  allSteps: CheckoutStep[];

  readonly steps$: BehaviorSubject<CheckoutStep[]> = new BehaviorSubject<
    CheckoutStep[]
  >([]);

  readonly activeStepIndex$: Observable<number> = this.routingService
    .getRouterState()
    .pipe(
      switchMap((router) => {
        const activeStepUrl = router.state.context.id;
        return this.steps$.pipe(
          map((steps) => {
            let activeIndex: number = 0;
            steps.forEach((step, index) => {
              const routeUrl = `/${
                this.routingConfigService.getRouteConfig(step.routeName)
                  .paths?.[0]
              }`;
              if (routeUrl === activeStepUrl) {
                activeIndex = index;
              }
            });
            return activeIndex;
          })
        );
      })
    );

  constructor(
    protected routingService: RoutingService,
    protected checkoutConfig: CheckoutConfig,
    protected routingConfigService: RoutingConfigService
  ) {
    this.resetSteps();
  }

  back(activatedRoute: ActivatedRoute): void {
    const previousUrl = this.getPreviousCheckoutStepUrl(activatedRoute);
    this.routingService.go(previousUrl === null ? 'cart' : previousUrl);
  }

  next(activatedRoute: ActivatedRoute): void {
    const nextUrl = this.getNextCheckoutStepUrl(activatedRoute);
    this.routingService.go(nextUrl);
  }

  goToStepWithIndex(stepIndex: number): void {
    this.routingService.go(
      this.getStepUrlFromStepRoute(this.allSteps[stepIndex].routeName)
    );
  }

  getBackBntText(activatedRoute: ActivatedRoute): string {
    if (this.getPreviousCheckoutStepUrl(activatedRoute) === null) {
      return 'checkout.backToCart';
    }
    return 'common.back';
  }

  resetSteps(): void {
    this.allSteps = (this.checkoutConfig.checkout?.steps ?? [])
      .filter((step) => !step.disabled)
      .map((x) => Object.assign({}, x));
    this.steps$.next(this.allSteps);
  }

  disableEnableStep(
    currentStepType: CheckoutStepType,
    disabled: boolean
  ): void {
    const currentStep = this.allSteps.find((step) =>
      step.type.includes(currentStepType)
    );
    if (currentStep && currentStep.disabled !== disabled) {
      currentStep.disabled = disabled;
      this.steps$.next(this.allSteps.filter((step) => !step.disabled));
    }
  }

  getCheckoutStep(currentStepType: CheckoutStepType): CheckoutStep | undefined {
    const index = this.getCheckoutStepIndex('type', currentStepType);
    if (index !== null) {
      return this.allSteps[index];
    }
  }

  getCheckoutStepRoute(currentStepType: CheckoutStepType): string | undefined {
    return this.getCheckoutStep(currentStepType)?.routeName;
  }

  getFirstCheckoutStepRoute(): string {
    return this.allSteps[0].routeName;
  }

  getNextCheckoutStepUrl(activatedRoute: ActivatedRoute): string | null {
    const stepIndex = this.getCurrentStepIndex(activatedRoute);

    if (stepIndex !== null && stepIndex >= 0) {
      let i = 1;
      while (
        this.allSteps[stepIndex + i] &&
        this.allSteps[stepIndex + i].disabled
      ) {
        i++;
      }
      const nextStep = this.allSteps[stepIndex + i];
      if (nextStep) {
        return this.getStepUrlFromStepRoute(nextStep.routeName);
      }
    }
    return null;
  }

  getPreviousCheckoutStepUrl(activatedRoute: ActivatedRoute): string | null {
    const stepIndex = this.getCurrentStepIndex(activatedRoute);

    if (stepIndex !== null && stepIndex >= 0) {
      let i = 1;
      while (
        this.allSteps[stepIndex - i] &&
        this.allSteps[stepIndex - i].disabled
      ) {
        i++;
      }
      const previousStep = this.allSteps[stepIndex - i];
      if (previousStep) {
        return this.getStepUrlFromStepRoute(previousStep.routeName);
      }
    }
    return null;
  }

  getCurrentStepIndex(activatedRoute: ActivatedRoute): number | null {
    const currentStepUrl = this.getStepUrlFromActivatedRoute(activatedRoute);

    const stepIndex = this.allSteps.findIndex(
      (step) =>
        currentStepUrl === `/${this.getStepUrlFromStepRoute(step.routeName)}`
    );
    return stepIndex === -1 ? null : stepIndex;
  }

  private getStepUrlFromActivatedRoute(
    activatedRoute: ActivatedRoute
  ): string | null {
    return activatedRoute &&
      activatedRoute.snapshot &&
      activatedRoute.snapshot.url
      ? `/${activatedRoute.snapshot.url.join('/')}`
      : null;
  }

  private getStepUrlFromStepRoute(stepRoute: string): string | null {
    return (
      this.routingConfigService.getRouteConfig(stepRoute).paths?.[0] ?? null
    );
  }

  private getCheckoutStepIndex(key: string, value: any): number | null {
    return key && value
      ? this.allSteps.findIndex((step: CheckoutStep) => {
          const propertyVal = step[key as keyof CheckoutStep];
          return propertyVal instanceof Array
            ? propertyVal.includes(value)
            : propertyVal === value;
        })
      : null;
  }
}

result-matching ""

    No results matching ""