Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] [Vis Builder] Add redux store persistence #3172

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ const createStartContract = (): Start => {
},
}),
get: jest.fn().mockReturnValue(Promise.resolve({})),
getDefault: jest.fn().mockReturnValue(
Promise.resolve({
name: 'Default name',
id: 'id',
})
),
clearCache: jest.fn(),
} as unknown) as IndexPatternsContract,
};
Expand Down
19 changes: 18 additions & 1 deletion src/plugins/vis_builder/public/application/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import { dataPluginMock } from '../../../../data/public/mocks';
import { embeddablePluginMock } from '../../../../embeddable/public/mocks';
import { expressionsPluginMock } from '../../../../expressions/public/mocks';
import { navigationPluginMock } from '../../../../navigation/public/mocks';
import { createOsdUrlStateStorage } from '../../../../opensearch_dashboards_utils/public';
import { VisBuilderServices } from '../../types';

export const createVisBuilderServicesMock = () => {
const coreStartMock = coreMock.createStart();
const toastNotifications = coreStartMock.notifications.toasts;
const applicationMock = coreStartMock.application;
const i18nContextMock = coreStartMock.i18n.Context;
const indexPatternMock = dataPluginMock.createStartContract().indexPatterns;
const indexPatternMock = dataPluginMock.createStartContract();
const embeddableMock = embeddablePluginMock.createStartContract();
const navigationMock = navigationPluginMock.createStartContract();
const expressionMock = expressionsPluginMock.createStartContract();
const osdUrlStateStorageMock = createOsdUrlStateStorage({ useHash: false });

const visBuilderServicesMock = {
...coreStartMock,
Expand All @@ -39,6 +41,21 @@ export const createVisBuilderServicesMock = () => {
data: indexPatternMock,
embeddable: embeddableMock,
scopedHistory: (scopedHistoryMock.create() as unknown) as ScopedHistory,
osdUrlStateStorage: osdUrlStateStorageMock,
types: {
all: () => [
{
name: 'viz',
ui: {
containerConfig: {
style: {
defaults: 'style default states',
},
},
},
},
],
},
};

return (visBuilderServicesMock as unknown) as jest.Mocked<VisBuilderServices>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { VisBuilderServices } from '../../../types';
import { createVisBuilderServicesMock } from '../mocks';
import { getPreloadedState } from './preload';
import { loadReduxState, saveReduxState } from './redux_persistence';

describe('test redux state persistence', () => {
let mockServices: jest.Mocked<VisBuilderServices>;
let reduxStateParams: any;

beforeEach(() => {
mockServices = createVisBuilderServicesMock();
reduxStateParams = {
style: 'style',
visualization: 'visualization',
metadata: 'metadata',
};
});

test('test load redux state when url is empty', async () => {
const defaultStates = {
style: 'style default states',
visualization: {
searchField: '',
activeVisualization: { name: 'viz', aggConfigParams: [] },
indexPattern: 'id',
},
metadata: {
editor: { validity: {}, state: 'loading' },
originatingApp: undefined,
},
};

const returnStates = await loadReduxState(mockServices);
expect(returnStates).toStrictEqual(defaultStates);
});

test('test load redux state', async () => {
mockServices.osdUrlStateStorage.set('_a', reduxStateParams, { replace: true });
const returnStates = await loadReduxState(mockServices);
expect(returnStates).toStrictEqual(reduxStateParams);
});

test('test save redux state', () => {
saveReduxState(reduxStateParams, mockServices);
const urlStates = mockServices.osdUrlStateStorage.get('_a');
expect(urlStates).toStrictEqual(reduxStateParams);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { VisBuilderServices } from '../../../types';
import { getPreloadedState } from './preload';
import { RootState } from './store';

export const loadReduxState = async (services: VisBuilderServices) => {
try {
const serializedState = services.osdUrlStateStorage.get<RootState>('_a');
if (serializedState !== null) return serializedState;
} catch (err) {
/* eslint-disable no-console */
console.error(err);
/* eslint-enable no-console */
}

return await getPreloadedState(services);
};

export const saveReduxState = (
{ style, visualization, metadata },
services: VisBuilderServices
) => {
try {
services.osdUrlStateStorage.set<RootState>(
'_a',
{ style, visualization, metadata },
{
replace: true,
}
);
} catch (err) {
return;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { reducer as styleReducer } from './style_slice';
import { reducer as visualizationReducer } from './visualization_slice';
import { reducer as metadataReducer } from './metadata_slice';
import { VisBuilderServices } from '../../..';
import { getPreloadedState } from './preload';
import { setEditorState } from './metadata_slice';
import { loadReduxState, saveReduxState } from './redux_persistence';

const rootReducer = combineReducers({
style: styleReducer,
Expand All @@ -25,7 +25,7 @@ export const configurePreloadedStore = (preloadedState: PreloadedState<RootState
};

export const getPreloadedStore = async (services: VisBuilderServices) => {
const preloadedState = await getPreloadedState(services);
const preloadedState = await loadReduxState(services);
const store = configurePreloadedStore(preloadedState);

const { metadata: metadataState, style: styleState, visualization: vizState } = store.getState();
Expand Down Expand Up @@ -62,6 +62,15 @@ export const getPreloadedStore = async (services: VisBuilderServices) => {

previousStore = currentStore;
previousMetadata = currentMetadata;

saveReduxState(
{
style: store.getState().style,
visualization: store.getState().visualization,
metadata: store.getState().metadata,
},
services
);
};

// the store subscriber will automatically detect changes and call handleChange function
Expand Down