-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.jsx
More file actions
58 lines (47 loc) · 1.73 KB
/
Copy pathstate.jsx
File metadata and controls
58 lines (47 loc) · 1.73 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
const initState = {
task: null,
comments: [],
};
export const reducer = (state = initState, action) => {
switch (action.type) {
case 'SET_COMMENTS':
return { ...state, comments: action.payload };
case 'SET_TASK':
return { ...state, task: action.payload };
default:
return state;
}
};
import http from 'utils/http';
import { push } from 'redux-router';
const setComments = (comments) => ({ type: 'SET_COMMENTS', payload: comments });
const setTask = (task) => ({ type: 'SET_TASK', payload: task });
const loadComments = (id) => (dispatch) =>
http.get(`/api/tasks/${id}/comments/page/0/5`)
.then(json => dispatch(setComments(json.items)));
const loadTask = (id) => (dispatch) =>
http.get(`/api/tasks/${id}`)
.then(json => dispatch(setTask(json)));
// PUBLICK
export const deleteTask = () => (dispatch, getState) => {
const { router: { params: { id, projectId } } } = getState();
return http.del(`/api/tasks/${id}`).then(() => dispatch(push(`/projects/${projectId}/tasks`)));
};
export const addComment = (text) => (dispatch, getState) => {
const {
auth: { user: { name } },
router: { params: { id } },
} = getState();
return http.post(`/api/tasks/${id}/comments`, { text, userName: name })
.then(() => dispatch(loadComments(id)));
};
export const deleteComment = (comment) => (dispatch, getState) => {
const { router: { params: { id } } } = getState();
return http.del(`/api/tasks/${id}/comments/${comment.id}`)
.then(() => dispatch(loadComments(id)));
};
export const showPage = ({ id }) => (dispatch) =>
Promise.all([
dispatch(loadComments(id)),
dispatch(loadTask(id)),
]);