File
Public
rewindHistory
|
Default value : () => {...}
|
|
import { NgxLoggerService } from '@allianz/ngx-logger';
import { Injectable } from '@angular/core';
import { PfeNavigationUtilService } from '../../services/pfe-navigation-service/navigation-util.service';
import { PFE_HISTORY_KEY } from '../../services/pfe-state-service/pfe-state-keys';
import { PfeStateService } from '../../services/pfe-state-service/state.service';
import { PfeRewindHistory, RewindHistoryActionConfig } from './rewind-history.model';
@Injectable()
export class PfeRewindHistoryService {
constructor(
private pfeStateService: PfeStateService,
private pfeNavigationUtilService: PfeNavigationUtilService,
private logger: NgxLoggerService
) {}
public rewindHistory = async (actionConfig: RewindHistoryActionConfig): Promise<void> => {
if (actionConfig?.clearHistory) {
this.pfeStateService.storeValue(PFE_HISTORY_KEY, []);
return;
}
const pageId = actionConfig?.pageId
? this.pfeStateService.getStateValueByExpression(actionConfig?.pageId)
: this.pfeNavigationUtilService.currentPageId$.value;
const pfeHistory: string[] = this.pfeStateService.getValue(PFE_HISTORY_KEY) as string[];
if (!pfeHistory) {
return;
}
let index = pfeHistory.findIndex((value) => value === pageId);
if (index < 0) {
this.logger.warnToServer(
`${PfeRewindHistory}: Unable to find index of page "${pageId}", that was supposed to be used in the rewind.`
);
return;
}
if (actionConfig?.keepSelf) {
index++;
}
const newPfeHistory = pfeHistory.splice(0, index);
this.pfeStateService.storeValue(PFE_HISTORY_KEY, newPfeHistory);
};
}