RED-6786: update editor

This commit is contained in:
Dan Percic 2023-05-26 00:43:37 +03:00
parent 0c4a705e52
commit e2bf87d147
4 changed files with 51 additions and 49 deletions

View File

@ -3,7 +3,7 @@ import { ActivatedRoute } from '@angular/router';
import { DictionaryManagerComponent } from '@shared/components/dictionary-manager/dictionary-manager.component';
import { DictionaryService } from '@services/entity-services/dictionary.service';
import { getCurrentUser, getParam, IqserPermissionsService, List, LoadingService } from '@iqser/common-ui';
import { BehaviorSubject, firstValueFrom, lastValueFrom } from 'rxjs';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { DICTIONARY_TO_ENTRY_TYPE_MAP, DICTIONARY_TYPE_KEY_MAP, DictionaryType, DOSSIER_TEMPLATE_ID, ENTITY_TYPE, User } from '@red/domain';
import { PermissionsService } from '@services/permissions.service';
import { ROLES } from '@users/roles';
@ -47,16 +47,14 @@ export class DictionaryScreenComponent implements OnInit {
this._loadingService.start();
try {
await lastValueFrom(
this._dictionaryService.saveEntries(
entries,
this.initialEntries$.value,
this.#dossierTemplateId,
this.entityType,
null,
true,
DICTIONARY_TO_ENTRY_TYPE_MAP[this.type],
),
await this._dictionaryService.saveEntries(
entries,
this.initialEntries$.value,
this.#dossierTemplateId,
this.entityType,
null,
true,
DICTIONARY_TO_ENTRY_TYPE_MAP[this.type],
);
await this._loadEntries();
} catch (e) {

View File

@ -50,17 +50,14 @@ export class EditDossierDictionaryComponent implements EditDossierSectionInterfa
async save(): EditDossierSaveResult {
try {
await firstValueFrom(
this._dictionaryService.saveEntries(
this._dictionaryManager.editor.currentEntries,
this._dictionaryManager.initialEntries,
this.dossier.dossierTemplateId,
this.dossierDictionary.type,
this.dossier.id,
false,
),
await this._dictionaryService.saveEntries(
this._dictionaryManager.editor.currentEntries,
this._dictionaryManager.initialEntries,
this.dossier.dossierTemplateId,
this.dossierDictionary.type,
this.dossier.id,
false,
);
await this._updateDossierDictionary();
return { success: true };
} catch (error) {

View File

@ -92,7 +92,6 @@ export class DictionaryManagerComponent implements OnChanges {
this._onDossierChanged(dossier.dossierTemplateId, dossier.id)
.pipe(take(1))
// eslint-disable-next-line rxjs/no-ignored-subscription
.subscribe(entries => {
this.diffEditorText = entries;
this.showDiffEditor = true;

View File

@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { forkJoin, Observable, of, throwError, zip } from 'rxjs';
import { firstValueFrom, forkJoin, Observable, of, throwError } from 'rxjs';
import { EntitiesService, List, QueryParam, RequiredParam, Toaster, Validate } from '@iqser/common-ui';
import { Dictionary, DictionaryEntryType, DictionaryEntryTypes, IDictionary, IUpdateDictionary, SuperTypes } from '@red/domain';
import { catchError, map, switchMap, tap } from 'rxjs/operators';
@ -95,7 +95,7 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
);
}
saveEntries(
async saveEntries(
entries: List,
initialEntries: List,
dossierTemplateId: string,
@ -103,42 +103,50 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
dossierId: string,
showToast = true,
dictionaryEntryType = DictionaryEntryTypes.ENTRY,
): Observable<unknown> {
const entriesToAdd = entries.map(e => e.trim()).filter(e => !!e);
const deletedEntries = initialEntries.filter(e => !entries.includes(e));
console.log({ entriesToAdd, deletedEntries });
): Promise<unknown> {
const entriesToAdd: Array<string> = [];
const deletedEntries: Array<string> = [];
for (let i = 0; i < entries.length; i++) {
if (!entries.at(i).trim()) {
continue;
}
entriesToAdd.push(entries.at(i));
}
for (let i = 0; i < initialEntries.length; i++) {
if (entries.includes(initialEntries.at(i))) {
continue;
}
deletedEntries.push(initialEntries.at(i));
}
// remove empty lines
const invalidRowsExist = entriesToAdd.filter(e => e.length < MIN_WORD_LENGTH);
if (invalidRowsExist.length === 0) {
// can add at least 1 - block UI
const obs: Observable<IDictionary>[] = [];
const promises: Promise<IDictionary>[] = [];
if (deletedEntries.length) {
obs.push(this._deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId));
promises.push(firstValueFrom(this._deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId)));
}
if (entriesToAdd.filter(e => !initialEntries.includes(e)).length) {
obs.push(this._addEntries(entriesToAdd, dossierTemplateId, type, dictionaryEntryType, dossierId));
promises.push(firstValueFrom(this._addEntries(entriesToAdd, dossierTemplateId, type, dictionaryEntryType, dossierId)));
}
try {
await Promise.all(promises);
if (showToast) {
this._toaster.success(_('dictionary-overview.success.generic'));
}
return;
} catch (error) {
if ((error as HttpErrorResponse).status === 400) {
this._toaster.error(_('dictionary-overview.error.400'));
} else {
this._toaster.error(_('dictionary-overview.error.generic'));
}
return;
}
return zip(obs).pipe(
switchMap(dictionary => this._dossierTemplateStatsService.getFor([dossierTemplateId]).pipe(map(() => dictionary))),
tap({
next: () => {
if (showToast) {
this._toaster.success(_('dictionary-overview.success.generic'));
}
},
error: error => {
if (error.status === 400) {
this._toaster.error(_('dictionary-overview.error.400'));
} else {
this._toaster.error(_('dictionary-overview.error.generic'));
}
},
}),
);
}
this._toaster.error(_('dictionary-overview.error.entries-too-short'));
return throwError(() => 'Entries too short');
throw new Error('Entries too short');
}
hasManualType(dossierTemplateId: string): boolean {