-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCommentReducer.js
64 lines (56 loc) · 1.5 KB
/
CommentReducer.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
import {
ADD_COMMENTS,
ADD_COMMENT,
EDIT_COMMENT,
EDIT_COMMENT_MODE,
CANCEL_EDIT_COMMENT,
DELETE_COMMENT,
CLEAR_COMMENTS
} from "./CommentActions";
const initialState = {
comments: [],
editComment: null
};
const CommentReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_COMMENTS:
return { ...state, comments: action.comments };
case ADD_COMMENT: {
return { ...state, comments: [...state.comments, action.comment] };
}
case EDIT_COMMENT: {
let index = state.comments.findIndex(comment => {
if (comment.cuid === action.comment.cuid) {
return true;
}
return false;
});
let startCommentArray = state.comments.slice(0, index);
let endCommentArray = state.comments.slice(
index + 1,
state.comments.length
);
return {
...state,
comments: [...startCommentArray, action.comment, ...endCommentArray]
};
}
case EDIT_COMMENT_MODE:
return { ...state, editComment: action.comment };
case CANCEL_EDIT_COMMENT:
return { ...state, editComment: null };
case DELETE_COMMENT:
return {
editComment: null,
comments: state.comments.filter(comment => comment.cuid !== action.cuid)
};
case CLEAR_COMMENTS:
return initialState;
default:
return state;
}
};
/* Selectors */
// Get corresponding comments
export const getComments = state => state.comments.comments;
export default CommentReducer;