-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Vis Builder] Add redux store persistence (#3088)
* 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> * changelog and rebase Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com> * Console log the error Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com> * rebase and changelog Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com> * add unit tests Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com> Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
- Loading branch information
1 parent
bd52024
commit bfc59d4
Showing
6 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/plugins/vis_builder/public/application/utils/state_management/redux_persistence.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
src/plugins/vis_builder/public/application/utils/state_management/redux_persistence.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters