Skip to content

Commit

Permalink
Fix Initial State Migration Bug (#399)
Browse files Browse the repository at this point in the history
* Fix Initial State Migration Bug

This commit fixes a bug where the merging of the
initial state and the persisted state when creating
the redux store caused the latest version to get
attached to the old version. This prevented the
migration from functioning properly since the
version was the latest even though the data was not.

Now, the initial state isn't given when creating
the store, so the only starting state used comes
from the persisted local storage state (if it exists).
The top level reducer uses the initial state if the
incoming state is null. This does not affect any other
parts of the frontend but should prevent migration
issues when the persisted state is v0.0.0

* Run migrator on init action

This commit updates the top level reducer to run the
migrators on the init action. This should allow us to
properly initialize the store using the migrator without
having to go through full reducer cycle.
  • Loading branch information
pr1sm committed Mar 30, 2019
1 parent d56fb3d commit cffb309
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/frontend/src/state/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const GLOBAL_ACTIONS = {
RESET: '@@RESET',
SET_THEME: '@@SET_THEME',
MIGRATE_STATE: '@@MIGRATE_STATE',
INIT: '@@INIT',
};

export const globalActions = {
Expand Down
3 changes: 1 addition & 2 deletions packages/frontend/src/state/configureStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import persistState from 'redux-localstorage';
import topLevelReducer from './reducers';
import { initialState } from './migrators';
import profileAttributeValidationMiddleware from './middleware/profiles/profileAttributeValidationMiddleware';
import profileFormValidationMiddleware from './middleware/profiles/profileFormValidationMiddleware';
import settingsAttributeValidationMiddleware from './middleware/settings/settingsAttributeValidationMiddleware';
Expand All @@ -14,7 +13,7 @@ const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
export default function configureStore() {
return createStore(
topLevelReducer,
initialState,
null,
composeEnhancers(
applyMiddleware(
profileAttributeValidationMiddleware,
Expand Down
18 changes: 11 additions & 7 deletions packages/frontend/src/state/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ import navbarReducer from './reducers/navbar/navbarReducer';
import { GLOBAL_ACTIONS } from './actions';
import topLevelMigrator, { initialState } from './migrators';

const topLevelReducer = (state = initialState, action) => {
const topLevelReducer = (startState, action) => {
// Return State if a null/undefined action is given
if (!action) {
return state;
return startState || initialState;
}

// Check for migration and perform it
if (action.type === GLOBAL_ACTIONS.MIGRATE_STATE || action.type === GLOBAL_ACTIONS.INIT) {
return topLevelMigrator(startState);
}

// Check for reset and return initial state
if (action.type === GLOBAL_ACTIONS.RESET) {
return { ...initialState };
}

// Use initial state if start state isn't given
const state = startState || initialState;

// Check for set theme and adjust it here
if (action.type === GLOBAL_ACTIONS.SET_THEME) {
if (action.theme) {
Expand All @@ -34,11 +43,6 @@ const topLevelReducer = (state = initialState, action) => {
return { ...state };
}

// Check for migration and perform it
if (action.type === GLOBAL_ACTIONS.MIGRATE_STATE) {
return topLevelMigrator(state);
}

// If not a global action, handle the action with sub reducers
const changes = {
tasks: taskListReducer(state.tasks, action),
Expand Down

0 comments on commit cffb309

Please sign in to comment.