|
| 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 | +}); |
0 commit comments