File

libs/common/frame/src/services/taly-frame-navigation-service.ts

Description

Helper service to integrate our frame navigation sections with external systems like PFE.

Best used with an actual implementation like the PfeFrameNavigationService. Provide it in your app root module.

{ provide: TalyFrameNavigationService, useClass: PfeFrameNavigationService }

Index

Properties
Methods

Constructor

constructor()

Properties

Abstract applicationBusy$
Type : Observable<boolean>
Abstract applicationBusyMessage$
Type : Observable<ExtendedDisplayMessage | undefined>
currentSectionStates$
Default value : this._currentSectionStates$.asObservable()
history$
Default value : this._history$.asObservable()
Abstract pageActionStatus$
Type : Observable<PageActionStatus>
visibleSections$
Default value : this._visibleSections$.asObservable()

Methods

Abstract cancel
cancel()
Returns : void
Public disableTitleUpdates
disableTitleUpdates()
Returns : void
Abstract getOfferCode$
getOfferCode$(stateKey: string)
Parameters :
Name Type Optional
stateKey string No
Abstract gotoErrorPage
gotoErrorPage(errorResponse?: HttpErrorResponse)
Parameters :
Name Type Optional
errorResponse HttpErrorResponse Yes
Returns : void
Abstract gotoPage
gotoPage(pageId: string)
Parameters :
Name Type Optional
pageId string No
Returns : void
Abstract gotoSection
gotoSection(sectionId: string)
Parameters :
Name Type Optional
sectionId string No
Returns : void
Abstract navigateBack
navigateBack()
Returns : void
Abstract navigateHome
navigateHome()
Returns : void
Abstract navigateNext
navigateNext()
Returns : void
Abstract saveOffer
saveOffer()
Returns : void
Public setSections
setSections(sections: SectionConfig)
Parameters :
Name Type Optional Default value
sections SectionConfig No new Map()
Returns : void
Public setTranslatedTitle
setTranslatedTitle(translatedTitle: string)
Parameters :
Name Type Optional
translatedTitle string No
Returns : void
import { ExtendedDisplayMessage } from '@allianz/ngx-pfe';
import { TALY_APP_TITLE } from '@allianz/taly-core';
import { HttpErrorResponse } from '@angular/common/http';
import { inject } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { tap } from 'rxjs/operators';
import { PageActionStatus } from '../frame-parts/actions/model';
import { SectionConfig, SectionWithState } from '../frame-parts/navigation/model';
import { StepState } from '../frame-parts/navigation/navigation-step-state';
import { getSectionsWithStates } from '../frame-parts/navigation/navigation-utils';

/**
 * Helper service to integrate our frame navigation sections with external systems like PFE.
 *
 * Best used with an actual implementation like the `PfeFrameNavigationService`.
 * Provide it in your app root module.
 *
 * {
 *   provide: TalyFrameNavigationService,
 *   useClass: PfeFrameNavigationService
 * }
 */
export abstract class TalyFrameNavigationService {
  protected _currentSectionStates$ = new BehaviorSubject<SectionWithState[]>([]);
  currentSectionStates$ = this._currentSectionStates$.asObservable();

  protected _history$ = new BehaviorSubject<string[]>([]);
  history$ = this._history$.asObservable();

  private visibleSectionsSubscription?: Subscription;
  protected _visibleSections$ = new BehaviorSubject<SectionConfig>(new Map());
  visibleSections$ = this._visibleSections$.asObservable();

  abstract pageActionStatus$: Observable<PageActionStatus>;
  abstract applicationBusy$: Observable<boolean>;
  abstract applicationBusyMessage$: Observable<ExtendedDisplayMessage | undefined>;

  abstract gotoSection(sectionId: string): void;

  abstract gotoPage(pageId: string): void;

  abstract gotoErrorPage(errorResponse?: HttpErrorResponse): void;

  abstract navigateBack(): void;

  abstract navigateNext(): void;

  abstract navigateHome(): void;

  abstract saveOffer(): void;

  abstract cancel(): void;

  abstract getOfferCode$(stateKey: string): Observable<string | undefined>;

  private titleService: Title;
  private appTitle: string;
  private injectedAppTitle = inject(TALY_APP_TITLE, { optional: true });
  private isTitleUpdatesDisabled = false;

  constructor() {
    this.titleService = inject(Title);
    this.appTitle = this.titleService.getTitle();
  }

  public setSections(sections: SectionConfig = new Map()) {
    this.visibleSectionsSubscription?.unsubscribe();
    this.visibleSectionsSubscription = this.getVisibleSections$(sections)
      .pipe(
        tap((visibleSections) => this._visibleSections$.next(visibleSections)),
        tap(() => this.updateSectionStates()),
        tap(() => this.updatePageTitle())
      )
      .subscribe();
  }

  public disableTitleUpdates() {
    this.isTitleUpdatesDisabled = true;
  }

  public setTranslatedTitle(translatedTitle: string) {
    this.appTitle = translatedTitle;
    this.updatePageTitle();
  }

  protected abstract getVisibleSections$(sections: SectionConfig): Observable<SectionConfig>;

  protected updatePageTitle() {
    if (this.isTitleUpdatesDisabled) {
      return;
    }

    let currentSectionLabel;

    const currentSectionId = this._currentSectionStates$.value.find(
      (section) => section.state === StepState.CURRENT
    )?.sectionId;
    if (currentSectionId) {
      currentSectionLabel = this._visibleSections$.value.get(currentSectionId)?.label;
    }

    let appTitle = this.injectedAppTitle || this.appTitle;
    if (currentSectionLabel?.trim()) {
      appTitle = `${currentSectionLabel} - ${appTitle}`;
    }

    this.titleService.setTitle(appTitle);
  }

  protected updateSectionStates() {
    if (this._visibleSections$.value) {
      const sectionStates = getSectionsWithStates(
        this._history$.value,
        this._visibleSections$.value
      );
      this._currentSectionStates$.next(sectionStates);
    }
  }

  protected updateHistory(history: string[]) {
    this._history$.next(history);
    this.updateSectionStates();
    this.updatePageTitle();
  }
}

results matching ""

    No results matching ""