Skip to content

Commit f81467a

Browse files
Add some ugly comments
1 parent ea6f364 commit f81467a

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

step-5/src/components/comments.js

Lines changed: 109 additions & 0 deletions
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-5/src/components/wine.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint react/no-multi-comp: 0, react/jsx-max-props-per-line: 0 */
22

33
import React, { PropTypes } from 'react';
4+
import { Comments } from './comments';
45

56
const Styles = {
67
Card: {
@@ -84,7 +85,7 @@ export const Wine = React.createClass({
8485
<div style={Styles.Info}>
8586
<span style={Styles.Label}>Cépages</span>{wine.grapes.join(', ')}
8687
</div>
87-
<div style={Object.assign({}, Styles.Info, { marginTop: 40 })}>
88+
<div style={Object.assign({}, Styles.Info, { marginTop: 40 })}>
8889
<span onClick={this.props.onToggleLike} style={liked ? Styles.Liked : Styles.Like}>{liked ? 'unlike' : 'like'}</span>
8990
</div>
9091
</div>
@@ -159,7 +160,10 @@ export const WinePage = React.createClass({
159160
return <div>Error while fetching wines : {this.state.error.message}</div>
160161
}
161162
return (
162-
<Wine wine={this.state.wine} liked={this.state.liked} onToggleLike={this.handleToggleLike} />
163+
<div>
164+
<Wine wine={this.state.wine} liked={this.state.liked} onToggleLike={this.handleToggleLike} />
165+
<Comments wineId={this.state.wine.id} />
166+
</div>
163167
);
164168
}
165169
});

0 commit comments

Comments
 (0)