From 75f7186ca7fb1d731db39cd9d6721f6793e6612d Mon Sep 17 00:00:00 2001 From: JP1998 Date: Tue, 16 Apr 2019 23:20:44 +0200 Subject: [PATCH] Fix a bug logging the user out when calling certain sites This has happened because the store was being updated before it was rehydrated, and thus first saving an empty state, which is thereafter being loaded when rehydrating the application state. --- js/api.js | 10 +++++++--- pages/create.js | 8 +++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/js/api.js b/js/api.js index afc81c7..a065064 100644 --- a/js/api.js +++ b/js/api.js @@ -340,6 +340,7 @@ const default_applicationstate = { }; var __store; +var applicationHydrated = false; export function initializeStore(initialState = default_applicationstate) { __store = createStore( @@ -348,7 +349,9 @@ export function initializeStore(initialState = default_applicationstate) { composeWithDevTools(applyMiddleware(thunkMiddleware)) ); __store.subscribe(() => { - localStorage.setItem('reduxState', JSON.stringify(__store.getState())); + if(applicationHydrated) { + localStorage.setItem('reduxState', JSON.stringify(__store.getState())); + } }); return __store; } @@ -449,11 +452,12 @@ function rehydrateApplicationState() { if(persistedState) { __store.dispatch({ type : actiontypes_userinfo.REHYDRATE, - parameters : Object.assign({}, persistedState.userinfo, {}) + parameters : Object.assign({}, persistedState.userinfo) }); __store.dispatch({ type : actiontypes_tournamentinfo.REHYDRATE, - parameters : Object.assign({}, persistedState.tournamentinfo, {}) + parameters : Object.assign({}, persistedState.tournamentinfo) }); + applicationHydrated = true; } } diff --git a/pages/create.js b/pages/create.js index c6503ad..8d079dd 100644 --- a/pages/create.js +++ b/pages/create.js @@ -25,7 +25,7 @@ import { createTournament } from '../js/api'; import '../static/everypage.css'; -class PrivateCreatePage extends React.Component { +class CreatePage extends React.Component { render() { const { isSignedIn } = this.props; @@ -66,11 +66,9 @@ function mapStateToCreatePageProperties(state) { return { isSignedIn }; } -const CreatePage = connect( +export default connect( mapStateToCreatePageProperties -)(PrivateCreatePage); - -export default CreatePage; +)(CreatePage); function CreateTournamentCard() { return (