Skip to content

Commit eb6f72e

Browse files
authored
Added react-router + redux-localstorage (#14)
1 parent 2ea1059 commit eb6f72e

File tree

9 files changed

+95
-28
lines changed

9 files changed

+95
-28
lines changed

app/client/components/LoggedIn.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { Component } from 'react';
2+
3+
export default class LoggedIn extends Component {
4+
render() {
5+
return (
6+
<div>
7+
<h2>Logged in as {this.props.user.username}</h2>
8+
</div>
9+
);
10+
}
11+
}

app/client/components/Login.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import React from 'react';
1+
import React, { Component, PropTypes } from 'react';
2+
3+
export default class Login extends Component {
4+
static propTypes = {
5+
onLogin: PropTypes.func.isRequired
6+
};
27

3-
export default class App extends React.Component {
48
handleLogin() {
59
const { onLogin } = this.props;
610
const username = this.refs.username.value;
711

812
onLogin({ username, loggedIn: true });
13+
14+
this.props.router.push('/loggedin');
915
}
1016

1117
render() {

app/client/containers/App.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import React from 'react';
2-
import { bindActionCreators } from 'redux';
3-
import { connect } from 'react-redux';
4-
import userActions from '../actions/user';
5-
import Login from '../components/Login';
1+
import React, { Component, PropTypes } from 'react';
62

7-
@connect((state) => state)
8-
export default class App extends React.Component {
9-
render() {
10-
const { user, dispatch } = this.props;
11-
const boundUserActions = bindActionCreators(userActions, dispatch);
3+
export default class App extends Component {
4+
static propTypes = {
5+
children: PropTypes.element.isRequired
6+
};
127

8+
render() {
139
return (
14-
!user.loggedIn ?
15-
<Login onLogin={ boundUserActions.login } /> :
16-
<div>Hi { user.username }!</div>
10+
<div>
11+
{this.props.children}
12+
</div>
1713
);
1814
}
1915
}

app/client/containers/LoggedInPage.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { connect } from 'react-redux';
2+
import LoggedIn from '../components/LoggedIn';
3+
4+
const mapStateToProps = (state) => {
5+
return state;
6+
};
7+
8+
const mapDispatchToProps = (dispatch) => { // eslint-disable-line no-unused-vars
9+
return {};
10+
};
11+
12+
export default connect(mapStateToProps, mapDispatchToProps)(LoggedIn);

app/client/containers/LoginPage.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { connect } from 'react-redux';
2+
import { bindActionCreators } from 'redux';
3+
import Login from '../components/Login';
4+
import userActions from '../actions/user';
5+
6+
const mapStateToProps = (state) => {
7+
return state;
8+
};
9+
10+
const mapDispatchToProps = (dispatch) => {
11+
const user = bindActionCreators(userActions, dispatch);
12+
return {
13+
onLogin: user.login
14+
};
15+
};
16+
17+
export default connect(mapStateToProps, mapDispatchToProps)(Login);

app/client/main.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { Provider } from 'react-redux';
4-
import App from './containers/App';
5-
import configureStore from './store/default';
4+
import { Router, hashHistory } from 'react-router';
5+
import { syncHistoryWithStore } from 'react-router-redux';
6+
import routes from './routes';
7+
import configureStore from './store';
68

79
const initialState = {};
810
const store = configureStore(initialState);
11+
const routerHistory = syncHistoryWithStore(hashHistory, store);
12+
913
const rootElement = document.querySelector(document.currentScript.getAttribute('data-container'));
1014

1115
ReactDOM.render(
1216
<Provider store={store}>
13-
<div>
14-
<App />
15-
</div>
17+
<Router history={routerHistory} routes={routes} />
1618
</Provider>,
1719
rootElement
1820
);

app/client/routes.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { Route, IndexRoute } from 'react-router';
3+
4+
import App from './containers/App';
5+
import LoginPage from './containers/LoginPage';
6+
import LoggedInPage from './containers/LoggedInPage';
7+
8+
export default (
9+
<Route path="/" component={App}>
10+
<IndexRoute component={LoginPage} />
11+
<Route path="loggedin" component={LoggedInPage} />
12+
</Route>
13+
);
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
2+
import { hashHistory } from 'react-router';
3+
import { routerMiddleware, routerReducer as routing, push } from 'react-router-redux';
4+
import persistState from 'redux-localstorage';
25
import thunk from 'redux-thunk';
36

4-
import user from '../reducers/user';
5-
import userActions from '../actions/user';
7+
import user from './reducers/user';
8+
import userActions from './actions/user';
9+
10+
const router = routerMiddleware(hashHistory);
611

712
const actionCreators = {
8-
...userActions
13+
...userActions,
14+
push
915
};
1016

1117
const reducers = {
12-
user
18+
user,
19+
routing
1320
};
1421

15-
const middlewares = [thunk];
22+
const middlewares = [ thunk, router ];
1623

1724
const composeEnhancers = (() => {
1825
const compose_ = window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
@@ -22,9 +29,9 @@ const composeEnhancers = (() => {
2229
return compose;
2330
})();
2431

25-
const enhancer = composeEnhancers(applyMiddleware(...middlewares));
26-
const rootReducer = combineReducers(reducers);
27-
2832
export default function configureStore(initialState) {
33+
const enhancer = composeEnhancers(applyMiddleware(...middlewares), persistState());
34+
const rootReducer = combineReducers(reducers);
35+
2936
return createStore(rootReducer, initialState, enhancer);
3037
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
"react": "^15.4.2",
1212
"react-dom": "^15.4.2",
1313
"react-redux": "^5.0.2",
14+
"react-router": "^3.0.2",
15+
"react-router-redux": "^4.0.7",
1416
"redux": "^3.0.0",
1517
"redux-actions": "^1.2.1",
18+
"redux-localstorage": "^0.4.1",
1619
"redux-thunk": "^2.2.0"
1720
},
1821
"devDependencies": {

0 commit comments

Comments
 (0)