From 0aae993091612b801c76fbf0a500d5e2caf2dc08 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Thu, 27 May 2021 18:33:06 +0300 Subject: [PATCH] Improve entity autocomplete --- .../entity/entity-autocomplete.component.ts | 54 ++++++------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/ui-ngx/src/app/shared/components/entity/entity-autocomplete.component.ts b/ui-ngx/src/app/shared/components/entity/entity-autocomplete.component.ts index d0ab629272..fef58ca22b 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-autocomplete.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-autocomplete.component.ts @@ -270,47 +270,27 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit } } - writeValue(value: string | EntityId | null): void { + async writeValue(value: string | EntityId | null): Promise { this.searchText = ''; - if (value != null) { + if (value !== null && (typeof value === 'string' || (value.entityType && value.id))) { + let targetEntityType: EntityType; + let id: string; if (typeof value === 'string') { - const targetEntityType = this.checkEntityType(this.entityTypeValue); - this.entityService.getEntity(targetEntityType, value, {ignoreLoading: true, ignoreErrors: true}).subscribe( - (entity) => { - this.modelValue = entity.id.id; - this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false}); - this.entityChanged.emit(entity); - }, - () => { - this.modelValue = null; - this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false}); - this.entityChanged.emit(null); - if (value !== null) { - this.propagateChange(this.modelValue); - } - } - ); - } else if (value.entityType && value.id) { - const targetEntityType = this.checkEntityType(value.entityType); - this.entityService.getEntity(targetEntityType, value.id, {ignoreLoading: true, ignoreErrors: true}).subscribe( - (entity) => { - this.modelValue = entity.id.id; - this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false}); - this.entityChanged.emit(entity); - }, - () => { - this.modelValue = null; - this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false}); - this.entityChanged.emit(null); - if (value !== null) { - this.propagateChange(this.modelValue); - } - } - ); + targetEntityType = this.checkEntityType(this.entityTypeValue); + id = value; } else { - this.modelValue = null; - this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false}); + targetEntityType = this.checkEntityType(value.entityType); + id = value.id; } + let entity: BaseData = null; + try { + entity = await this.entityService.getEntity(targetEntityType, id, {ignoreLoading: true, ignoreErrors: true}).toPromise(); + } catch (e) { + this.propagateChange(null); + } + this.modelValue = entity !== null ? entity.id.id : null; + this.selectEntityFormGroup.get('entity').patchValue(entity !== null ? entity : '', {emitEvent: false}); + this.entityChanged.emit(entity); } else { this.modelValue = null; this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});