forked from shakacode/react-webpack-rails-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommentsActionCreators.js
80 lines (70 loc) · 1.58 KB
/
commentsActionCreators.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
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
import requestsManager from 'libs/requestsManager';
import * as actionTypes from '../constants/commentsConstants';
export function setIsFetching() {
return {
type: actionTypes.SET_IS_FETCHING,
};
}
export function setIsSaving() {
return {
type: actionTypes.SET_IS_SAVING,
};
}
export function fetchCommentsSuccess(data) {
return {
type: actionTypes.FETCH_COMMENTS_SUCCESS,
comments: data.comments,
};
}
export function fetchCommentsFailure(error) {
return {
type: actionTypes.FETCH_COMMENTS_FAILURE,
error,
};
}
export function messageReceived(comment) {
return {
type: actionTypes.MESSAGE_RECEIVED,
comment,
};
}
export function submitCommentSuccess(comment) {
return {
type: actionTypes.SUBMIT_COMMENT_SUCCESS,
comment,
};
}
export function submitCommentFailure(error) {
return {
type: actionTypes.SUBMIT_COMMENT_FAILURE,
error,
};
}
export function fetchComments() {
return (dispatch) => {
dispatch(setIsFetching());
return (
requestsManager
.fetchEntities()
.then(res => dispatch(fetchCommentsSuccess(res.data)))
.catch(error => dispatch(fetchCommentsFailure(error)))
);
};
}
export function submitComment(comment) {
return (dispatch) => {
dispatch(setIsSaving());
return (
requestsManager
.submitEntity({ comment })
.then(res => dispatch(submitCommentSuccess(res.data)))
.catch(error => dispatch(submitCommentFailure(error)))
);
};
}
export function setLocale(locale) {
return {
type: actionTypes.SET_LOCALE,
locale,
};
}