Skip to content

Commit b4c986d

Browse files
committed
Refactor logic for restoring from localStorage
The behavior is now the same regardless of whether you're authenticated or not!
1 parent 3878665 commit b4c986d

5 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/actions/clients.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ export const repoExportError = createAction('REPO_EXPORT_ERROR');
1818
export const repoExportDisplayed = createAction('REPO_EXPORT_DISPLAYED');
1919
export const repoExportNotDisplayed =
2020
createAction('REPO_EXPORT_NOT_DISPLAYED');
21+
export const projectRestoredFromLastSession =
22+
createAction('PROJECT_RESTORED_FROM_LAST_SESSION');

src/reducers/currentProject.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ function currentProject(stateIn, action) {
1919
return state.set('projectKey', action.payload.projectKey);
2020
case 'GIST_IMPORTED':
2121
return state.set('projectKey', action.payload.projectKey);
22+
case 'PROJECT_RESTORED_FROM_LAST_SESSION':
23+
return state.set('projectKey', action.payload.projectKey);
2224
default:
2325
return state;
2426
}

src/reducers/projects.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ export default function reduceProjects(stateIn, action) {
127127
action.payload.gistData,
128128
);
129129

130+
case 'PROJECT_RESTORED_FROM_LAST_SESSION':
131+
return addProject(state, action.payload);
132+
130133
case 'TOGGLE_LIBRARY':
131134
return state.updateIn(
132135
[action.payload.projectKey, 'enabledLibraries'],

src/sagas/errors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export default function* () {
6363
takeEvery('CHANGE_CURRENT_PROJECT', validateCurrentProject, tasks),
6464
takeEvery('GIST_IMPORTED', validateCurrentProject, tasks),
6565
takeEvery('SNAPSHOT_IMPORTED', validateCurrentProject, tasks),
66+
takeEvery(
67+
'PROJECT_RESTORED_FROM_LAST_SESSION',
68+
validateCurrentProject,
69+
tasks,
70+
),
6671
takeEvery('UPDATE_PROJECT_SOURCE', updateProjectSource, tasks),
6772
takeEvery('TOGGLE_LIBRARY', toggleLibrary, tasks),
6873
]);

src/sagas/projects.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import {
1616
gistNotFound,
1717
projectCreated,
1818
projectLoaded,
19-
changeCurrentProject as __changeCurrentProject,
2019
} from '../actions/projects';
2120
import {
2221
snapshotImported,
2322
snapshotImportError,
2423
snapshotNotFound,
24+
projectRestoredFromLastSession,
2525
} from '../actions/clients';
26-
import {getProjectKeys, saveCurrentProject} from '../util/projectUtils';
26+
import {saveCurrentProject} from '../util/projectUtils';
2727
import {loadGistFromId} from '../clients/github';
2828
import {loadAllProjects, loadProjectSnapshot} from '../clients/firebase';
2929
import {getCurrentUserId} from '../selectors';
@@ -51,14 +51,7 @@ export function* changeCurrentProject() {
5151
}
5252

5353
export function* rehydrateProject({payload: {rehydratedProject}}) {
54-
const state = yield select();
55-
const allProjectKeys = getProjectKeys(state);
56-
if (allProjectKeys.includes(rehydratedProject.projectKey)) {
57-
yield put(__changeCurrentProject(rehydratedProject.projectKey));
58-
} else {
59-
yield put(projectLoaded(rehydratedProject));
60-
yield put(__changeCurrentProject(rehydratedProject.projectKey));
61-
}
54+
yield put(projectRestoredFromLastSession(rehydratedProject));
6255
}
6356

6457
export function* importSnapshot({payload: {snapshotKey}}) {

0 commit comments

Comments
 (0)