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

[Vis Builder] Add redux store persistence #3088

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add redux store persistence
implement persistence without using state container or state sync utils, and it
works with both the URL and session storage.

Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
  • Loading branch information
abbyhu2000 committed Dec 29, 2022
commit e3bd95348ef3796ebadf9ae79689179627eb85e4
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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) => {
abbyhu2000 marked this conversation as resolved.
Show resolved Hide resolved
try {
const serializedState = services.osdUrlStateStorage.get<RootState>('_a');
if (serializedState === null) {
return await getPreloadedState(services);
}
return serializedState;
} catch (err) {
return await getPreloadedState(services);
}
abbyhu2000 marked this conversation as resolved.
Show resolved Hide resolved
};

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