-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPostActions.js
More file actions
127 lines (113 loc) · 2.59 KB
/
PostActions.js
File metadata and controls
127 lines (113 loc) · 2.59 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import callApi from '../../util/apiCaller';
// Export Constants
export const ADD_POST = 'ADD_POST';
export const ADD_POSTS = 'ADD_POSTS';
export const DELETE_POST = 'DELETE_POST';
export const ADD_COMMENT = 'ADD_COMMENT';
export const ADD_COMMENTS = 'ADD_COMMENTS';
export const DELETE_COMMENT = 'DELETE_COMMENT';
export const EDIT_COMMENT = 'EDIT_COMMENT';
// Export Actions
export function addPost(post) {
return {
type: ADD_POST,
post,
};
}
export function addPostRequest(post) {
return (dispatch) => {
return callApi('posts', 'post', {
post: {
name: post.name,
title: post.title,
content: post.content,
},
}).then(res => dispatch(addPost(res.post)));
};
}
export function addComment(comment) {
return {
type: ADD_COMMENT,
comment,
};
}
export function addCommentRequest(comment) {
return (dispatch) => {
return callApi('comments', 'post', {
comment: {
name: comment.name,
content: comment.content,
postId: comment.postId,
},
}).then(res => dispatch(addComment(res.comment)));
};
}
export function addPosts(posts) {
return {
type: ADD_POSTS,
posts,
};
}
export function fetchPosts() {
return (dispatch) => {
return callApi('posts').then(res => {
dispatch(addPosts(res.posts));
});
};
}
export function addComments(comments) {
return {
type: ADD_COMMENTS,
comments,
};
}
export function fetchComments(postId) {
return (dispatch) => {
return callApi(`comments/${postId}`).then(res => {
dispatch(addComments(res.comments));
});
};
}
export function fetchPost(cuid) {
return (dispatch) => {
return callApi(`posts/${cuid}`).then(res => dispatch(addPost(res.post)));
};
}
export function deletePost(cuid) {
return {
type: DELETE_POST,
cuid,
};
}
export function deletePostRequest(cuid) {
return (dispatch) => {
return callApi(`posts/${cuid}`, 'delete').then(() => dispatch(deletePost(cuid)));
};
}
export function deleteComment(cuid) {
return {
type: DELETE_COMMENT,
cuid,
};
}
export function deleteCommentRequest(cuid) {
return (dispatch) => {
return callApi(`comments/${cuid}`, 'delete').then(() => dispatch(deleteComment(cuid)));
};
}
export function editComment(comment) {
return {
type: EDIT_COMMENT,
comment,
};
}
export function editCommentRequest(comment) {
return (dispatch) => {
return callApi(`comments/${comment.postId}`, 'post', {
comment: {
name: comment.name,
content: comment.content,
},
}).then(res => dispatch(editComment(res.comment)));
};
}