RED-9175: prevent search re-trigger upon navigating the file pages.

This commit is contained in:
Nicoleta Panaghiu 2024-06-03 15:19:46 +03:00
parent 53cc0f6a10
commit 5097ce3dba
3 changed files with 45 additions and 5 deletions

View File

@ -4,7 +4,6 @@ import { InputWithActionComponent, LoadingService, shareLast, Toaster } from '@i
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { IPageRange } from '@red/domain';
import { ReanalysisService } from '@services/reanalysis.service';
import { ExcludedPagesService } from '../../services/excluded-pages.service';
import { combineLatest, firstValueFrom, Observable } from 'rxjs';
import { FilePreviewStateService } from '../../services/file-preview-state.service';
import { map } from 'rxjs/operators';
@ -23,7 +22,6 @@ export class PageExclusionComponent {
constructor(
readonly permissionsService: PermissionsService,
readonly excludedPagesService: ExcludedPagesService,
private readonly _reanalysisService: ReanalysisService,
private readonly _toaster: Toaster,
private readonly _loadingService: LoadingService,
@ -58,6 +56,7 @@ export class PageExclusionComponent {
const pageRanges = extractPageRanges(inputValue, file);
const excludePages$ = this._reanalysisService.excludePages({ pageRanges }, file.dossierId, file);
await firstValueFrom(excludePages$);
this._state.excludePages(this._flattenPageRanges(pageRanges));
this._inputComponent.reset();
} catch (e) {
this._toaster.error(_('file-preview.tabs.exclude-pages.error'));
@ -76,10 +75,22 @@ export class PageExclusionComponent {
file,
);
await firstValueFrom(includePages$);
this._state.includePages(this._flattenPageRanges([range]));
this._inputComponent.reset();
this._loadingService.stop();
}
private _flattenPageRanges(ranges: IPageRange[]) {
const flattenedPages = [];
ranges.forEach(range => {
for (let page = range.startPage; page <= range.endPage; page++) {
flattenedPages.push(page);
}
});
return flattenedPages;
}
private _toPageRange(pages: number[]) {
return pages.reduce((ranges: IPageRange[], page) => {
if (!ranges.length) {

View File

@ -604,11 +604,16 @@ export class FilePreviewScreenComponent
)
.subscribe();
this.addActiveScreenSubscription = combineLatest([this._documentViewer.loaded$, this._viewModeService.viewMode$, this.state.file$])
this.addActiveScreenSubscription = combineLatest([
this._documentViewer.loaded$,
this._viewModeService.viewMode$,
this.state.excludedPages$,
])
.pipe(
filter(([loaded, viewMode, shouldUpdate]) => loaded && !!viewMode),
tap(async ([loaded, viewMode, shouldUpdate]) => {
if (loaded) {
this._logger.info('[PDF] Stamp pdf');
await this._stampService.stampPDF();
}
}),

View File

@ -1,5 +1,5 @@
import { Injectable, Injector } from '@angular/core';
import { combineLatest, firstValueFrom, from, merge, Observable, of, pairwise, Subject, switchMap } from 'rxjs';
import { BehaviorSubject, combineLatest, firstValueFrom, from, merge, Observable, of, pairwise, Subject, switchMap } from 'rxjs';
import { Dictionary, Dossier, DOSSIER_ID, DOSSIER_TEMPLATE_ID, File, FILE_ID } from '@red/domain';
import { Router } from '@angular/router';
import { FilesMapService } from '@services/files/files-map.service';
@ -37,6 +37,7 @@ export class FilePreviewStateService {
readonly dossierFileChange$: Observable<File[]>;
readonly isReadonly$: Observable<boolean>;
readonly isWritable$: Observable<boolean>;
readonly excludedPages$: Observable<number[]>;
readonly dossierId: string = getParam(DOSSIER_ID);
readonly dossierTemplateId: string = getParam(DOSSIER_TEMPLATE_ID);
@ -46,6 +47,7 @@ export class FilePreviewStateService {
file: File;
#dossierDictionary: Dictionary;
readonly #reloadBlob$ = new Subject();
readonly #excludedPages$ = new BehaviorSubject<number[]>([]);
constructor(
router: Router,
@ -62,7 +64,19 @@ export class FilePreviewStateService {
) {
const dossiersService = dossiersServiceResolver(_injector, router);
this.dossier$ = dossiersService.getEntityChanged$(this.dossierId).pipe(tap(dossier => (this.dossier = dossier)));
this.file$ = filesMapService.watch$(this.dossierId, this.fileId).pipe(tap(file => (this.file = file)));
this.file$ = filesMapService.watch$(this.dossierId, this.fileId).pipe(
tap(file => {
this.file = file;
const areExcludedPagesEqual =
this.file.excludedPages.length === this.#excludedPages$.value.length &&
this.file.excludedPages.every(page => this.#excludedPages$.value.includes(page));
if (!areExcludedPagesEqual) {
this.#excludedPages$.next(this.file.excludedPages);
}
}),
);
this.excludedPages$ = this.#excludedPages$.asObservable();
[this.isReadonly$, this.isWritable$] = boolFactory(
combineLatest([this.file$, this.dossier$]),
([file, dossier]) => !permissionsService.canPerformAnnotationActions(file, dossier),
@ -107,6 +121,16 @@ export class FilePreviewStateService {
this.#reloadBlob$.next(true);
}
excludePages(excludedPages: number[]) {
const newExcludedPages = [...new Set([...this.#excludedPages$.value, ...excludedPages])];
this.#excludedPages$.next(newExcludedPages);
}
includePages(includedPages: number[]) {
const newExcludedPages = this.#excludedPages$.value.filter(excludedPage => includedPages.includes(excludedPage));
this.#excludedPages$.next(newExcludedPages);
}
#getRemainingTimeVerbose(event: HttpProgressEvent, startTime: number) {
const remainingTime = getRemainingTime(event, startTime);
if (remainingTime > 60) {