-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomments.jsx
More file actions
55 lines (44 loc) · 1.37 KB
/
Copy pathcomments.jsx
File metadata and controls
55 lines (44 loc) · 1.37 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
const initState = {
comments: [],
};
export function reducer(state = initState, action) {
switch (action.type) {
case 'SET_COMMENTS':
return { ...state, comments: action.payload };
case 'REMOVE_COMMENTS': {
const comments = state.comments.filter(coment => coment.id !== action.payload.id);
return { ...state, comments };
}
default:
return state;
}
}
import http from 'utils/http';
function setComments(payload) {
return {
type: 'SET_COMMENTS',
payload,
};
}
export function loadComments({ id }) {
// TODO: make load more
return (dispatch) => http.get(`/api/tasks/${id}/comments/page/0/5`)
.then(json => dispatch(setComments(json.items)));
}
export function addComment(text) {
return (dispatch, getState) => {
const {
auth: { user: { name } },
router: { params: { id } },
} = getState();
http.post(`/api/tasks/${id}/comments`, { text, userName: name })
.then(() => dispatch(loadComments({ id })));
};
}
export function removeComent(comment) {
return (dispatch, getState) => {
const { router: { params: { id } } } = getState();
return http.del(`/api/tasks/${id}/comments/${comment.id}`)
.then(() => dispatch({ type: 'REMOVE_COMMENTS', payload: comment }));
};
}