-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCommentsActions.js
More file actions
91 lines (78 loc) · 2.1 KB
/
CommentsActions.js
File metadata and controls
91 lines (78 loc) · 2.1 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import callApi from '../../util/apiCaller';
// Export Constants
export const ADD_COMMENT_SUCCESS = 'ADD_COMMENT_SUCCESS';
export const ADD_COMMENT_FAILURE = 'ADD_COMMENT_FAILURE';
export const EDIT_COMMENT_SUCCESS = 'EDIT_COMMENT_SUCCESS';
export const EDIT_COMMENT_FAILURE = 'EDIT_COMMENT_FAILURE';
export const DELETE_COMMENT_SUCCESS = 'DELETE_COMMENT_SUCCESS';
export const DELETE_COMMENT_FAILURE = 'DELETE_COMMENT_FAILURE';
// Export Actions
export function addCommentSuccess(comment) {
return {
type: ADD_COMMENT_SUCCESS,
comment,
};
}
export function addCommentFailure(error) {
return {
type: ADD_COMMENT_FAILURE,
error,
};
}
export function addCommentRequest({ comment, resolve, reject, postId }) {
return (dispatch) => {
return callApi(`comments/${postId}`, 'post', { comment })
.then(res => {
if (resolve) resolve();
dispatch(addCommentSuccess(res.comment));
})
.catch(error => {
if (reject) reject(error);
dispatch(addCommentFailure(error));
});
};
}
export function editCommentSuccess(comment) {
return {
type: EDIT_COMMENT_SUCCESS,
comment,
};
}
export function editCommentFailure(error) {
return {
type: EDIT_COMMENT_FAILURE,
error,
};
}
export function editCommentRequest({ comment, resolve, reject }) {
return (dispatch) => {
return callApi(`comments/${comment.cuid}`, 'put', { comment })
.then(res => {
if (resolve) resolve();
dispatch(editCommentSuccess(res.comment));
})
.catch(error => {
if (reject) reject(error);
dispatch(editCommentFailure(error));
});
};
}
export function deleteCommentSuccess(comment) {
return {
type: DELETE_COMMENT_SUCCESS,
comment,
};
}
export function deleteCommentFailure(error) {
return {
type: DELETE_COMMENT_FAILURE,
error,
};
}
export function deleteCommentRequest(comment) {
return (dispatch) => {
return callApi(`comments/${comment.cuid}`, 'delete')
.then(() => dispatch(deleteCommentSuccess(comment)))
.catch(error => dispatch(deleteCommentFailure(error)));
};
}