diff --git a/apps/red-ui/src/app/services/entity-services/dictionary.service.ts b/apps/red-ui/src/app/services/entity-services/dictionary.service.ts index 789b274f0..0766d0398 100644 --- a/apps/red-ui/src/app/services/entity-services/dictionary.service.ts +++ b/apps/red-ui/src/app/services/entity-services/dictionary.service.ts @@ -103,50 +103,58 @@ export class DictionaryService extends EntitiesService dossierId: string, showToast = true, dictionaryEntryType = DictionaryEntryTypes.ENTRY, - ): Promise { + ) { const entriesToAdd: Array = []; - const deletedEntries: Array = []; + const initialEntriesSet = new Set(initialEntries); + let hasInvalidRows = false; + let hasNewEntries = false; for (let i = 0; i < entries.length; i++) { - if (!entries.at(i).trim()) { + const entry = entries.at(i); + if (!entry.trim()) { continue; } - entriesToAdd.push(entries.at(i)); - } - for (let i = 0; i < initialEntries.length; i++) { - if (entries.includes(initialEntries.at(i))) { - continue; + if (entry.length < MIN_WORD_LENGTH) { + hasInvalidRows = true; } - 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 promises: Promise[] = []; - if (deletedEntries.length) { - promises.push(firstValueFrom(this._deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId))); + if (!hasNewEntries && !initialEntriesSet.has(entry)) { + hasNewEntries = true; } - if (entriesToAdd.filter(e => !initialEntries.includes(e)).length) { - 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; - } - } - this._toaster.error(_('dictionary-overview.error.entries-too-short')); - throw new Error('Entries too short'); + entriesToAdd.push(entry); + } + if (hasInvalidRows) { + this._toaster.error(_('dictionary-overview.error.entries-too-short')); + + throw new Error('Entries too short'); + } + const deletedEntries: Array = []; + const entriesSet = new Set(entries); + for (let i = 0; i < initialEntries.length; i++) { + const entry = initialEntries.at(i); + if (entriesSet.has(entry)) { + continue; + } + deletedEntries.push(entry); + } + + try { + if (deletedEntries.length) { + await this._deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId); + } + if (hasNewEntries) { + await this._addEntries(entriesToAdd, dossierTemplateId, type, dictionaryEntryType, dossierId); + } + + if (showToast) { + this._toaster.success(_('dictionary-overview.success.generic')); + } + } catch (error) { + if ((error as HttpErrorResponse).status === 400) { + this._toaster.error(_('dictionary-overview.error.400')); + } else { + this._toaster.error(_('dictionary-overview.error.generic')); + } + } } hasManualType(dossierTemplateId: string): boolean { @@ -263,7 +271,7 @@ export class DictionaryService extends EntitiesService { key: 'removeCurrent', value: true }, ]; const url = `${this._defaultModelPath}/${type}/${dossierTemplateId}`; - return this._post(entries, url, queryParams); + return firstValueFrom(this._post(entries, url, queryParams)); } /** @@ -281,6 +289,6 @@ export class DictionaryService extends EntitiesService ? [{ key: 'dossierId', value: dossierId }] : [{ key: 'dictionaryEntryType', value: dictionaryEntryType }]; const url = `${this._defaultModelPath}/delete/${type}/${dossierTemplateId}`; - return this._post(entries, url, queryParams); + return firstValueFrom(this._post(entries, url, queryParams)); } }