UI: Fixed invalidate cache for change entityType or entitySubtype in entity-autocomplete component

This commit is contained in:
Vladyslav_Prykhodko 2021-08-12 19:52:43 +03:00
parent eb8f115c69
commit f9552e09c2

View File

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