From 7c348c31ad65cabc0df35dcb819e284fcea53966 Mon Sep 17 00:00:00 2001 From: Timo Date: Thu, 3 Dec 2020 14:22:23 +0200 Subject: [PATCH] messages for dict, smarter update and corner case fixes, sorted entries --- .../dictionary-overview-screen.component.ts | 46 +++++++++++-------- apps/red-ui/src/assets/i18n/en.json | 6 ++- .../lib/api/dictionaryController.service.ts | 15 ++++-- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/apps/red-ui/src/app/screens/admin/dictionary-overview-screen/dictionary-overview-screen.component.ts b/apps/red-ui/src/app/screens/admin/dictionary-overview-screen/dictionary-overview-screen.component.ts index 64700284d..1304c7674 100644 --- a/apps/red-ui/src/app/screens/admin/dictionary-overview-screen/dictionary-overview-screen.component.ts +++ b/apps/red-ui/src/app/screens/admin/dictionary-overview-screen/dictionary-overview-screen.component.ts @@ -10,6 +10,7 @@ import { reference } from '../../../utils/functions'; import { debounce } from '../../../utils/debounce'; import { NotificationService, NotificationType } from '../../../notification/notification.service'; import { TranslateService } from '@ngx-translate/core'; +import { Observable } from 'rxjs'; declare var ace; @@ -64,7 +65,7 @@ export class DictionaryOverviewScreenComponent { private _initialize() { this._dictionaryControllerService.getDictionaryForType(this.dictionary.type).subscribe((data) => { - this.initialDictionaryEntries = data.entries; + this.initialDictionaryEntries = data.entries.sort((str1, str2) => str1.localeCompare(str2, undefined, { sensitivity: 'accent' })); this.revert(); }); } @@ -155,36 +156,43 @@ export class DictionaryOverviewScreenComponent { } get hasChanges() { - return this.changedLines.length > 0 || this.currentDictionaryEntries.length !== this.initialDictionaryEntries.length; + return this.activeEditMarkers.length > 0 || this.currentDictionaryEntries.length < this.initialDictionaryEntries.length; } async saveEntries() { let entriesToAdd = []; - const entriesToDelete = []; this.currentDictionaryEntries.forEach((currentEntry) => { - if (this.initialDictionaryEntries.indexOf(currentEntry) < 0) { - entriesToAdd.push(currentEntry); - } + entriesToAdd.push(currentEntry); }); - this.initialDictionaryEntries.forEach((initialEntry) => { - if (this.currentDictionaryEntries.indexOf(initialEntry) < 0) { - entriesToDelete.push(initialEntry); - } - }); - // remove empty lines entriesToAdd = entriesToAdd.filter((e) => e && e.trim().length > 0).map((e) => e.trim()); - const invalidRowsExist = entriesToAdd.filter((e) => e.length < DictionaryOverviewScreenComponent.MIN_WORD_LENGTH); - if (invalidRowsExist.length === 0) { + // can add at least 1 + let obs: Observable; if (entriesToAdd.length > 0) { - await this._dictionaryControllerService.addEntry(entriesToAdd, this.dictionary.type).toPromise(); + obs = this._dictionaryControllerService.addEntry(entriesToAdd, this.dictionary.type, true); + } else { + obs = this._dictionaryControllerService.deleteEntries(this.initialDictionaryEntries, this.dictionary.type); } - if (entriesToDelete.length > 0) { - await this._dictionaryControllerService.deleteEntries(entriesToDelete, this.dictionary.type).toPromise(); - } - this._initialize(); + + obs.subscribe( + () => { + this._initialize(); + this._notificationService.showToastNotification( + this._translateService.instant('dictionary-overview.success.generic'), + null, + NotificationType.SUCCESS + ); + }, + () => { + this._notificationService.showToastNotification( + this._translateService.instant('dictionary-overview.error.generic'), + null, + NotificationType.ERROR + ); + } + ); } else { this._notificationService.showToastNotification( this._translateService.instant('dictionary-overview.error.entries-to-short'), diff --git a/apps/red-ui/src/assets/i18n/en.json b/apps/red-ui/src/assets/i18n/en.json index 6c024f78a..9ed1c3f65 100644 --- a/apps/red-ui/src/assets/i18n/en.json +++ b/apps/red-ui/src/assets/i18n/en.json @@ -500,7 +500,11 @@ }, "dictionary-overview": { "error": { - "entries-to-short": "Some entries of the dictionary are below the minimum length of 2. These are highlighted with red!" + "entries-to-short": "Some entries of the dictionary are below the minimum length of 2. These are highlighted with red!", + "generic": "Something went wrong... Dictionary update failed!" + }, + "success": { + "generic": "Dictionary updated!" }, "search": "Search...", "save-changes": "Save Changes", diff --git a/libs/red-ui-http/src/lib/api/dictionaryController.service.ts b/libs/red-ui-http/src/lib/api/dictionaryController.service.ts index f03adec36..7d270e0dc 100644 --- a/libs/red-ui-http/src/lib/api/dictionaryController.service.ts +++ b/libs/red-ui-http/src/lib/api/dictionaryController.service.ts @@ -60,13 +60,14 @@ export class DictionaryControllerService { * None * @param body entries * @param type type + * @param removeCurrent removeCurrent * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. * @param reportProgress flag to report request and response progress. */ - public addEntry(body: Array, type: string, observe?: 'body', reportProgress?: boolean): Observable; - public addEntry(body: Array, type: string, observe?: 'response', reportProgress?: boolean): Observable>; - public addEntry(body: Array, type: string, observe?: 'events', reportProgress?: boolean): Observable>; - public addEntry(body: Array, type: string, observe: any = 'body', reportProgress: boolean = false): Observable { + public addEntry(body: Array, type: string, removeCurrent?: boolean, observe?: 'body', reportProgress?: boolean): Observable; + public addEntry(body: Array, type: string, removeCurrent?: boolean, observe?: 'response', reportProgress?: boolean): Observable>; + public addEntry(body: Array, type: string, removeCurrent?: boolean, observe?: 'events', reportProgress?: boolean): Observable>; + public addEntry(body: Array, type: string, removeCurrent?: boolean, observe: any = 'body', reportProgress: boolean = false): Observable { if (body === null || body === undefined) { throw new Error('Required parameter body was null or undefined when calling addEntry.'); } @@ -75,6 +76,11 @@ export class DictionaryControllerService { throw new Error('Required parameter type was null or undefined when calling addEntry.'); } + let queryParameters = new HttpParams({ encoder: new CustomHttpUrlEncodingCodec() }); + if (removeCurrent !== undefined && removeCurrent !== null) { + queryParameters = queryParameters.set('removeCurrent', removeCurrent); + } + let headers = this.defaultHeaders; // authentication (RED-OAUTH) required @@ -99,6 +105,7 @@ export class DictionaryControllerService { return this.httpClient.request('post', `${this.basePath}/dictionary/${encodeURIComponent(String(type))}`, { body: body, + params: queryParameters, withCredentials: this.configuration.withCredentials, headers: headers, observe: observe,