Skip to content

Commit caf5f79

Browse files
author
Brian Schemp
committed
Updated react and react-router versions. Added new way of handling state from server.
1 parent eca318b commit caf5f79

25 files changed

+277
-28989
lines changed

.eslintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"ecmaFeatures": {
3+
"blockBindings": true,
4+
"forOf": true,
5+
"jsx": true
6+
},
7+
"env": {
8+
"browser": 1,
9+
"node": 1
10+
},
11+
"globals": {
12+
"module": 1,
13+
"require": 1
14+
},
15+
"rules": {
16+
"quotes": [2, "single"],
17+
"strict": [2, "global"]
18+
}
19+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
.DS_Store
3+
static/bundle.js

.jshintrc

Lines changed: 0 additions & 20 deletions
This file was deleted.

app.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var React = require('react');
44
var Router = require('react-router');
55
var Routes = require('./lib/Routes.jsx');
6-
var SearchActions = require('./lib/actions/SearchActions');
6+
//var SearchActions = require('./lib/actions/SearchActions');
77

88
// Export React so the dev tools can find it
99
(window !== window.top ? window.top : window).React = React;
@@ -12,11 +12,20 @@ Router.run(Routes, Router.HistoryLocation, function (Handler, state) {
1212

1313
// Set initial state. Use params from the route state and the serialized javascript
1414
// stored in window.App. The JSON stored in window.App was created in
15-
// the server request.
15+
// the server request.
16+
/*
1617
SearchActions.setState({
1718
params: state.params,
1819
state: window.App
1920
});
21+
*/
2022

21-
React.render(React.createFactory(Handler)(window.App), document.getElementById('app'));
23+
// Store the url parameters and the state from the server that we
24+
// are storing in window.App
25+
var props = {
26+
params: state.params,
27+
state: window.App
28+
};
29+
30+
React.render(React.createFactory(Handler)(props), document.getElementById('app'));
2231
});

lib/Routes.jsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@ var React = require('react/addons');
44
var Router = require('react-router');
55
var Route = Router.Route;
66
var AppHandler = require('./handlers/AppHandler.jsx');
7-
var IndexHandler = require('./handlers/IndexHandler.jsx');
87
var ProductHandler = require('./handlers/ProductHandler.jsx');
98

109
/**
11-
* Define routes. Each route will have a handler to deal with the
10+
* Define routes. Each route will have a handler to deal with the
1211
* route.
1312
*/
1413
var Routes = (
15-
/* jshint ignore:start */
16-
<Route handler={AppHandler}>
17-
<Route name="index" path="/" handler={IndexHandler}>
18-
<Route name="products" path="/products/:category" handler={ProductHandler}/>
19-
</Route>
14+
<Route name="app" path="/" handler={AppHandler}>
15+
<Route name="products" path="/products/:category" handler={ProductHandler}/>
2016
</Route>
21-
/* jshint ignore:end */
22-
2317
);
2418

2519
module.exports = Routes;

lib/actions/ActionCreators.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var Dispatcher = require('../dispatcher/Dispatcher');
4+
var ActionConstants = require('../constants/ActionConstants');
5+
6+
var ActionCreators = {
7+
setState: function(state) {
8+
Dispatcher.handleViewAction({
9+
actionType: ActionConstants.SET_STATE,
10+
state: state
11+
});
12+
},
13+
14+
requestProducts: function(category) {
15+
Dispatcher.handleViewAction({
16+
actionType: ActionConstants.REQUEST_PRODUCTS,
17+
category: category
18+
});
19+
}
20+
};
21+
22+
module.exports = ActionCreators;

lib/actions/SearchActions.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/actions/ServerActionCreators.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var Dispatcher = require('../dispatcher/Dispatcher');
4+
var ActionConstants = require('../constants/ActionConstants');
5+
6+
var ServerActionCreators = {
7+
8+
handleProductsSuccess: function(response) {
9+
Dispatcher.handleServerAction({
10+
actionType: ActionConstants.REQUEST_PRODUCTS_SUCCESS,
11+
products: response
12+
});
13+
},
14+
15+
handleProductsError: function(response) {
16+
Dispatcher.handleServerAction({
17+
actionType: ActionConstants.REQUEST_PRODUCTS_ERROR,
18+
error: response
19+
});
20+
}
21+
22+
};
23+
24+
module.exports = ServerActionCreators;

lib/components/Category.jsx renamed to lib/components/CategoryList.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

33
var React = require('react');
4-
var Navigation = require('react-router').Navigation;
54

6-
var Category = React.createClass({
7-
mixins: [Navigation],
5+
var CategoryList = React.createClass({
6+
contextTypes: {
7+
router: React.PropTypes.func
8+
},
89

910
handleChange: function (e) {
10-
this.transitionTo('/products/' + e.target.value);
11+
this.context.router.transitionTo('products', { category: e.target.value });
1112
},
1213

1314
propTypes: {
@@ -16,11 +17,10 @@ var Category = React.createClass({
1617

1718
getDefaultProps: function () {
1819
return {
19-
cagtegories: null
20+
cagtegories: []
2021
};
2122
},
2223

23-
/* jshint ignore:start */
2424
render: function () {
2525
var categories;
2626

@@ -38,12 +38,11 @@ var Category = React.createClass({
3838
<option value="">Select a Category</option>
3939
{ categories }
4040
</select>
41-
</div>
41+
</div>
4242
);
4343

4444
}
45-
/* jshint ignore:end */
4645

4746
});
4847

49-
module.exports = Category;
48+
module.exports = CategoryList;

lib/components/ProductList.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var ProductList = React.createClass({
1313
};
1414
},
1515

16-
/* jshint ignore:start */
1716
render: function() {
1817
return (
1918
<div>
@@ -29,7 +28,6 @@ var ProductList = React.createClass({
2928
</div>
3029
);
3130
}
32-
/* jshint ignore:end */
3331

3432
});
3533

lib/constants/ActionConstants.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
SET_STATE: 'SET_STATE',
5+
REQUEST_PRODUCTS: 'REQUEST_PRODUCTS',
6+
REQUEST_PRODUCTS_SUCCESS: 'REQUEST_PRODUCTS_SUCCESS',
7+
REQUEST_PRODUCTS_ERROR: 'REQUEST_PRODUCTS_ERROR'
8+
};

lib/constants/ActionTypes.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/core/Dispatcher.js renamed to lib/dispatcher/Dispatcher.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
var Flux = require('flux');
44
var assign = require('object-assign');
55

6-
/**
7-
* A singleton that operates as the central hub for application updates.
8-
* For more information visit https://facebook.github.io/flux/
9-
*/
106
var Dispatcher = assign(new Flux.Dispatcher(), {
117

12-
/**
13-
* @param {object} action The details of the action, including the action's
14-
* type and additional data coming from the view.
15-
*/
168
handleViewAction: function (action) {
179
var payload = {
1810
action: action
1911
};
2012
this.dispatch(payload);
13+
},
14+
15+
handleServerAction: function (action) {
16+
var payload = {
17+
action: action
18+
};
19+
this.dispatch(payload);
2120
}
2221

2322
});

lib/handlers/AppHandler.jsx

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,30 @@
33
var React = require('react');
44
var Router = require('react-router');
55
var RouteHandler = Router.RouteHandler;
6+
var CategoryList = require('../components/CategoryList.jsx');
67

78
var AppHandler = React.createClass({
89

9-
getInitialState: function() {
10-
return {
11-
mode: 'Server Rendering',
12-
counter: 0
13-
};
14-
},
15-
1610
propTypes: {
17-
categories: React.PropTypes.array,
18-
products: React.PropTypes.array
11+
params: React.PropTypes.object.isRequired,
12+
state: React.PropTypes.object.isRequired
1913
},
2014

2115
getDefaultProps: function() {
2216
return {
23-
categories: [],
24-
products: []
17+
params: {},
18+
state: {}
2519
};
2620
},
2721

28-
componentDidMount: function() {
29-
this.setState({
30-
mode: 'Client Rendering'
31-
});
32-
33-
window.setInterval(this.incrementCounter, 1000);
34-
},
35-
36-
incrementCounter: function() {
37-
this.setState({counter: this.state.counter+1});
38-
},
39-
40-
/* jshint ignore:start */
4122
render: function() {
4223
return (
4324
<div>
44-
<header>
45-
Current mode: {this.state.mode} <br />
46-
{(this.state.counter > 0) ? this.state.counter + ' seconds' : ''}
47-
</header>
48-
49-
<RouteHandler categories={ this.props.categories } products= { this.props.products } />
25+
<CategoryList categories={ this.props.state.categories }/>
26+
<RouteHandler products={ this.props.state.products } params={ this.props.params }/>
5027
</div>
5128
);
5229
}
53-
/* jshint ignore:end */
5430

5531
});
5632

0 commit comments

Comments
 (0)