Skip to content

Commit

Permalink
Implement redux-persist, and fix first screen setting
Browse files Browse the repository at this point in the history
  • Loading branch information
yutotakano committed Jun 15, 2019
1 parent 13aea9e commit 50fbbad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
13 changes: 7 additions & 6 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
import { Font } from 'expo';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from './src/reducers';
import configureStore from './configureStore';
import { PersistGate } from 'redux-persist/integration/react';

import ManagebacStack from './src/ManagebacStack';
import TasksStack from './src/TasksStack';
Expand All @@ -20,8 +20,7 @@ import LogoutScreen from './src/screens/LogoutScreen';

import { colors } from './src/styles';

// Initialise the entire Redux Store with all reducers combined.
const store = createStore(reducers);
const { store, persistor } = configureStore();

// The main app navigation stack.
// Screens made later on (individual message screens, feed, or whatever) will be added here
Expand All @@ -32,7 +31,7 @@ const AppStack = createMaterialBottomTabNavigator(
SettingsTabs: SettingsStack
},
{
initialRouteName: store.getState().settings.general.firstScreenManagebac ? 'ManagebacTabs' : 'TasksTabs',
initialRouteName: 'ManagebacTabs',
shifting: true,
activeColor: colors.primary,
inactiveColor: colors.inactive,
Expand Down Expand Up @@ -103,7 +102,9 @@ export default class Root extends React.Component {
return (
this.state.fontLoaded && (
<Provider store={store}>
<AppContainer />
<PersistGate loading={null} persistor={persistor}>
<AppContainer />
</PersistGate>
</Provider>
)
);
Expand Down
21 changes: 21 additions & 0 deletions configureStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createStore } from 'redux';
import reducers from './src/reducers';

import { persistReducer, persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

export default function configureStore() {
const persistConfig = {
key: 'root',
storage
};

const persistedReducers = persistReducer(persistConfig, reducers);

// Initialise the entire Redux Store with all reducers combined.
const store = createStore(persistedReducers);

let persistor = persistStore(store);

return { store, persistor };
}
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-navigation-material-bottom-tabs": "^1.0.0",
"react-redux": "^7.0.3",
"redux": "^4.0.1",
"redux-persist": "^5.10.0",
"socket.io-client": "^2.2.0"
}
}
5 changes: 3 additions & 2 deletions src/screens/LoginCheckScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LoginCheckScreen extends React.Component {
this.props.setManagebacOverview(
JSON.parse(response.headers.map['managebac-data'])
);
this.props.navigation.navigate('AppStack');
this.props.navigation.navigate(this.props.firstScreenManagebac ? 'ManagebacTabs' : 'TasksTabs');
})
.catch(err => {
console.warn(err);
Expand Down Expand Up @@ -130,7 +130,8 @@ const mapDispatchToProps = dispatch =>

const mapStateToProps = state => {
const managebac = state.managebac;
return { managebac };
const firstScreenManagebac = state.settings.general.firstScreenManagebac;
return { managebac, firstScreenManagebac };
};

export default connect(
Expand Down

0 comments on commit 50fbbad

Please sign in to comment.