File

feature-libs/asm/components/asm-session-timer/asm-session-timer.component.ts

Implements

OnInit OnDestroy

Metadata

selector cx-asm-session-timer
templateUrl ./asm-session-timer.component.html

Index

Properties
Methods

Constructor

constructor(config: AsmConfig, asmComponentService: AsmComponentService, routingService: RoutingService, changeDetectorRef: ChangeDetectorRef, userIdService: UserIdService)
Parameters :
Name Type Optional
config AsmConfig No
asmComponentService AsmComponentService No
routingService RoutingService No
changeDetectorRef ChangeDetectorRef No
userIdService UserIdService No

Methods

Protected getTimerStartDelayInSeconds
getTimerStartDelayInSeconds()
Returns : number
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Protected resetOnCustomerSessionChange
resetOnCustomerSessionChange()
Returns : void
Protected resetOnNavigate
resetOnNavigate()
Returns : void
resetTimer
resetTimer()
Returns : void

Properties

Protected interval
Type : any
Protected maxStartDelayInSeconds
Type : number
Default value : 60000
Protected subscriptions
Default value : new Subscription()
timeLeft
Type : number
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { AsmConfig } from '@spartacus/asm/core';
import { RoutingService, UserIdService } from '@spartacus/core';
import { Subscription } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
import { AsmComponentService } from '../services/asm-component.service';

@Component({
  selector: 'cx-asm-session-timer',
  templateUrl: './asm-session-timer.component.html',
})
export class AsmSessionTimerComponent implements OnInit, OnDestroy {
  protected subscriptions = new Subscription();
  protected interval: any;
  protected maxStartDelayInSeconds = 60000;
  timeLeft: number;

  constructor(
    protected config: AsmConfig,
    protected asmComponentService: AsmComponentService,
    protected routingService: RoutingService,
    protected changeDetectorRef: ChangeDetectorRef,
    protected userIdService: UserIdService
  ) {}

  ngOnInit(): void {
    this.timeLeft = this.getTimerStartDelayInSeconds();
    this.interval = setInterval(() => {
      if (this.timeLeft > 0) {
        this.timeLeft--;
      } else {
        clearInterval(this.interval);
        this.asmComponentService.logoutCustomerSupportAgentAndCustomer();
      }
      this.changeDetectorRef.markForCheck();
    }, 1000);

    this.resetOnNavigate();
    this.resetOnCustomerSessionChange();
  }

  protected resetOnNavigate(): void {
    this.subscriptions.add(
      this.routingService.isNavigating().subscribe((isNavigating) => {
        if (isNavigating) {
          this.resetTimer();
        }
      })
    );
  }

  protected resetOnCustomerSessionChange(): void {
    this.subscriptions.add(
      this.userIdService
        .getUserId()
        .pipe(distinctUntilChanged())
        .subscribe(() => this.resetTimer())
    );
  }

  resetTimer(): void {
    if (this.timeLeft > 0) {
      this.timeLeft = this.getTimerStartDelayInSeconds();
    }
  }

  protected getTimerStartDelayInSeconds(): number {
    if (
      this.config.asm?.agentSessionTimer?.startingDelayInSeconds === undefined
    ) {
      return 600;
    }
    if (
      this.config.asm.agentSessionTimer.startingDelayInSeconds >
      this.maxStartDelayInSeconds
    ) {
      return this.maxStartDelayInSeconds;
    } else {
      return this.config.asm.agentSessionTimer.startingDelayInSeconds;
    }
  }
  ngOnDestroy(): void {
    this.subscriptions.unsubscribe();
    if (this.interval) {
      clearInterval(this.interval);
    }
  }
}
<span class="label">{{ 'asm.agentSessionTimer.label' | cxTranslate }}:</span>
<span class="time"
  >{{ timeLeft | formatTimer }}
  {{ 'asm.agentSessionTimer.minutes' | cxTranslate }}</span
>
<button
  class="reset"
  title="{{ 'asm.agentSessionTimer.reset' | cxTranslate }}"
  (click)="resetTimer()"
></button>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""