Commit 30f9a8e8 authored by Michael Vrána's avatar Michael Vrána

Algorithm graph persistence fixes

parent 3dd3ac34
Pipeline #80132 passed with stages
in 5 minutes and 41 seconds
......@@ -69,9 +69,14 @@ export const initialAlgorithmState: AlgorithmGraphState = {
const incompatibleTypeError = (from: AlibType, to: AlibType) =>
`Type ${from} is not castable to type ${to}`
export const localStorageAlgorithmReducer = (state?: AlgorithmGraphState, action?: any) => {
const localStorageKey = 'webui_graph'
export const localStorageAlgorithmReducer = (
state?: AlgorithmGraphState,
action?: any
) => {
if (state === undefined) {
const savedGraph = window.localStorage.getItem('webui_graph')
const savedGraph = window.localStorage.getItem(localStorageKey)
if (savedGraph === null) return algorithmReducer(state, action)
......@@ -88,9 +93,22 @@ export const localStorageAlgorithmReducer = (state?: AlgorithmGraphState, action
return algorithmReducer(loadedState, action)
}
const newState = algorithmReducer(state, action)
window.localStorage.setItem('webui_graph', JSON.stringify(newState))
if (
newState !== state ||
['undoAlgorithm', 'redoAlgorithm'].includes(action.type)
) {
try {
window.localStorage.setItem(
localStorageKey,
JSON.stringify(newState)
)
} catch (e) {
console.error(e)
}
}
return newState
}
......
......@@ -112,6 +112,7 @@ const reducer = combineReducers<ReduxState>({
algorithmData: undoable(algorithmReducer, {
undoType: undoAlgorithmActionType,
redoType: redoAlgorithmActionType,
neverSkipReducer: true,
filter: (action) => [
'addNode',
'addEdge',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment