From 4f8fa98e65e01d9c39b93f4e7cc69876ee4e7675 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Sun, 14 Nov 2021 12:10:22 +0200 Subject: [PATCH] remove fileChanged and fileReanalyzed --- .../page-indicator.component.ts | 52 ++++++------------- .../dossier-overview-screen.component.ts | 8 ++- .../dossiers-listing-actions.component.html | 2 +- .../dossiers-listing-actions.component.ts | 10 ++-- .../file-preview-screen.component.ts | 14 ++--- .../entity-services/files-map.service.ts | 7 ++- .../red-ui/src/app/state/app-state.service.ts | 33 ++++-------- 7 files changed, 51 insertions(+), 75 deletions(-) diff --git a/apps/red-ui/src/app/modules/dossier/components/page-indicator/page-indicator.component.ts b/apps/red-ui/src/app/modules/dossier/components/page-indicator/page-indicator.component.ts index 04b1f1836..8ead6fb90 100644 --- a/apps/red-ui/src/app/modules/dossier/components/page-indicator/page-indicator.component.ts +++ b/apps/red-ui/src/app/modules/dossier/components/page-indicator/page-indicator.component.ts @@ -1,21 +1,11 @@ -import { - ChangeDetectionStrategy, - Component, - EventEmitter, - Input, - OnChanges, - OnDestroy, - OnInit, - Output, - SimpleChanges, -} from '@angular/core'; -import { AppStateService } from '@state/app-state.service'; +import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, Output } from '@angular/core'; import { PermissionsService } from '@services/permissions.service'; import { ConfigService } from '@services/config.service'; import { DossiersService } from '@services/entity-services/dossiers.service'; import { ViewedPagesService } from '../../shared/services/viewed-pages.service'; import { File, IViewedPage } from '@red/domain'; -import { AutoUnsubscribe } from '@iqser/common-ui'; +import { AutoUnsubscribe, OnChange } from '@iqser/common-ui'; +import { FilesMapService } from '@services/entity-services/files-map.service'; @Component({ selector: 'redaction-page-indicator', @@ -23,9 +13,15 @@ import { AutoUnsubscribe } from '@iqser/common-ui'; styleUrls: ['./page-indicator.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class PageIndicatorComponent extends AutoUnsubscribe implements OnChanges, OnInit, OnDestroy { - @Input() file: File; - @Input() active: boolean; +export class PageIndicatorComponent extends AutoUnsubscribe implements OnDestroy { + @Input() + @OnChange('handlePageRead') + file: File; + + @Input() + @OnChange('handlePageRead') + active: boolean; + @Input() showDottedIcon = false; @Input() number: number; @Input() viewedPages: IViewedPage[]; @@ -34,11 +30,10 @@ export class PageIndicatorComponent extends AutoUnsubscribe implements OnChanges @Output() readonly pageSelected = new EventEmitter(); pageReadTimeout: number = null; - canMarkPagesAsViewed: boolean; constructor( private readonly _viewedPagesService: ViewedPagesService, - private readonly _appStateService: AppStateService, + private readonly _filesMapService: FilesMapService, private readonly _dossiersService: DossiersService, private readonly _configService: ConfigService, private readonly _permissionService: PermissionsService, @@ -59,23 +54,8 @@ export class PageIndicatorComponent extends AutoUnsubscribe implements OnChanges } } - ngOnInit(): void { - this.addSubscription = this._appStateService.fileChanged$.subscribe(() => { - if (this.canMarkPagesAsViewed !== this._permissionService.canMarkPagesAsViewed(this.file)) { - this.canMarkPagesAsViewed = this._permissionService.canMarkPagesAsViewed(this.file); - this._handlePageRead(); - } - }); - } - - ngOnChanges(changes: SimpleChanges): void { - if (changes.active) { - this._handlePageRead(); - } - } - async toggleReadState() { - if (this.canMarkPagesAsViewed) { + if (this._permissionService.canMarkPagesAsViewed(this.file)) { if (this.read) { await this._markPageUnread(); } else { @@ -84,8 +64,8 @@ export class PageIndicatorComponent extends AutoUnsubscribe implements OnChanges } } - private _handlePageRead() { - if (!this.canMarkPagesAsViewed) { + handlePageRead() { + if (!this._permissionService.canMarkPagesAsViewed(this.file)) { return; } diff --git a/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/screen/dossier-overview-screen.component.ts b/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/screen/dossier-overview-screen.component.ts index 33b0f4905..382483ad3 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/screen/dossier-overview-screen.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/screen/dossier-overview-screen.component.ts @@ -53,6 +53,7 @@ import { UserPreferenceService } from '@services/user-preference.service'; import { saveAsCSV } from '@utils/csv-utils'; import { FilesService } from '@services/entity-services/files.service'; import { FilesMapService } from '@services/entity-services/files-map.service'; +import { ReanalysisService } from '@services/reanalysis.service'; @Component({ templateUrl: './dossier-overview-screen.component.html', @@ -91,6 +92,7 @@ export class DossierOverviewScreenComponent extends ListingComponent imple readonly permissionsService: PermissionsService, private readonly _loadingService: LoadingService, private readonly _appStateService: AppStateService, + private readonly _reanalysisService: ReanalysisService, private readonly _dossiersService: DossiersService, private readonly _dossierTemplatesService: DossierTemplatesService, readonly routerHistoryService: RouterHistoryService, @@ -208,7 +210,7 @@ export class DossierOverviewScreenComponent extends ListingComponent imple async reanalyseDossier() { try { - await this._appStateService.reanalyzeDossier(); + await this._reanalysisService.reanalyzeDossier(this.dossierId, true).toPromise(); await this.reloadFiles(); this._toaster.success(_('dossier-overview.reanalyse-dossier.success')); } catch (e) { @@ -223,10 +225,6 @@ export class DossierOverviewScreenComponent extends ListingComponent imple } async calculateData(): Promise { - if (!this.dossierId) { - return; - } - await this._loadEntitiesFromState(); this._computeAllFilters(); diff --git a/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/components/dossiers-listing-actions/dossiers-listing-actions.component.html b/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/components/dossiers-listing-actions/dossiers-listing-actions.component.html index a1d5d69c9..19e0151dc 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/components/dossiers-listing-actions/dossiers-listing-actions.component.html +++ b/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/components/dossiers-listing-actions/dossiers-listing-actions.component.html @@ -10,7 +10,7 @@ > { $event.stopPropagation(); - this.appStateService.reanalyzeDossier(dossier).then(() => { - this.appStateService.loadAllDossiers().then(() => this.actionPerformed.emit()); - }); + const reanalysis$ = this._reanalysisService.reanalyzeDossier(id).pipe(switchMapTo(this.appStateService.loadAllDossiers())); + await reanalysis$.pipe(tap(() => this.actionPerformed.emit())).toPromise(); } } diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts index 943ebfef6..ddb70ffcd 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts @@ -358,7 +358,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni response.manualRedactionEntryWrapper.rectId, ); this._instance.Core.annotationManager.deleteAnnotation(annotation); - this.fileData.file = await this.appStateService.reloadActiveFile(); + this.fileData.file = await this.appStateService.reloadFile(this.dossierId, this.fileId); const distinctPages = entryWrapper.manualRedactionEntry.positions .map(p => p.page) .filter((item, pos, self) => self.indexOf(item) === pos); @@ -499,7 +499,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni async assignToMe() { await this._fileActionService.assignToMe([this.fileData.file], async () => { - await this.appStateService.reloadActiveFile(); + await this.appStateService.reloadFile(this.dossierId, this.fileId); this._updateCanPerformActions(); }); } @@ -512,7 +512,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni await this._filesService.setReviewerFor([fileId], dossierId, reviewerId).toPromise(); this._toaster.info(_('assignment.reviewer'), { params: { reviewerName, filename } }); - await this.appStateService.reloadActiveFile(); + await this.appStateService.reloadFile(this.dossierId, this.fileId); this._updateCanPerformActions(); this.editingReviewer = false; } @@ -585,8 +585,10 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni } private _subscribeToFileUpdates(): void { - this.addSubscription = timer(0, 10000).pipe(switchMapTo(this.appStateService.reloadActiveFile())).subscribe(); - this.addSubscription = this.appStateService.fileReanalysed$ + this.addSubscription = timer(0, 10000) + .pipe(switchMapTo(this.appStateService.reloadFile(this.dossierId, this.fileId))) + .subscribe(); + this.addSubscription = this._filesMapService.fileReanalysed$ .pipe(filter(file => file.fileId === this.fileId)) .subscribe(async () => { await this._loadFileData(!this._reloadFileOnReanalysis); @@ -645,7 +647,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni const currentPageAnnotations = this.annotations.filter(a => a.pageNumber === page); const currentPageAnnotationIds = currentPageAnnotations.map(a => a.id); - this.fileData.file = await this.appStateService.reloadActiveFile(); + this.fileData.file = await this.appStateService.reloadFile(this.dossierId, this.fileId); this.fileData.redactionLog = await this._fileDownloadService.loadRedactionLogFor(this.dossierId, this.fileId).toPromise(); this.rebuildFilters(); diff --git a/apps/red-ui/src/app/services/entity-services/files-map.service.ts b/apps/red-ui/src/app/services/entity-services/files-map.service.ts index d7c102db0..27c8bf090 100644 --- a/apps/red-ui/src/app/services/entity-services/files-map.service.ts +++ b/apps/red-ui/src/app/services/entity-services/files-map.service.ts @@ -40,7 +40,7 @@ export class FilesMapService { const newEntities = entities.map(newEntity => { const oldEntity = this.get(key, newEntity.id); - if (oldEntity.lastProcessed !== newEntity.lastProcessed) { + if (oldEntity?.lastProcessed !== newEntity.lastProcessed) { this.fileReanalysed$.next(newEntity); } @@ -55,6 +55,11 @@ export class FilesMapService { this._map.get(key).next(newEntities); } + replace(entity: File) { + const all = this.get(entity.dossierId).filter(file => file.fileId !== entity.fileId); + this.set(entity.dossierId, [...all, entity]); + } + watch$(key: string, entityId: string): Observable { return this._entityChanged$.pipe( filter(entity => entity.id === entityId), diff --git a/apps/red-ui/src/app/state/app-state.service.ts b/apps/red-ui/src/app/state/app-state.service.ts index b0eb7a1ee..3f9b2d296 100644 --- a/apps/red-ui/src/app/state/app-state.service.ts +++ b/apps/red-ui/src/app/state/app-state.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { Dictionary, Dossier, DossierTemplate, File, IColors, IDossier } from '@red/domain'; import { ActivationEnd, Router } from '@angular/router'; import { UserService } from '@services/user.service'; -import { forkJoin, Observable, of, Subject } from 'rxjs'; +import { forkJoin, Observable, of } from 'rxjs'; import { catchError, filter, first, map, tap } from 'rxjs/operators'; import { currentComponentRoute, FALLBACK_COLOR, hexToRgb } from '@utils/functions'; import { DossiersService } from '@services/entity-services/dossiers.service'; @@ -11,7 +11,6 @@ import { FilesService } from '@services/entity-services/files.service'; import { DictionaryService } from '@shared/services/dictionary.service'; import { DossierTemplatesService } from '@services/entity-services/dossier-templates.service'; import { FileAttributesService } from '@services/entity-services/file-attributes.service'; -import { ReanalysisService } from '@services/reanalysis.service'; import { DossierStatsService } from '@services/entity-services/dossier-stats.service'; import { FilesMapService } from '@services/entity-services/files-map.service'; @@ -24,9 +23,6 @@ export interface AppState { providedIn: 'root', }) export class AppStateService { - readonly fileChanged$ = new Subject(); - readonly fileReanalysed$ = new Subject(); - private _appState: AppState = {}; constructor( @@ -34,7 +30,6 @@ export class AppStateService { private readonly _userService: UserService, private readonly _dossiersService: DossiersService, private readonly _filesService: FilesService, - private readonly _reanalysisService: ReanalysisService, private readonly _dictionaryService: DictionaryService, private readonly _dossierTemplatesService: DossierTemplatesService, private readonly _dossierStatsService: DossierStatsService, @@ -143,26 +138,24 @@ export class AppStateService { return Promise.all(mappedDossiers$); } - async reloadActiveFile() { - const activeDossier = this._dossiersService.activeDossier; + async reloadFile(dossierId: string, fileId: string) { + const dossier = this._dossiersService.find(dossierId); + const oldFile = this._filesMapService.get(dossierId, fileId); - if (!this.activeFile || !activeDossier) { + if (!oldFile || !dossier) { return null; } - const oldProcessedDate = this.activeFile.lastProcessed; - const iFile = await this._filesService.get(activeDossier.dossierId, this.activeFileId).toPromise(); - const activeFile = new File( + const iFile = await this._filesService.get(dossierId, fileId).toPromise(); + + const newFile = new File( iFile, this._userService.getNameForId(iFile.currentReviewer), - this._fileAttributesService.getFileAttributeConfig(activeDossier.dossierTemplateId), + this._fileAttributesService.getFileAttributeConfig(dossier.dossierTemplateId), ); - if (activeFile.lastProcessed !== oldProcessedDate) { - this.fileReanalysed$.next(activeFile); - } - this.fileChanged$.next(activeFile); - return activeFile; + this._filesMapService.replace(newFile); + return newFile; } async getFiles(dossier = this._dossiersService.activeDossier) { @@ -178,10 +171,6 @@ export class AppStateService { return newFiles; } - async reanalyzeDossier({ id } = this._dossiersService.activeDossier) { - await this._reanalysisService.reanalyzeDossier(id, true).toPromise(); - } - async activateFile(dossierId: string, fileId: string) { if (this._dossiersService.activeDossierId === dossierId && this.activeFileId === fileId) { return;