Skip to content

Commit 34c4f7f

Browse files
committed
Persists Redux store when reloading app for login
1 parent 67e4669 commit 34c4f7f

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

client/modules/IDE/actions/project.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { setUnsavedChanges,
77
justOpenedProject,
88
resetJustOpenedProject,
99
showErrorModal } from './ide';
10+
import { saveState } from '../../../persistState';
1011

1112
const ROOT_URL = process.env.API_URL;
1213

@@ -41,6 +42,13 @@ export function getProject(id) {
4142
};
4243
}
4344

45+
export function persistState() {
46+
return (dispatch, getState) => {
47+
const state = getState();
48+
saveState(state);
49+
};
50+
}
51+
4452
export function saveProject(autosave = false) {
4553
return (dispatch, getState) => {
4654
const state = getState();

client/modules/IDE/pages/IDEView.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ class IDEView extends React.Component {
170170
warnIfUnsavedChanges(route) { // eslint-disable-line
171171
if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) {
172172
// don't warn
173+
this.props.persistState();
173174
} else if (route && (this.props.location.pathname === '/login' || this.props.location.pathname === '/signup')) {
174175
// don't warn
176+
this.props.persistState();
175177
} else if (this.props.ide.unsavedChanges) {
176178
if (!window.confirm('Are you sure you want to leave this page? You have unsaved changes.')) {
177179
return false;
@@ -588,7 +590,8 @@ IDEView.propTypes = {
588590
})).isRequired,
589591
clearConsole: PropTypes.func.isRequired,
590592
showErrorModal: PropTypes.func.isRequired,
591-
hideErrorModal: PropTypes.func.isRequired
593+
hideErrorModal: PropTypes.func.isRequired,
594+
persistState: PropTypes.func.isRequired
592595
};
593596

594597
function mapStateToProps(state) {

client/persistState.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Saves and loads a snapshot of the Redux store
3+
state to session storage
4+
*/
5+
const key = 'p5js-editor';
6+
const storage = sessionStorage;
7+
8+
export const saveState = (state) => {
9+
console.warn('save');
10+
try {
11+
storage.setItem(key, JSON.stringify(state));
12+
} catch (error) {
13+
console.warn('Unable to persist state to storage:', error);
14+
}
15+
};
16+
17+
export const loadState = () => {
18+
try {
19+
return JSON.parse(storage.getItem(key));
20+
} catch (error) {
21+
console.warn('Failed to retrieve initialize state from storage:', error);
22+
return null;
23+
}
24+
};
25+
26+
export const clearState = () => {
27+
storage.removeItem(key);
28+
};

client/store.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createStore, applyMiddleware, compose } from 'redux';
22
import thunk from 'redux-thunk';
33
import DevTools from './modules/App/components/DevTools';
44
import rootReducer from './reducers';
5+
import { clearState, loadState } from './persistState';
56

67
export default function configureStore(initialState) {
78
const enhancers = [
@@ -13,9 +14,12 @@ export default function configureStore(initialState) {
1314
enhancers.push(window.devToolsExtension ? window.devToolsExtension() : DevTools.instrument());
1415
}
1516

17+
const savedState = loadState();
18+
clearState();
19+
1620
const store = createStore(
1721
rootReducer,
18-
initialState,
22+
savedState != null ? savedState : initialState,
1923
compose(...enhancers)
2024
);
2125

0 commit comments

Comments
 (0)