Skip to content

Commit 4301fc6

Browse files
Alexander MatyushentsevAlexander Matyushentsev
Alexander Matyushentsev
authored and
Alexander Matyushentsev
committed
Issue #337 - remember my resource filtering preferences
1 parent a30aff9 commit 4301fc6

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

src/app/app.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import * as PropTypes from 'prop-types';
44
import * as React from 'react';
55
import { Redirect, Route, RouteComponentProps, Router, Switch } from 'react-router';
66

7+
import { services } from './shared/services';
78
import requests from './shared/services/requests';
89

10+
services.viewPreferences.init();
911
export const history = createHistory();
1012

1113
import applications from './applications';

src/app/applications/components/application-details/application-details.tsx

+19-4
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ import * as AppUtils from '../utils';
2424

2525
require('./application-details.scss');
2626

27-
export class ApplicationDetails extends React.Component<RouteComponentProps<{ name: string; namespace: string; }>, { application: appModels.Application }> {
27+
export class ApplicationDetails extends React.Component<RouteComponentProps<{ name: string; }>, { application: appModels.Application; defaultKindFilter: string[]}> {
2828

2929
public static contextTypes = {
3030
apis: PropTypes.object,
3131
};
3232

3333
private changesSubscription: Subscription;
34+
private viewPrefSubscription: Subscription;
3435
private formApi: FormApi;
3536

36-
constructor(props: RouteComponentProps<{ name: string; namespace: string; }>) {
37+
constructor(props: RouteComponentProps<{ name: string; }>) {
3738
super(props);
38-
this.state = { application: null};
39+
this.state = { application: null, defaultKindFilter: [] };
3940
}
4041

4142
private get showOperationState() {
@@ -82,6 +83,11 @@ export class ApplicationDetails extends React.Component<RouteComponentProps<{ na
8283
this.changesSubscription = appUpdates.subscribe((application) => {
8384
this.setState({ application });
8485
});
86+
this.viewPrefSubscription = services.viewPreferences.getPreferences()
87+
.map((preferences) => preferences.appDetails.defaultKindFilter)
88+
.subscribe((filter) => {
89+
this.setState({ defaultKindFilter: filter });
90+
});
8591
}
8692

8793
public componentWillUnmount() {
@@ -106,6 +112,11 @@ export class ApplicationDetails extends React.Component<RouteComponentProps<{ na
106112
selectedValues: kindsFilter,
107113
selectionChanged: (items) => {
108114
this.appContext.apis.navigation.goto('.', { kinds: `${items.join(',')}`});
115+
services.viewPreferences.updatePreferences({
116+
appDetails: {
117+
defaultKindFilter: items,
118+
},
119+
});
109120
},
110121
};
111122

@@ -233,7 +244,7 @@ export class ApplicationDetails extends React.Component<RouteComponentProps<{ na
233244
private getKindsFilter() {
234245
let kinds = new URLSearchParams(this.props.history.location.search).get('kinds');
235246
if (kinds === null) {
236-
kinds = 'Deployment,Service,Pod,StatefulSet';
247+
kinds = this.state.defaultKindFilter.join(',');
237248
}
238249
return kinds.split(',').filter((item) => !!item);
239250
}
@@ -387,5 +398,9 @@ export class ApplicationDetails extends React.Component<RouteComponentProps<{ na
387398
this.changesSubscription.unsubscribe();
388399
}
389400
this.changesSubscription = null;
401+
if (this.viewPrefSubscription) {
402+
this.viewPrefSubscription.unsubscribe();
403+
this.viewPrefSubscription = null;
404+
}
390405
}
391406
}

src/app/shared/services/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ClustersService } from './clusters-service';
44
import { ProjectsService } from './projects-service';
55
import { RepositoriesService } from './repo-service';
66
import { UserService } from './user-service';
7+
import { ViewPreferencesService } from './view-preferences-service';
78

89
export interface Services {
910
applications: ApplicationsService;
@@ -12,6 +13,7 @@ export interface Services {
1213
reposService: RepositoriesService;
1314
clustersService: ClustersService;
1415
projects: ProjectsService;
16+
viewPreferences: ViewPreferencesService;
1517
}
1618

1719
export const services: Services = {
@@ -21,4 +23,5 @@ export const services: Services = {
2123
userService: new UserService(),
2224
reposService: new RepositoriesService(),
2325
projects: new ProjectsService(),
26+
viewPreferences: new ViewPreferencesService(),
2427
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { BehaviorSubject, Observable } from 'rxjs';
2+
3+
export interface ViewPreferences {
4+
appDetails: { defaultKindFilter: string[] };
5+
}
6+
7+
const VIEW_PREFERENCES_KEY = 'view_preferences';
8+
9+
const DEFAULT_PREFERENCES = {
10+
appDetails: { defaultKindFilter: ['Deployment', 'Service', 'Pod', 'StatefulSet', 'Ingress', 'ConfigMap'] },
11+
};
12+
13+
export class ViewPreferencesService {
14+
private preferencesSubj: BehaviorSubject<ViewPreferences>;
15+
16+
public init() {
17+
if (!this.preferencesSubj) {
18+
this.preferencesSubj = new BehaviorSubject(this.loadPreferences());
19+
window.addEventListener('storage', () => {
20+
this.preferencesSubj.next(this.loadPreferences());
21+
});
22+
}
23+
}
24+
25+
public getPreferences(): Observable<ViewPreferences> {
26+
return this.preferencesSubj;
27+
}
28+
29+
public updatePreferences(change: Partial<ViewPreferences>) {
30+
const nextPref = Object.assign({}, this.preferencesSubj.getValue(), change);
31+
window.localStorage.setItem(VIEW_PREFERENCES_KEY, JSON.stringify(nextPref));
32+
this.preferencesSubj.next(nextPref);
33+
}
34+
35+
private loadPreferences(): ViewPreferences {
36+
let preferences: ViewPreferences;
37+
const preferencesStr = window.localStorage.getItem(VIEW_PREFERENCES_KEY);
38+
if (preferencesStr) {
39+
try {
40+
preferences = JSON.parse(preferencesStr);
41+
} catch (e) {
42+
preferences = DEFAULT_PREFERENCES;
43+
}
44+
} else {
45+
preferences = DEFAULT_PREFERENCES;
46+
}
47+
return preferences;
48+
}
49+
}

0 commit comments

Comments
 (0)