Commit f9552e09c25c6eda35132a8741645fc5a3c8470f

Authored by Vladyslav_Prykhodko
1 parent eb8f115c

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

... ... @@ -26,8 +26,8 @@ import {
26 26 ViewChild
27 27 } from '@angular/core';
28 28 import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
29   -import { Observable, of } from 'rxjs';
30   -import { catchError, debounceTime, distinctUntilChanged, map, share, switchMap, tap } from 'rxjs/operators';
  29 +import { merge, Observable, of, Subject } from 'rxjs';
  30 +import { catchError, debounceTime, map, share, switchMap, tap } from 'rxjs/operators';
31 31 import { Store } from '@ngrx/store';
32 32 import { AppState } from '@app/core/core.state';
33 33 import { TranslateService } from '@ngx-translate/core';
... ... @@ -66,6 +66,7 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
66 66 this.entityTypeValue = entityType;
67 67 this.load();
68 68 this.reset();
  69 + this.refresh$.next([]);
69 70 this.dirty = true;
70 71 }
71 72 }
... ... @@ -78,6 +79,7 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
78 79 if (currentEntity) {
79 80 if ((currentEntity as any).type !== this.entitySubtypeValue) {
80 81 this.reset();
  82 + this.refresh$.next([]);
81 83 this.dirty = true;
82 84 }
83 85 }
... ... @@ -121,6 +123,8 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
121 123
122 124 private dirty = false;
123 125
  126 + private refresh$ = new Subject<Array<BaseData<EntityId>>>();
  127 +
124 128 private propagateChange = (v: any) => { };
125 129
126 130 constructor(private store: Store<AppState>,
... ... @@ -140,27 +144,29 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
140 144 }
141 145
142 146 ngOnInit() {
143   - this.filteredEntities = this.selectEntityFormGroup.get('entity').valueChanges
144   - .pipe(
145   - debounceTime(150),
146   - tap(value => {
147   - let modelValue;
148   - if (typeof value === 'string' || !value) {
149   - modelValue = null;
150   - } else {
151   - modelValue = value.id.id;
152   - }
153   - this.updateView(modelValue, value);
154   - if (value === null) {
155   - this.clear();
156   - }
157   - }),
158   - // startWith<string | BaseData<EntityId>>(''),
159   - map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
160   - distinctUntilChanged(),
161   - switchMap(name => this.fetchEntities(name)),
162   - share()
163   - );
  147 + this.filteredEntities = merge(
  148 + this.refresh$.asObservable(),
  149 + this.selectEntityFormGroup.get('entity').valueChanges
  150 + .pipe(
  151 + debounceTime(150),
  152 + tap(value => {
  153 + let modelValue;
  154 + if (typeof value === 'string' || !value) {
  155 + modelValue = null;
  156 + } else {
  157 + modelValue = value.id.id;
  158 + }
  159 + this.updateView(modelValue, value);
  160 + if (value === null) {
  161 + this.clear();
  162 + }
  163 + }),
  164 + // startWith<string | BaseData<EntityId>>(''),
  165 + map(value => value ? (typeof value === 'string' ? value : value.name) : ''),
  166 + switchMap(name => this.fetchEntities(name)),
  167 + share()
  168 + )
  169 + );
164 170 }
165 171
166 172 ngAfterViewInit(): void {}
... ...