Merge pull request #5081 from vvlladd28/bug/entity-autocomplete/cache

[3.3.0] UI: Fixed invalidate cache for change entityType or entitySubtype in entity-autocomplete component
This commit is contained in:
Igor Kulikov 2021-08-12 20:16:19 +03:00 committed by GitHub
commit f3dcf720e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,8 +26,8 @@ import {
ViewChild
} from '@angular/core';
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Observable, of } from 'rxjs';
import { catchError, debounceTime, distinctUntilChanged, map, share, switchMap, tap } from 'rxjs/operators';
import { merge, Observable, of, Subject } from 'rxjs';
import { catchError, debounceTime, map, share, switchMap, tap } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { AppState } from '@app/core/core.state';
import { TranslateService } from '@ngx-translate/core';
@ -66,6 +66,7 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
this.entityTypeValue = entityType;
this.load();
this.reset();
this.refresh$.next([]);
this.dirty = true;
}
}
@ -78,6 +79,7 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
if (currentEntity) {
if ((currentEntity as any).type !== this.entitySubtypeValue) {
this.reset();
this.refresh$.next([]);
this.dirty = true;
}
}
@ -121,6 +123,8 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
private dirty = false;
private refresh$ = new Subject<Array<BaseData<EntityId>>>();
private propagateChange = (v: any) => { };
constructor(private store: Store<AppState>,
@ -140,7 +144,9 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
}
ngOnInit() {
this.filteredEntities = this.selectEntityFormGroup.get('entity').valueChanges
this.filteredEntities = merge(
this.refresh$.asObservable(),
this.selectEntityFormGroup.get('entity').valueChanges
.pipe(
debounceTime(150),
tap(value => {
@ -157,9 +163,9 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
}),
// startWith<string | BaseData<EntityId>>(''),
map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
distinctUntilChanged(),
switchMap(name => this.fetchEntities(name)),
share()
)
);
}