Skip to content

Commit bcc3d39

Browse files
authored
Fixed and improved Smart Search events to use cached result (#413)
1 parent 7d536c2 commit bcc3d39

File tree

10 files changed

+56
-32
lines changed

10 files changed

+56
-32
lines changed

src/app/dashboard-module/domain-module/common/detail-component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class DetailComponent {
3838
}
3939

4040
protected showResumed(value: string, length: number): string {
41-
return DataUtils.showResumed(value, length);
41+
return DataUtils.showResumed(value, length);
4242
}
4343

4444
scrollToElement($element: any): void {

src/app/dashboard-module/domain-module/common/element-autocomplete/element-autocomplete.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<mat-form-field style="width: 100%;">
22
<mat-label>Smart Search</mat-label>
3-
<input id="smartSearchInput" #smartSearchInput class="smart-search" matInput (click)="loadComponent()"
3+
<input id="smartSearchInput" #smartSearchInput class="smart-search" matInput (keyup)="loadComponent($event)" (click)="loadComponent($event)"
44
matTooltip="Use 'S:' for Switchers, 'G:' for Groups and 'C:' for Components"
55
readonly="{{ lockFilter }}"
66
[formControl]="smartSearchFormControl"

src/app/dashboard-module/domain-module/common/element-autocomplete/element-autocomplete.component.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Component, OnInit, OnDestroy, Input } from '@angular/core';
2-
import { takeUntil, startWith, map } from 'rxjs/operators';
2+
import { takeUntil, startWith, map, debounceTime } from 'rxjs/operators';
33
import { Subject, Observable } from 'rxjs';
4-
import { QueryRef } from 'apollo-angular';
54
import { ConsoleLogger } from 'src/app/_helpers/console-logger';
65
import { FormControl } from '@angular/forms';
76
import { DomainService } from 'src/app/services/domain.service';
7+
import { Group } from 'src/app/model/group';
8+
import { ApolloQueryResult } from '@apollo/client';
89

910
@Component({
1011
selector: 'element-autocomplete',
@@ -25,13 +26,18 @@ export class ElementAutocompleteComponent implements OnInit, OnDestroy {
2526
smartSearchFormControl = new FormControl('');
2627
searchListItems: any[] = [];
2728
searchedValues: Observable<string[]>;
28-
private query: QueryRef<any>;
29+
private query: Observable<ApolloQueryResult<any>>;
2930

3031
constructor(
3132
private readonly domainService: DomainService
3233
) { }
3334

3435
ngOnInit() {
36+
this.searchedValues = this.smartSearchFormControl.valueChanges.pipe(
37+
startWith(''),
38+
map(value => this.filter(value))
39+
);
40+
3541
if (this.value) {
3642
this.smartSearchFormControl.setValue(this.value);
3743
}
@@ -42,26 +48,27 @@ export class ElementAutocompleteComponent implements OnInit, OnDestroy {
4248
this.unsubscribe.complete();
4349
}
4450

45-
loadComponent() {
46-
if (!this.query) {
47-
this.loadKeys(this.parentComponent.getDomainId());
51+
loadComponent($event: any) {
52+
if ($event instanceof MouseEvent) {
53+
this.loadKeys(this.parentComponent.getDomainId(), true);
4854
} else {
49-
this.query?.refetch();
55+
this.loadKeys(this.parentComponent.getDomainId());
5056
}
5157
}
5258

5359
showTooltip(item: any): string {
5460
return `[${item.name}]: ${item.description}`;
5561
}
5662

57-
private loadKeys(domainId: string): void {
58-
if (!domainId)
63+
private loadKeys(domainId: string, forceRefresh = false): void {
64+
if (!domainId) {
5965
return;
66+
}
6067

6168
this.query = this.domainService.executeConfigurationTreeQuery(
62-
domainId, this.switchers, this.groups, this.components);
69+
domainId, this.switchers, this.groups, this.components, forceRefresh);
6370

64-
this.query.valueChanges.pipe(takeUntil(this.unsubscribe))
71+
this.query.pipe(takeUntil(this.unsubscribe), debounceTime(500))
6572
.subscribe({
6673
next: result => {
6774
if (result?.data?.domain?.group) {
@@ -77,16 +84,12 @@ export class ElementAutocompleteComponent implements OnInit, OnDestroy {
7784
ConsoleLogger.printError(error);
7885
}
7986
});
80-
81-
this.searchedValues = this.smartSearchFormControl.valueChanges.pipe(
82-
startWith(''),
83-
map(value => this.filter(value))
84-
);
8587
}
8688

87-
private loadByComponent(groups?: any) {
88-
if (!this.components)
89+
private loadByComponent(groups?: Group[]) {
90+
if (!this.components) {
8991
return;
92+
}
9093

9194
const filtered = groups?.map(
9295
group => group.config?.map(config => {
@@ -103,9 +106,10 @@ export class ElementAutocompleteComponent implements OnInit, OnDestroy {
103106
this.searchListItems.push(...filtered.flat());
104107
}
105108

106-
private loadBySwitcher(groups: any) {
107-
if (!this.switchers || !groups)
109+
private loadBySwitcher(groups: Group[]) {
110+
if (!this.switchers || !groups) {
108111
return;
112+
}
109113

110114
const filtered = groups.map(
111115
group => group.config?.map(config => {
@@ -122,9 +126,10 @@ export class ElementAutocompleteComponent implements OnInit, OnDestroy {
122126
this.searchListItems.push(...filtered.flat());
123127
}
124128

125-
private loadByGroup(groups: any) {
126-
if (!this.groups || !groups)
129+
private loadByGroup(groups: Group[]) {
130+
if (!this.groups || !groups) {
127131
return;
132+
}
128133

129134
const filtered = groups.map(group => {
130135
return {

src/app/dashboard-module/domain-module/config/config-detail/config-detail.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,19 @@ export class ConfigDetailComponent extends DetailComponent implements OnInit, On
111111
this.domainId = params.domainid;
112112
this.domainName = params.name;
113113
});
114-
114+
115115
this.route.params.subscribe(params => {
116116
this.groupId = params.groupid;
117117
this.configId = params.configid;
118-
this.loadConfig();
118+
119+
const configFromState = this.router.getCurrentNavigation()?.extras.state?.element;
120+
if (configFromState) {
121+
this.config = JSON.parse(configFromState);
122+
this.updateData(this.config);
123+
this.loadStrategies();
124+
} else {
125+
this.loadConfig();
126+
}
119127
});
120128

121129
this.hasNewStrategy = false;
@@ -307,7 +315,7 @@ export class ConfigDetailComponent extends DetailComponent implements OnInit, On
307315
if ($event.reloadPermissions) {
308316
this.readPermissionToObject();
309317
}
310-
318+
311319
this.loadStrategies();
312320
this.disableMetrics = this.isMetricDisabled();
313321
}

src/app/dashboard-module/domain-module/group/group-detail/group-detail.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
</div>
6767
</section>
6868
</div>
69-
<div *ngIf="environments" class="config-list">
69+
<div *ngIf="!loading && environments" class="config-list">
7070
<hr>
7171
<app-config-list [childEnvironmentEmitter]="childEnvironmentEmitter" [environments]="environments"></app-config-list>
7272
</div>

src/app/dashboard-module/domain-module/group/group-detail/group-detail.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ export class GroupDetailComponent extends DetailComponent implements OnInit, OnD
6868
});
6969

7070
this.route.params.subscribe(params => {
71+
this.detailBodyStyle = 'detail-body loading';
72+
this.loading = true;
7173
this.groupId = params.groupid;
72-
this.loadGroup();
74+
75+
const groupFromState = this.router.getCurrentNavigation()?.extras.state?.element;
76+
if (groupFromState) {
77+
this.updateData(JSON.parse(groupFromState));
78+
} else {
79+
this.loadGroup();
80+
}
7381
});
7482
}
7583

src/app/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ <h1>
111111
</h1>
112112
<p>
113113
Our team is diverse and vital to keep this project growing.<br>
114-
As an open-source project, we appreciate any feedback and contribution from the community.<br>
114+
As an open-source project, we appreciate any feedback and contribution from the community.<br><br>
115115
Your support will help us to continue evolving and providing the best Feature Management experience.
116116
</p>
117117
<br>

src/app/model/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Admin } from "./admin";
22

33
export class Config {
4+
_id: string;
45
id: string;
56
key: string;
67
description: string;

src/app/model/group.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Admin } from './admin';
22
import { Config } from './config';
33

44
export class Group {
5+
_id: string;
56
id: string;
67
name: string;
78
description: string;

src/app/services/domain.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ export class DomainService extends ApiService {
119119
});
120120
}
121121

122-
public executeConfigurationTreeQuery(domainId: string, switchers: boolean, groups: boolean, components: boolean) {
123-
return this.apollo.watchQuery<any>({
122+
public executeConfigurationTreeQuery(domainId: string, switchers: boolean, groups: boolean, components: boolean, forceFetch = false) {
123+
return this.apollo.query<any>({
124124
query: configurationTreeQuery(switchers, groups, components),
125+
fetchPolicy: forceFetch ? 'network-only' : 'cache-first',
125126
variables: {
126127
id: domainId,
127128
}

0 commit comments

Comments
 (0)