state management for file attributes config

This commit is contained in:
Timo 2021-04-27 16:42:38 +03:00
parent 5c52b55d6c
commit 3d8a664f99
4 changed files with 43 additions and 33 deletions

View File

@ -1,16 +1,15 @@
import { FileAttributeConfig, FileStatus } from '@redaction/red-ui-http';
import { FileAttributeConfig, FileAttributesConfig, FileStatus } from '@redaction/red-ui-http';
import { StatusSorter } from '../../utils/sorters/status-sorter';
export class FileStatusWrapper {
primaryAttribute: string;
searchField: string;
constructor(public fileStatus: FileStatus, public reviewerName: string, fileAttributesConfig?: FileAttributeConfig[]) {
constructor(public fileStatus: FileStatus, public reviewerName: string, public ruleSetId: string, fileAttributesConfig?: FileAttributesConfig) {
this.searchField = fileStatus.filename;
if (fileAttributesConfig) {
const primary = fileAttributesConfig.find((c) => c.primaryAttribute);
const primary = fileAttributesConfig.fileAttributeConfigs?.find((c) => c.primaryAttribute);
if (primary && fileStatus.fileAttributes?.attributeIdToValue) {
this.primaryAttribute = fileStatus.fileAttributes?.attributeIdToValue[primary.id];
this.searchField += ' ' + this.primaryAttribute;

View File

@ -17,7 +17,7 @@
<div class="right-content" redactionHasScrollbar>
<div class="section">
<div class="attribute" *ngFor="let attr of fileAttributesConfig">
<div class="attribute" *ngFor="let attr of fileAttributesConfig?.fileAttributeConfigs">
<div class="small-label">{{ attr.label }}:</div>
<div>{{ (file.fileAttributes?.attributeIdToValue)[attr.id] || '-' }}</div>
</div>

View File

@ -1,5 +1,5 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FileAttributeConfig, FileStatus } from '@redaction/red-ui-http';
import { FileAttributesConfig, FileStatus } from '@redaction/red-ui-http';
import { AppStateService } from '../../../../state/app-state.service';
import { ProjectsDialogService } from '../../services/projects-dialog.service';
@ -12,10 +12,10 @@ export class DocumentInfoComponent implements OnInit {
@Input() file: FileStatus;
@Output() closeDocumentInfoView = new EventEmitter();
public fileAttributesConfig: FileAttributeConfig[];
public fileAttributesConfig: FileAttributesConfig;
constructor(private readonly _appStateService: AppStateService, private readonly _dialogService: ProjectsDialogService) {
this.fileAttributesConfig = this._appStateService.fileAttributesConfig;
this.fileAttributesConfig = this._appStateService.activeFileAttributesConfig;
}
ngOnInit(): void {}

View File

@ -30,7 +30,7 @@ export interface AppState {
activeProjectId: string;
activeFileId: string;
activeRuleSetId: string;
activeFileAttributesConfig: FileAttributeConfig[];
fileAttributesConfig: { [key: string]: FileAttributesConfig };
activeDictionaryType: string;
totalAnalysedPages?: number;
totalDocuments?: number;
@ -63,10 +63,10 @@ export class AppStateService {
this._appState = {
projects: [],
ruleSets: [],
fileAttributesConfig: {},
activeProjectId: null,
activeFileId: null,
activeRuleSetId: null,
activeFileAttributesConfig: [],
activeDictionaryType: null,
versions: {}
};
@ -171,8 +171,10 @@ export class AppStateService {
return this.ruleSets.find((rs) => rs.ruleSetId === id);
}
public get fileAttributesConfig(): FileAttributeConfig[] {
return this._appState.activeFileAttributesConfig;
public get activeFileAttributesConfig(): FileAttributesConfig {
if (this.activeProject) {
return this._appState.fileAttributesConfig[this.activeProject.ruleSetId];
}
}
get activeDictionaryType(): string {
@ -247,14 +249,7 @@ export class AppStateService {
for (const projectId of Object.keys(fileData)) {
const project = mappedProjects.find((p) => p.projectId === projectId);
const fileAttributesConfig = await this._fileAttributesService
.getFileAttributesConfiguration(project.ruleSetId)
.pipe(catchError(() => of({})))
.toPromise();
console.log(fileAttributesConfig);
this._processFiles(project, fileData[projectId], emitEvents, fileAttributesConfig);
this._processFiles(project, fileData[projectId], emitEvents);
}
this._appState.projects = mappedProjects;
@ -268,14 +263,17 @@ export class AppStateService {
}
async reloadActiveFile() {
if (!this.activeFile) return null;
if (!this.activeFile) {
return null;
}
const oldProcessedDate = this.activeFile.lastProcessed;
const activeFile = await this._statusControllerService.getFileStatus(this.activeProjectId, this.activeFileId).toPromise();
const activeFileWrapper = new FileStatusWrapper(
activeFile,
this._userService.getNameForId(activeFile.currentReviewer),
this._appState.activeFileAttributesConfig
this.activeProject.ruleSetId,
this._appState.fileAttributesConfig[this.activeProject.ruleSetId]
);
this.activeProject.files = this.activeProject.files.map((file) => (file.fileId === activeFileWrapper.fileId ? activeFileWrapper : file));
@ -294,15 +292,10 @@ export class AppStateService {
}
const files = await this._statusControllerService.getProjectStatus(project.projectId).toPromise();
const fileAttributesConfig = await this._fileAttributesService
.getFileAttributesConfiguration(project.ruleSetId)
.pipe(catchError(() => of({})))
.toPromise();
return this._processFiles(project, files, emitEvents, fileAttributesConfig);
return this._processFiles(project, files, emitEvents);
}
private _processFiles(project: ProjectWrapper, files: FileStatus[], emitEvents: boolean = true, fileAttributesConfig: FileAttributesConfig) {
private _processFiles(project: ProjectWrapper, files: FileStatus[], emitEvents: boolean = true) {
const oldFiles = [...project.files];
const fileStatusChangedEvent = [];
@ -316,7 +309,8 @@ export class AppStateService {
const fileStatusWrapper = new FileStatusWrapper(
file,
this._userService.getNameForId(file.currentReviewer),
fileAttributesConfig?.fileAttributeConfigs
project.ruleSetId,
this._appState.fileAttributesConfig[project.ruleSetId]
);
if (JSON.stringify(oldFile) !== JSON.stringify(fileStatusWrapper)) {
fileStatusChangedEvent.push(fileStatusWrapper);
@ -330,13 +324,24 @@ export class AppStateService {
}
// emit for new file
if (!found) {
const fsw = new FileStatusWrapper(file, this._userService.getNameForId(file.currentReviewer), fileAttributesConfig?.fileAttributeConfigs);
const fsw = new FileStatusWrapper(
file,
this._userService.getNameForId(file.currentReviewer),
project.ruleSetId,
this._appState.fileAttributesConfig[project.ruleSetId]
);
fileStatusChangedEvent.push(fsw);
}
}
project.files = files.map(
(f) => new FileStatusWrapper(f, this._userService.getNameForId(f.currentReviewer), fileAttributesConfig?.fileAttributeConfigs)
(f) =>
new FileStatusWrapper(
f,
this._userService.getNameForId(f.currentReviewer),
project.ruleSetId,
this._appState.fileAttributesConfig[project.ruleSetId]
)
);
this._computeStats();
@ -360,7 +365,6 @@ export class AppStateService {
this._appState.activeProjectId = projectId;
if (!this.activeProject) {
this._appState.activeProjectId = null;
this._appState.activeFileAttributesConfig = [];
this._router.navigate(['/main/projects']);
return;
}
@ -482,6 +486,13 @@ export class AppStateService {
async loadAllRuleSets() {
this._appState.ruleSets = await this._ruleSetControllerService.getAllRuleSets1().toPromise();
this._appState.fileAttributesConfig = {};
for (const ruleSet of this._appState.ruleSets) {
this._appState.fileAttributesConfig[ruleSet.ruleSetId] = await this._fileAttributesService
.getFileAttributesConfiguration(ruleSet.ruleSetId)
.pipe(catchError(() => of({})))
.toPromise();
}
}
async loadRuleSetsIfNecessary() {