Skip to content

Commit 562ca82

Browse files
committed
Updated presence demo with redux-persist for quicker navigation
1 parent 01fbd41 commit 562ca82

File tree

9 files changed

+192
-29
lines changed

9 files changed

+192
-29
lines changed

app/components/Navigation/Navigator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class FirestackNavigator extends React.Component {
9898
route = Object.assign({}, routes.default, {title, actions});
9999
}
100100

101-
route.actions = actions
101+
// route.actions = actions
102102
const {headerStyle} = route;
103103
let headerProps = Object.assign({}, sceneProps, {
104104
route,

app/redux/configureStore.js

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,66 @@
11
import { applyMiddleware, combineReducers, createStore } from 'redux'
2-
import { bindActionCreators } from 'redux'
3-
import thunk from 'redux-thunk'
2+
import { bindActionCreatorsToStore } from 'redux-module-builder';
3+
import { bindActionCreators, compose } from 'redux'
4+
import thunkMiddleware from 'redux-thunk'
5+
import {persistStore, autoRehydrate} from 'redux-persist'
6+
import {AsyncStorage} from 'react-native'
47

58
import Firestack from 'react-native-firestack'
69
import env from '../../config/environment'
710

8-
import * as currentUser from './modules/currentUser'
9-
import * as navigation from './modules/navigation'
11+
import { rootReducer, actions, initialState } from './rootReducer';
1012

1113
export const configureStore = (userInitialState = {}) => {
12-
const middleware = applyMiddleware(thunk);
14+
let middleware = [
15+
thunkMiddleware,
16+
]
17+
18+
let tools = [];
19+
if (process.env.NODE_ENV === 'development') {
20+
// const devTools = require('remote-redux-devtools');
21+
// tools.push(devTools());
22+
}
23+
24+
tools.push(autoRehydrate())
1325

1426
const firestack = new Firestack(env.firestack)
1527

16-
const rootReducer = combineReducers({
17-
user: currentUser.reducer,
18-
navigation: navigation.reducer,
19-
firestack: (state) => firestack
20-
});
28+
let finalCreateStore;
29+
finalCreateStore = compose(
30+
applyMiddleware(...middleware),
31+
...tools
32+
)(createStore);
2133

22-
let initialState =
23-
Object.assign({}, {
24-
navigation: navigation.initialState
25-
})
34+
const finalInitialState = Object.assign({},
35+
initialState,
36+
userInitialState,
37+
{firestack}
38+
);
2639

27-
const store = createStore(
40+
const store = finalCreateStore(
2841
rootReducer,
29-
initialState,
30-
middleware);
42+
finalInitialState
43+
);
44+
45+
const persistor = persistStore(store, {
46+
storage: AsyncStorage,
47+
blacklist: ['firestack']
48+
});
3149

3250
firestack.setStore(store);
3351

34-
const dispatch = store.dispatch;
35-
const actions = {
36-
currentUser: bindActionCreators(currentUser.actions, dispatch),
37-
navigation: bindActionCreators(navigation.actions, dispatch),
52+
if (module.hot) {
53+
module.hot.accept('./rootReducer', () => {
54+
const {rootReducer} = require('./rootReducer').default;
55+
store.replaceReducer(rootReducer);
56+
});
3857
}
3958

40-
return {store,actions}
59+
const boundActions = bindActionCreatorsToStore(actions, store);
60+
return {
61+
store,
62+
actions: boundActions
63+
}
4164
}
4265

4366
export default configureStore

app/redux/modules/navigation.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createConstants, createReducer} from 'redux-module-builder'
2+
import {REHYDRATE} from 'redux-persist/constants'
23
import * as NavUtils from 'NavigationStateUtils'
34

45
import routes from '../../routes'
@@ -16,7 +17,7 @@ export const types = createConstants('navigation')(
1617
export const actions = {
1718
// init: firstRoute => dispatch => dispatch({type: types.INIT, payload: firstRoute}),
1819
push: (routeKey, routeProps={}) => {
19-
const route = Object.assign({}, {routeProps, key: routeKey}, routes[routeKey])
20+
const route = Object.assign({}, {key: routeKey}, routes[routeKey])
2021
return {
2122
type: types.PUSH,
2223
route
@@ -31,7 +32,7 @@ export const actions = {
3132

3233
const getRoutesForKeys = (keys) => keys.map(key => Object.assign({}, routes[key], {key}))
3334

34-
const initialRoutes = ['home']
35+
const initialRoutes = [routes['home']]
3536
const reset = (state, payload = {}) => {
3637
const {nextRoutes, index} = payload;
3738
let newIndex = index || 0;
@@ -42,6 +43,17 @@ const reset = (state, payload = {}) => {
4243
}
4344

4445
export const reducer = createReducer({
46+
[REHYDRATE]: (state, {payload}) => {
47+
console.log('REHYDRATE called for navigation', payload);
48+
const {navigation} = payload;
49+
const index = navigation ? navigation.index : 0;
50+
// return NavUtils.reset(state, [routes['welcome']]);
51+
const navRoutes = navigation ? navigation.routes : initialRoutes;
52+
const newRoutes = navRoutes
53+
.map(route => Object.assign({}, routes[route.key], route));
54+
55+
return NavUtils.reset(state, newRoutes, index);
56+
},
4557
[types.INIT]: (state, {payload}) => ({
4658
...state,
4759
ready: true,
@@ -71,7 +83,7 @@ export const reducer = createReducer({
7183

7284
export const initialState = {
7385
index: 0,
74-
routes: getRoutesForKeys(initialRoutes),
86+
routes: initialRoutes,
7587
showMenu: false
7688
// routes: [routes['signup']]
7789
}

app/redux/rootReducer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { combineReducers } from 'redux';
2+
3+
const modules = {
4+
currentUser: require('./modules/currentUser'),
5+
navigation: require('./modules/navigation'),
6+
}
7+
8+
// const currentUser = require('./reducers/currentUser')
9+
// const navigation = require('./reducers/navigation')
10+
// const feed = require('./reducers/feed')
11+
12+
export let actions = {}
13+
14+
export let reducers = {
15+
'firestack': state => state || null // filled in later
16+
}
17+
export let initialState = {};
18+
19+
Object.keys(modules).forEach(key => {
20+
const container = modules[key];
21+
initialState[key] = container.initialState || {};
22+
actions[key] = container.actions;
23+
reducers[key] = container.reducer;
24+
});
25+
26+
export const rootReducer = combineReducers(reducers);

app/routes.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Home from './views/Home';
1010
import Database from './views/Database';
1111
import Authentication from './views/Authentication';
1212
import Messaging, { Routes as MessagingRoutes } from './views/Messaging';
13+
import Presence, { Routes as PresenceRoutes } from './views/Presence'
1314

1415
export type Route = {
1516
key: String,
@@ -30,6 +31,13 @@ export const exampleRoutes = {
3031
Component: Authentication
3132
}
3233
},
34+
'presence': {
35+
route: {
36+
title: 'Presence',
37+
Component: Presence
38+
},
39+
children: PresenceRoutes
40+
},
3341
'messaging': {
3442
route: {
3543
title: 'Messaging',
@@ -51,5 +59,4 @@ export const routes = toList({
5159
},
5260
});
5361

54-
console.log('routes ->', routes);
5562
export default routes;

app/views/Presence/Demos/Online.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
3+
import {
4+
View,
5+
Text,
6+
} from 'react-native'
7+
8+
import appStyles from '../../../styles/app';
9+
10+
export class Online extends React.Component {
11+
12+
componentWillMount() {
13+
const {firestack} = this.props;
14+
}
15+
16+
_sendHi() {
17+
const {firestack} = this.props;
18+
}
19+
20+
render() {
21+
return (
22+
<View>
23+
<View style={appStyles.row}>
24+
<Text>
25+
This is a test for dealing with Presence.
26+
</Text>
27+
</View>
28+
</View>
29+
)
30+
}
31+
32+
}
33+
34+
export default Online

app/views/Presence/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import {connect} from 'react-redux'
3+
import {
4+
View,
5+
Text
6+
} from 'react-native'
7+
8+
import appStyles from '../../styles/app';
9+
10+
import List from '../../components/List/List'
11+
12+
// Demos
13+
import Online from './Demos/Online';
14+
15+
export const Routes = {
16+
'online': {
17+
route: {
18+
title: 'Online',
19+
Component: Online
20+
}
21+
}
22+
}
23+
24+
export class Presence extends React.Component {
25+
render() {
26+
const initialRows = [
27+
{ title: 'Online', key: 'presence.online' }
28+
];
29+
return (
30+
<View style={appStyles.container}>
31+
<List
32+
initialRows={initialRows}
33+
renderRow={this._renderRow.bind(this)}
34+
onRowPress={this._onRowPress.bind(this)}
35+
/>
36+
</View>
37+
)
38+
}
39+
40+
_renderRow(rowData, sectionID, rowID, highlightRow) {
41+
console.log(rowData)
42+
return (
43+
<View style={[appStyles.row]}>
44+
<Text>{rowData.title}</Text>
45+
</View>
46+
)
47+
}
48+
49+
_onRowPress(rowData) {
50+
const rowKey = rowData.key;
51+
const {actions} = this.props;
52+
const {navigation} = actions;
53+
navigation.push(rowKey, this.props);
54+
}
55+
}
56+
57+
const mapStateToProps = (state) => ({
58+
firestack: state.firestack
59+
})
60+
export default connect(mapStateToProps)(Presence)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"react-redux": "^4.4.5",
1818
"redux": "^3.6.0",
1919
"redux-module-builder": "^0.3.2",
20+
"redux-persist": "^3.5.0",
2021
"redux-thunk": "^2.1.0"
2122
},
2223
"devDependencies": {

scripts/adb-logcat.sh

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#!/usr/local/env bash
1+
#!/usr/bin/env bash
22

3-
adb logcat | grep $(adb shell ps | grep -i com.firestackapp | cut -c10-15)
3+
adb logcat | grep $(adb shell ps | grep -i com.firestackapp | cut -c10-15)

0 commit comments

Comments
 (0)