File

integration-libs/cdc/root/service/cdc-js.service.ts

Index

Properties
Methods

Constructor

constructor(cdcConfig: CdcConfig, baseSiteService: BaseSiteService, languageService: LanguageService, scriptLoader: ScriptLoader, winRef: WindowRef, cdcAuth: CdcAuthFacade, auth: AuthService, zone: NgZone, userProfileFacade: UserProfileFacade, platform: any)
Parameters :
Name Type Optional
cdcConfig CdcConfig No
baseSiteService BaseSiteService No
languageService LanguageService No
scriptLoader ScriptLoader No
winRef WindowRef No
cdcAuth CdcAuthFacade No
auth AuthService No
zone NgZone No
userProfileFacade UserProfileFacade No
platform any No

Methods

Protected addCdcEventHandlers
addCdcEventHandlers(baseSite: string)

Method to register CDC event handlers

Parameters :
Name Type Optional
baseSite string No
Returns : void
didLoad
didLoad()

Returns observable with the information if CDC script is loaded.

Returns : Observable<boolean>
didScriptFailToLoad
didScriptFailToLoad()

Returns observable with the information if CDC script failed to load.

Returns : Observable<boolean>
Private getJavascriptUrlForCurrentSite
getJavascriptUrlForCurrentSite(baseSite: string)
Parameters :
Name Type Optional
baseSite string No
Returns : string
initialize
initialize()

Initialize CDC script

Returns : void
loadCdcJavascript
loadCdcJavascript()

Method which loads the CDC Script

Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
onLoginEventHandler
onLoginEventHandler(baseSite: string, response?: any)

Trigger login to Commerce once an onLogin event is triggered by CDC Screen Set.

Parameters :
Name Type Optional
baseSite string No
response any Yes
Returns : void
onProfileUpdateEventHandler
onProfileUpdateEventHandler(response?: any)

Updates user details using the existing User API

Parameters :
Name Type Optional
response any Yes
Returns : void
Protected registerEventListeners
registerEventListeners(baseSite: string)

Register login event listeners for CDC login

Parameters :
Name Type Optional
baseSite string No
Returns : void

Properties

Protected errorLoading$
Default value : new ReplaySubject<boolean>(1)
Protected loaded$
Default value : new ReplaySubject<boolean>(1)
Protected subscription
Type : Subscription
Default value : new Subscription()
import { isPlatformBrowser } from '@angular/common';
import {
  Inject,
  Injectable,
  NgZone,
  OnDestroy,
  PLATFORM_ID,
} from '@angular/core';
import {
  AuthService,
  BaseSiteService,
  LanguageService,
  ScriptLoader,
  User,
  WindowRef,
} from '@spartacus/core';
import { combineLatest, Observable, ReplaySubject, Subscription } from 'rxjs';
import { take } from 'rxjs/operators';
import { UserProfileFacade } from '@spartacus/user/profile/root';
import { CdcConfig } from '../config/cdc-config';
import { CdcAuthFacade } from '../facade/cdc-auth.facade';

@Injectable({
  providedIn: 'root',
})
export class CdcJsService implements OnDestroy {
  protected loaded$ = new ReplaySubject<boolean>(1);
  protected errorLoading$ = new ReplaySubject<boolean>(1);
  protected subscription: Subscription = new Subscription();

  constructor(
    protected cdcConfig: CdcConfig,
    protected baseSiteService: BaseSiteService,
    protected languageService: LanguageService,
    protected scriptLoader: ScriptLoader,
    protected winRef: WindowRef,
    protected cdcAuth: CdcAuthFacade,
    protected auth: AuthService,
    protected zone: NgZone,
    protected userProfileFacade: UserProfileFacade,
    @Inject(PLATFORM_ID) protected platform: any
  ) {}

  /**
   * Initialize CDC script
   */
  initialize(): void {
    this.loadCdcJavascript();
  }

  /**
   * Returns observable with the information if CDC script is loaded.
   */
  didLoad(): Observable<boolean> {
    return this.loaded$.asObservable();
  }

  /**
   * Returns observable with the information if CDC script failed to load.
   */
  didScriptFailToLoad(): Observable<boolean> {
    return this.errorLoading$.asObservable();
  }

  /**
   * Method which loads the CDC Script
   */
  loadCdcJavascript(): void {
    // Only load the script on client side (no SSR)
    if (isPlatformBrowser(this.platform)) {
      this.subscription.add(
        combineLatest([
          this.baseSiteService.getActive(),
          this.languageService.getActive(),
        ])
          .pipe(take(1))
          .subscribe(([baseSite, language]) => {
            const scriptForBaseSite =
              this.getJavascriptUrlForCurrentSite(baseSite);
            if (scriptForBaseSite) {
              const javascriptUrl = `${scriptForBaseSite}&lang=${language}`;
              this.scriptLoader.embedScript({
                src: javascriptUrl,
                params: undefined,
                attributes: { type: 'text/javascript' },
                callback: () => {
                  this.registerEventListeners(baseSite);
                  this.loaded$.next(true);
                },
                errorCallback: () => {
                  this.errorLoading$.next(true);
                },
              });
              if (this.winRef?.nativeWindow !== undefined) {
                (this.winRef.nativeWindow as { [key: string]: any })[
                  '__gigyaConf'
                ] = {
                  include: 'id_token',
                };
              }
            }
          })
      );
    }
  }

  private getJavascriptUrlForCurrentSite(baseSite: string): string {
    const filteredConfigs = (this.cdcConfig.cdc ?? []).filter(
      (conf) => conf.baseSite === baseSite
    );
    if (filteredConfigs && filteredConfigs.length > 0) {
      return filteredConfigs[0].javascriptUrl;
    }
    return '';
  }

  /**
   * Register login event listeners for CDC login
   *
   * @param baseSite
   */
  protected registerEventListeners(baseSite: string): void {
    this.addCdcEventHandlers(baseSite);
  }

  /**
   * Method to register CDC event handlers
   *
   * @param baseSite
   */
  protected addCdcEventHandlers(baseSite: string): void {
    (this.winRef.nativeWindow as { [key: string]: any })?.[
      'gigya'
    ]?.accounts?.addEventHandlers({
      onLogin: (...params: any[]) => {
        this.zone.run(() => this.onLoginEventHandler(baseSite, ...params));
      },
    });
  }

  /**
   * Trigger login to Commerce once an onLogin event is triggered by CDC Screen Set.
   *
   * @param baseSite
   * @param response
   */
  onLoginEventHandler(baseSite: string, response?: any) {
    if (response) {
      this.cdcAuth.loginWithCustomCdcFlow(
        response.UID,
        response.UIDSignature,
        response.signatureTimestamp,
        response.id_token !== undefined ? response.id_token : '',
        baseSite
      );
    }
  }

  /**
   * Updates user details using the existing User API
   *
   * @param response
   */
  onProfileUpdateEventHandler(response?: any) {
    if (response) {
      const userDetails: User = {};
      userDetails.firstName = response.profile.firstName;
      userDetails.lastName = response.profile.lastName;
      this.userProfileFacade.update(userDetails);
    }
  }

  ngOnDestroy(): void {
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
  }
}

result-matching ""

    No results matching ""