Skip to content

Commit 033f640

Browse files
Add step 6
1 parent f81467a commit 033f640

19 files changed

+690
-0
lines changed

step-6/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015", "react"]
3+
}

step-6/.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
webpack.config.js
3+
public
4+
README.md

step-6/.eslintrc

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"env": {
4+
"browser": true,
5+
"node": true,
6+
"es6": true
7+
},
8+
"plugins": [
9+
"react"
10+
],
11+
"ecmaFeatures": {
12+
"jsx": true,
13+
"modules": true
14+
},
15+
"rules": {
16+
"jsx-quotes": 1,
17+
"react/display-name": 0,
18+
"react/forbid-prop-types": 1,
19+
"react/jsx-boolean-value": 1,
20+
"react/jsx-closing-bracket-location": 1,
21+
"react/jsx-curly-spacing": 1,
22+
"react/jsx-handler-names": 1,
23+
"react/jsx-indent-props": 1,
24+
"react/jsx-key": 1,
25+
"react/jsx-max-props-per-line": 1,
26+
"react/jsx-no-bind": 1,
27+
"react/jsx-no-duplicate-props": 1,
28+
"react/jsx-no-literals": 0,
29+
"react/jsx-no-undef": 1,
30+
"react/jsx-pascal-case": 1,
31+
"react/jsx-sort-prop-types": 1,
32+
"react/jsx-sort-props": 0,
33+
"react/jsx-uses-react": 1,
34+
"react/jsx-uses-vars": 1,
35+
"react/no-danger": 1,
36+
"react/no-did-mount-set-state": 0,
37+
"react/no-did-update-set-state": 0,
38+
"react/no-direct-mutation-state": 1,
39+
"react/no-multi-comp": 1,
40+
"react/no-set-state": 0,
41+
"react/no-unknown-property": 1,
42+
"react/prefer-es6-class": 0,
43+
"react/prop-types": 1,
44+
"react/react-in-jsx-scope": 1,
45+
"react/require-extension": 1,
46+
"react/self-closing-comp": 1,
47+
"react/sort-comp": 1,
48+
"react/wrap-multilines": 1
49+
}
50+
}

step-6/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Node.js
2+
node_modules
3+
npm-debug.log
4+
5+
# Bundles
6+
bundle.js
7+
bundle.js.map

step-6/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "react-workshop",
3+
"description": "React Workshop",
4+
"version": "0.1.0",
5+
"dependencies": {
6+
"react": "0.14.7",
7+
"react-dom": "0.14.7",
8+
"react-router": "^2.0.1",
9+
"whatwg-fetch": "^0.11.0"
10+
},
11+
"devDependencies": {
12+
"webpack": "1.12.9",
13+
"webpack-dev-server": "1.14.0",
14+
"babel-loader": "6.2.0",
15+
"babel-preset-es2015": "6.1.18",
16+
"babel-preset-react": "6.1.18",
17+
"babel-register": "6.3.13",
18+
"eslint": "1.10.3",
19+
"eslint-plugin-react": "3.11.3"
20+
},
21+
"scripts": {
22+
"lint": "eslint src",
23+
"bundle": "webpack -p --colors --progress",
24+
"start": "webpack-dev-server -d --colors --inline --content-base public",
25+
"build": "npm run lint && npm run bundle"
26+
}
27+
}

step-6/public/css/avalanche.css

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

step-6/public/css/main.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
font-family: 'Roboto', sans-serif;
3+
}

step-6/public/img/chevrol-bel-air.png

28.6 KB
Loading

step-6/public/img/react.png

13.2 KB
Loading

step-6/public/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>React Workshop</title>
6+
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
7+
<link href="./css/avalanche.css" rel="stylesheet" type="text/css">
8+
<link href="./css/main.css" rel="stylesheet" type="text/css">
9+
<link rel="icon" type="image/png" href="/img/react.png">
10+
</head>
11+
<body>
12+
<h1>Wines</h1>
13+
14+
<div id="main"></div>
15+
16+
<script src="/js/bundle.js"></script>
17+
</body>
18+
</html>

step-6/public/js/.gitkeep

Whitespace-only changes.

step-6/src/app.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint react/jsx-max-props-per-line: 0 */
2+
3+
import 'whatwg-fetch';
4+
5+
import React from 'react';
6+
import ReactDOM from 'react-dom';
7+
8+
import WineApp from './components/wine-app';
9+
import { RegionsPage } from './components/regions';
10+
import { WineListPage } from './components/wine-list';
11+
import { WinePage } from './components/wine';
12+
import { NotFound } from './components/not-found';
13+
14+
import { Router, Route, hashHistory, IndexRoute } from 'react-router';
15+
16+
ReactDOM.render(
17+
<Router history={hashHistory}>
18+
<Route path="/" component={WineApp}>
19+
<IndexRoute component={RegionsPage} />
20+
<Route path="regions/:regionId" component={WineListPage} />
21+
<Route path="regions/:regionId/wines/:wineId" component={WinePage} />
22+
<Route path="*" component={NotFound} />
23+
</Route>
24+
</Router>
25+
, document.getElementById('main')
26+
);

step-6/src/components/comments.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* eslint react/no-multi-comp: 0, react/jsx-max-props-per-line: 0 */
2+
3+
import React, { PropTypes } from 'react';
4+
5+
export const Comment = React.createClass({
6+
render() {
7+
return (
8+
<span>Comment</span>
9+
);
10+
}
11+
});
12+
13+
export const Comments = React.createClass({
14+
15+
propTypes: {
16+
wineId: PropTypes.string.isRequired
17+
},
18+
19+
getInitialState() {
20+
return {
21+
comments: [],
22+
loaded: false,
23+
error: null,
24+
commentTitle: '',
25+
commentBody: ''
26+
};
27+
},
28+
29+
componentDidMount() {
30+
this.updateComments();
31+
},
32+
33+
updateComments() {
34+
fetch(`http://localhost:3000/api/wines/${this.props.wineId}/comments`)
35+
.then(r => r.json())
36+
.then(comments => {
37+
this.setState({ comments: comments.sort((a, b) => new Date(b.date) - new Date(a.date)), loaded: true });
38+
})
39+
.catch(error => {
40+
this.setState({ error, loaded: true });
41+
});
42+
},
43+
44+
handleCommentTitleChange(e) {
45+
this.setState({ commentTitle: e.target.value });
46+
},
47+
48+
handleCommentBodyChange(e) {
49+
this.setState({ commentBody: e.target.value });
50+
},
51+
52+
handlePostComment() {
53+
fetch(`http://localhost:3000/api/wines/${this.props.wineId}/comments`, {
54+
method: 'post',
55+
headers: {
56+
'Accept': 'application/json',
57+
'Content-Type': 'application/json'
58+
},
59+
body: JSON.stringify({
60+
title: this.state.commentTitle,
61+
content: this.state.commentBody
62+
})
63+
})
64+
.then(() => {
65+
this.updateComments();
66+
this.setState({ commentTitle: '', commentBody: '' });
67+
})
68+
.catch(error => {
69+
this.setState({ error });
70+
});
71+
},
72+
73+
render() {
74+
if (!this.state.loaded) {
75+
return <div>Loading ...</div>
76+
}
77+
if (this.state.error) {
78+
return <div>Error while fetching comments : {this.state.error.message}</div>
79+
}
80+
return (
81+
<div>
82+
<h3>Comments</h3>
83+
<div style={{ display: 'flex', flexDirection: 'column' }}>
84+
<div style={{ display: 'flex', marginBottom: 10 }}>
85+
<input
86+
onChange={this.handleCommentTitleChange}
87+
style={{ flexGrow: 8 }}
88+
type="text"
89+
placeholder="Comment title"
90+
value={this.state.commentTitle} />
91+
<button
92+
onClick={this.handlePostComment}
93+
style={{ flexGrow: 2, marginLeft: 10 }}
94+
type="button">Comment</button>
95+
</div>
96+
<textarea onChange={this.handleCommentBodyChange} placeholder="Comment" rows="5" value={this.state.commentBody}></textarea>
97+
</div>
98+
{
99+
this.state.comments.map(comment =>
100+
<div key={comment.date.toString()} style={{ padding: 10, backgroundColor: '#ececec', marginTop: 5 }}>
101+
<span>{comment.title} (le <small>{comment.date}</small>)</span>
102+
<p>{comment.content}</p>
103+
</div>
104+
)
105+
}
106+
</div>
107+
);
108+
}
109+
});

step-6/src/components/not-found.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React, { PropTypes } from 'react';
2+
3+
export const NotFound = React.createClass({
4+
render() {
5+
return (
6+
<h2>Il semble que vous n'êtes pas au bon endroit !!!</h2>
7+
);
8+
}
9+
});

step-6/src/components/regions.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* eslint react/jsx-no-bind: 0, react/no-multi-comp: 0, react/jsx-closing-bracket-location: 0 */
2+
3+
import React, { PropTypes } from 'react';
4+
5+
const computeRegionStyle = function(region, selected) {
6+
let style = {
7+
padding: 16,
8+
cursor: 'pointer'
9+
};
10+
if (region === selected) {
11+
style['fontWeight'] = 'bold';
12+
style['backgroundColor'] = 'lightGrey';
13+
}
14+
return style;
15+
}
16+
17+
export const Regions = React.createClass({
18+
propTypes: {
19+
onRegionChange: PropTypes.func,
20+
regions: PropTypes.arrayOf(PropTypes.string)
21+
},
22+
23+
handleRegionClick(event) {
24+
this.props.onRegionChange(event.target.textContent);
25+
},
26+
27+
render () {
28+
return (
29+
<div>
30+
{
31+
this.props.regions.map(region =>
32+
<div key={region}
33+
style={computeRegionStyle(region, null)}
34+
onClick={this.handleRegionClick}>
35+
{region}
36+
</div>
37+
)
38+
}
39+
</div>
40+
)
41+
}
42+
});
43+
44+
export const RegionsPage = React.createClass({
45+
46+
propTypes: {
47+
history: PropTypes.shape({
48+
push: PropTypes.func.isRequired
49+
}),
50+
setTitle: PropTypes.func
51+
},
52+
53+
contextTypes: {
54+
router: React.PropTypes.object
55+
},
56+
57+
getInitialState() {
58+
return {
59+
regions: [],
60+
loaded: false,
61+
error: null
62+
};
63+
},
64+
65+
componentDidMount() {
66+
fetch('http://localhost:3000/api/regions')
67+
.then(r => r.json())
68+
.then(data => {
69+
this.setState({ regions: data, loaded: true });
70+
this.props.setTitle(`Regions`);
71+
})
72+
.catch(error => {
73+
this.setState({ error, loaded: true });
74+
});
75+
},
76+
77+
handleNavigateToRegion(region) {
78+
this.context.router.push({
79+
pathname: `/regions/${region}`
80+
});
81+
},
82+
83+
render () {
84+
if (!this.state.loaded) {
85+
return <div>Loading ...</div>
86+
}
87+
if (this.state.error) {
88+
return <div>Error while fetching regions : {this.state.error.message}</div>
89+
}
90+
return (
91+
<Regions regions={this.state.regions}
92+
onRegionChange={this.handleNavigateToRegion} />
93+
);
94+
}
95+
});

0 commit comments

Comments
 (0)