Skip to content

Commit b269f5e

Browse files
author
James Wilson
committed
add logic to show error if comments don't retrieve
1 parent 5e210f0 commit b269f5e

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

src/AddComment.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import {BASE_URL} from './constants';
44

55

66
class AddComment extends React.Component{
7-
7+
88
state = {
99
active: false,
1010
body: '',
1111
}
12-
13-
submitComment = () => {
12+
13+
submitComment = (event) => {
14+
event.preventDefault();
1415
const {_id} = this.props;
1516
const {body} = this.state;
16-
17+
1718
fetch(`${BASE_URL}/articles/${_id}/comments`,
1819
{
1920
method: 'POST',
@@ -34,50 +35,56 @@ class AddComment extends React.Component{
3435
this.setState({body:''});
3536
});
3637
}
37-
38+
3839
handleKeys = event => {
3940
if (event.key === 'Enter') this.submitComment();
4041
}
41-
42+
4243
render () {
43-
if (this.state.active) return (
44+
const {body, active} = this.state;
45+
if (active) return (
4446
<form>
4547
<input
4648
type="text"
4749
className="input field"
4850
autoFocus
4951
onChange={({target:{value}}) => this.setState({body:value})}
5052
onKeyPress={this.handleKeys}
51-
value={this.state.body}
53+
value={body}
5254
/>
5355
<div className="field is-grouped">
5456
<button
57+
type="button"
5558
className="button control"
5659
onClick={this.submitComment}
60+
disabled={!body}
5761
>
5862
Submit Comment
5963
</button>
64+
6065
<button
66+
type="button"
6167
className="button control"
6268
onClick={()=>this.setState({active:false, body:''})}
6369
>
6470
Cancel
6571
</button>
6672
</div>
67-
73+
6874
</form>
6975
);
70-
76+
7177
else return (
72-
78+
7379
<button
74-
className="button control"
75-
onClick={()=>this.setState({active:true})}>
76-
{/* <FontAwesomeIcon icon={faPlus}/> */} New Comment
80+
className="button control"
81+
onClick={()=>this.setState({active:true})}
82+
>
83+
New Comment
7784
</button>
78-
85+
7986
);
80-
87+
8188
}
8289
}
8390

src/Article.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Article extends React.Component {
2323
},
2424
comments: [],
2525
status: 200,
26+
commentError: false,
2627
}
2728

2829
componentDidMount () {
@@ -32,7 +33,10 @@ class Article extends React.Component {
3233
.then(newState => this.setState(newState));
3334

3435
getCommentsFromArticleID(_id)
35-
.then(newState => this.setState(newState));
36+
.then(newState => {
37+
if (newState.status !== undefined) this.setState({commentError: true});
38+
else this.setState(newState);
39+
});
3640

3741
}
3842

@@ -62,7 +66,7 @@ class Article extends React.Component {
6266
}
6367

6468
render () {
65-
const {status} = this.state;
69+
const {status, commentError} = this.state;
6670
if (status !== 200) return <Error status={status}/>;
6771

6872
const {topicName, topicId, title, avatarUrl, authorName,
@@ -85,13 +89,21 @@ class Article extends React.Component {
8589
<AuthorName name={authorName} _id={authorId}/>
8690
</h2>
8791
<p>{body}</p>
92+
93+
{commentError
94+
?
95+
<p> Problem retrieving Comments </p>
96+
:
8897
<CommentList
8998
comments={comments}
9099
changeVoting={this.changeVoting}
91100
_id={_id}
92101
optimisticallyAddComment={this.optimisticallyAddComment}
93102
optimisticallyDeleteComment={this.optimisticallyDeleteComment}
94103
/>
104+
}
105+
106+
95107
</div>
96108
);
97109
}

src/CommentList.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class CommentList extends React.Component {
2727
this.setState({orderBy:value});
2828
};
2929

30-
31-
3230
render () {
3331
const {comments, changeVoting, _id, optimisticallyAddComment,
3432
optimisticallyDeleteComment} = this.props;
@@ -50,7 +48,6 @@ class CommentList extends React.Component {
5048
optimisticallyDeleteComment={optimisticallyDeleteComment}
5149
/>
5250

53-
5451
{sortedComments.map(comment => {
5552
return <Comment
5653
{...comment}

0 commit comments

Comments
 (0)