File size: 736 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { 
  RECEIVE_COMMENT 
} from '../actions/comment_actions';
import { RECEIVE_POST_PAYLOAD } from '../actions/post_actions';

const commentsReducer = (state = {}, action) => {
  Object.freeze(state);
  const newState = Object.assign({}, state);

  switch (action.type) {
    case RECEIVE_POST_PAYLOAD:
      return Object.assign(newState, action.postPayload.comments);
    case RECEIVE_COMMENT:
      const {id, authorId, postId, body} = action.comment;
    
      if (newState[postId]) {
        newState[postId] = [...newState[postId], {id, authorId, body}];
      } else {
        newState[postId] = [{id, authorId, body}];
      }
      return newState;
    default: 
      return state;
  }
};

export default commentsReducer;