File size: 1,048 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
28
29
30
31
32
33
34
35
36
import { RECEIVE_CURRENT_USER } from '../actions/session_actions';
import { RECEIVE_USERS, RECEIVE_USER } from '../actions/user_actions';
import { RECEIVE_POST_PAYLOAD } from '../actions/post_actions';

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

  switch (action.type) {
    case RECEIVE_POST_PAYLOAD:      
      for (let userId in action.postPayload.users) {
        const user = action.postPayload.users[userId];
        
        if (newState[userId]) {
          Object.assign(newState[userId], user);
        } else {
          newState[userId] = user;
        }
      }

      return newState;
    case RECEIVE_USERS:
      return Object.assign(newState, action.users);
    case RECEIVE_CURRENT_USER:
      return Object.assign(newState, {
        [action.currentUser.id]: action.currentUser
      });
    case RECEIVE_USER:
      return Object.assign(newState, { [action.user.id]: action.user });
    default:
      return state;
  }
};

export default usersReducer;