Skip to content

Commit ad34ea2

Browse files
committed
Fetch POST comments
1 parent 74b4637 commit ad34ea2

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/components/DishdetailComponent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CommentForm extends Component{
2525
handleSubmit(values) {
2626
console.log(" Current State is :" +JSON.stringify(values));
2727
//alert(" Current State is :" +JSON.stringify(values));
28-
this.props.addComment(this.props.dishId, values.rating, values.author, values.comment);
28+
this.props.postComment(this.props.dishId, values.rating, values.author, values.comment);
2929
}
3030

3131

@@ -130,7 +130,7 @@ class CommentForm extends Component{
130130
);
131131
}
132132

133-
function RenderComments({comments, addComment, dishId}) {
133+
function RenderComments({comments, postComment, dishId}) {
134134
var commentList = comments.map(comment => {
135135
return (
136136
<li key={comment.id} >
@@ -149,7 +149,7 @@ class CommentForm extends Component{
149149
{commentList}
150150
</ul>
151151
<ul>
152-
<CommentForm dishId={dishId} addComment={addComment}/>
152+
<CommentForm dishId={dishId} postComment={postComment}/>
153153
</ul>
154154
</div>
155155
);
@@ -195,7 +195,7 @@ class CommentForm extends Component{
195195
</div>
196196
<div className="col-12 col-md-5 m-1">
197197
<RenderComments comments={props.comments}
198-
addComment = {props.addComment}
198+
postComment = {props.postComment}
199199
dishId ={props.dish.id}
200200
/>
201201

src/components/MainComponent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Header from './HeaderComponent';
88
import Footer from './FooterComponent';
99
import { Switch, Route, Redirect, withRouter } from 'react-router-dom';
1010
import { connect } from 'react-redux';
11-
import { addComment, fetchDishes, fetchComments, fetchPromos } from '../redux/ActionCreators';
11+
import { postComment, fetchDishes, fetchComments, fetchPromos } from '../redux/ActionCreators';
1212
import { actions } from 'react-redux-form';
1313

1414
const mapStateToProps = state =>{
@@ -20,7 +20,7 @@ const mapStateToProps = state =>{
2020
}
2121
}
2222
const mapDispatchToProps = (dispatch) => ({
23-
addComment: (dishId, rating, author, comment ) => dispatch(addComment(dishId, rating, author, comment )),
23+
postComment: (dishId, rating, author, comment ) => dispatch(postComment(dishId, rating, author, comment )),
2424
fetchDishes: () => {dispatch(fetchDishes())},
2525
resetFeedbackForm: () =>{dispatch(actions.reset('feedback'))},
2626
fetchComments: () => dispatch(fetchComments()),
@@ -60,7 +60,7 @@ class Main extends Component {
6060
errMess={this.props.dishes.errMess}
6161
comments={this.props.comments.comments.filter((comment) => comment.dishId === parseInt(match.params.dishId,10))}
6262
commentsErrMess={this.props.comments.errMess}
63-
addComment={this.props.addComment}
63+
postComment={this.props.postComment}
6464
/>
6565
);
6666
};

src/redux/ActionCreators.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,49 @@ import * as ActionTypes from './ActionTypes';
22
//import { DISHES } from '../shared/dishes';
33
import { baseUrl } from '../shared/baseUrl';
44

5-
export const addComment = (dishId, rating, author, comment) => ({
5+
export const addComment = (comment) => ({
66
type: ActionTypes.ADD_COMMENT,
7-
payload : {
8-
dishId: dishId,
9-
rating: rating,
10-
author: author,
11-
comment: comment
12-
}
7+
payload : comment
138
});
9+
10+
export const postComment =(dishId, rating, author, comment) => (dispatch) => {
11+
12+
const newComment = {
13+
dishId: dishId,
14+
rating: rating,
15+
author: author,
16+
comment: comment
17+
}
18+
newComment.date = new Date().toISOString();
19+
20+
return fetch(baseUrl + 'comments', {
21+
method: 'POST',
22+
body: JSON.stringify(newComment),
23+
headers:{
24+
'Content-Type':'application/json'
25+
},
26+
credentials: 'same-origin'
27+
})
28+
.then(response => {
29+
if(response.ok){
30+
return response;
31+
}
32+
else{
33+
var error = new Error('Error ' + response.status + ':' + response.statusText);
34+
error.response = response;
35+
throw error;
36+
}
37+
},
38+
error => {
39+
var errmess = new Error(error.message);
40+
throw errmess;
41+
})
42+
.then(response => response.json())
43+
.then(response => dispatch(addComment(response)))
44+
.catch(error => { console.log('post comments', error.message);
45+
alert('Your comment could not be posted\nError: '+error.message); });
46+
}
47+
1448
export const fetchDishes = () => (dispatch) => {
1549

1650
dispatch(dishesLoading(true));

src/redux/comment.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export const Comments = (state = {
1515

1616
case ActionTypes.ADD_COMMENT:
1717
var comment = action.payload;
18-
comment.id = state.comments.length;
19-
comment.date = new Date().toISOString();
2018
return {...state, comments: state.comments.concat(comment)};
2119
default:
2220
return state;

0 commit comments

Comments
 (0)