Skip to content

Commit 803b0a0

Browse files
rickstefanikmergify[bot]
authored andcommitted
feat(content-explorer): local storage for view mode preference (#1476)
* feat: added persistence to view preference * fix: removed duplicate state
1 parent eff73cf commit 803b0a0

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/elements/content-explorer/ContentExplorer.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import DeleteConfirmationDialog from './DeleteConfirmationDialog';
2929
import Content from './Content';
3030
import { isFocusableElement, isInputElement, focus } from '../../utils/dom';
3131
import { FOLDER_FIELDS_TO_FETCH } from '../../utils/fields';
32+
import LocalStore from '../../utils/LocalStore';
3233
import {
3334
isFeatureEnabled,
3435
withFeatureConsumer,
@@ -134,9 +135,10 @@ type State = {
134135
sortBy: SortBy,
135136
sortDirection: SortDirection,
136137
view: View,
137-
viewMode: ViewMode,
138138
};
139139

140+
const localStoreViewMode = 'bce.defaultViewMode';
141+
140142
class ContentExplorer extends Component<Props, State> {
141143
id: string;
142144

@@ -156,6 +158,8 @@ class ContentExplorer extends Component<Props, State> {
156158

157159
firstLoad: boolean = true; // Keeps track of very 1st load
158160

161+
store: LocalStore = new LocalStore();
162+
159163
static defaultProps = {
160164
rootFolderId: DEFAULT_ROOT,
161165
sortBy: FIELD_NAME,
@@ -248,7 +252,6 @@ class ContentExplorer extends Component<Props, State> {
248252
sortBy,
249253
sortDirection,
250254
view: VIEW_FOLDER,
251-
viewMode: VIEW_MODE_LIST,
252255
};
253256
}
254257

@@ -281,7 +284,7 @@ class ContentExplorer extends Component<Props, State> {
281284
* @return {void}
282285
*/
283286
componentDidMount() {
284-
const { defaultView, currentFolderId }: Props = this.props;
287+
const { currentFolderId, defaultView }: Props = this.props;
285288
this.rootElement = ((document.getElementById(this.id): any): HTMLElement);
286289
this.appElement = ((this.rootElement.firstElementChild: any): HTMLElement);
287290

@@ -1249,14 +1252,26 @@ class ContentExplorer extends Component<Props, State> {
12491252
this.setState({ currentOffset: newOffset }, this.refreshCollection);
12501253
};
12511254

1255+
getViewMode = (): ViewMode => {
1256+
const { features }: Props = this.props;
1257+
const viewModePreference = this.store.getItem(localStoreViewMode);
1258+
const isGridViewEnabled = isFeatureEnabled(features, 'contentExplorer.gridView.enabled');
1259+
return isGridViewEnabled && viewModePreference ? viewModePreference : VIEW_MODE_LIST;
1260+
};
1261+
12521262
/**
12531263
* Change the current view mode
12541264
*
12551265
* @param {ViewMode} viewMode - the new view mode
12561266
* @return {void}
12571267
*/
12581268
changeViewMode = (viewMode: ViewMode): void => {
1259-
this.setState({ viewMode });
1269+
const { features }: Props = this.props;
1270+
1271+
if (isFeatureEnabled(features, 'contentExplorer.gridView.enabled')) {
1272+
this.store.setItem(localStoreViewMode, viewMode);
1273+
this.forceUpdate();
1274+
}
12601275
};
12611276

12621277
/**
@@ -1302,7 +1317,6 @@ class ContentExplorer extends Component<Props, State> {
13021317

13031318
const {
13041319
view,
1305-
viewMode,
13061320
rootName,
13071321
currentCollection,
13081322
currentPageSize,
@@ -1325,6 +1339,8 @@ class ContentExplorer extends Component<Props, State> {
13251339
const allowUpload: boolean = canUpload && !!can_upload;
13261340
const allowCreate: boolean = canCreateNewFolder && !!can_upload;
13271341

1342+
const viewMode = this.getViewMode();
1343+
13281344
/* eslint-disable jsx-a11y/no-static-element-interactions */
13291345
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
13301346
return (

src/elements/content-explorer/__tests__/ContentExplorer-test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { mount } from 'enzyme';
33
import { ContentExplorerComponent as ContentExplorer } from '../ContentExplorer';
44
import { FOLDER_FIELDS_TO_FETCH } from '../../../utils/fields';
5-
import { FIELD_REPRESENTATIONS, VIEW_MODE_LIST, VIEW_MODE_GRID } from '../../../constants';
5+
import { FIELD_REPRESENTATIONS, VIEW_MODE_GRID } from '../../../constants';
66

77
jest.mock('../../common/header/Header', () => 'mock-header');
88
jest.mock('../../common/sub-header/SubHeader', () => 'mock-subheader');
@@ -43,19 +43,14 @@ describe('elements/content-explorer/ContentExplorer', () => {
4343
});
4444

4545
describe('changeViewMode()', () => {
46-
let wrapper;
47-
beforeEach(() => {
48-
wrapper = getWrapper();
49-
});
50-
51-
test('should initially be list view', () => {
52-
expect(wrapper.state('viewMode')).toBe(VIEW_MODE_LIST);
53-
});
46+
const localStoreViewMode = 'bce.defaultViewMode';
5447

5548
test('should change to grid view', () => {
49+
const wrapper = getWrapper({ features: { contentExplorer: { gridView: { enabled: true } } } });
5650
const instance = wrapper.instance();
51+
instance.store.setItem = jest.fn();
5752
instance.changeViewMode(VIEW_MODE_GRID);
58-
expect(wrapper.state('viewMode')).toBe(VIEW_MODE_GRID);
53+
expect(instance.store.setItem).toHaveBeenCalledWith(localStoreViewMode, VIEW_MODE_GRID);
5954
});
6055
});
6156

0 commit comments

Comments
 (0)