-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCommentActions.js
52 lines (44 loc) · 1.1 KB
/
CommentActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import callApi from '../../../../util/apiCaller';
export const GET_COMMENTS = 'GET_COMMENTS';
export const ADD_COMMENTS = 'ADD_COMMENTS';
export const ADD_COMMENT = 'ADD_COMMENT';
export const DELETE_COMMENT = 'DELETE_COMMENT';
function addComment(comment) {
return {
type: ADD_COMMENT,
comment,
};
}
function addComments(comments) {
return {
type: ADD_COMMENTS,
comments,
};
}
export function addCommentRequestAPI(comment) {
return (dispatch) => {
console.log(`addCommentRequest ${comment.author}`);
return callApi('comments', 'post', {
comment: {
author: comment.author,
text: comment.text,
postCuid: comment.postCuid,
},
})
.then(res => dispatch(addComment(res.comment)));
};
}
export function getCommentsRequestAPI() {
return (dispatch) => {
return callApi('comments')
.then(res => {
dispatch(addComments(res.comments));
});
};
}
export function todeleteCommentAPI(cuid, dispatch) {
return callApi(`comments/${cuid}`, 'delete')
.then(() => {
dispatch({ type: DELETE_COMMENT, cuid });
});
}