File size: 1,097 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
37
38
import {RECEIVE_CURRENT_USER,
   RECEIVE_SEARCH,CLEAR_SEARCH_RESULTS,RECEIVE_ALL_USERS,RECEIVE_USER }
from '../actions/session_actions';
import {merge} from 'lodash';

const usersReducer = (state = { search_results: []}, action) => {
  Object.freeze(state);


  switch (action.type) {
    case RECEIVE_ALL_USERS:
    return action.users;
    case RECEIVE_CURRENT_USER:
    const newUser =
    merge({},state,action.currentUser);
    return newUser;
    case RECEIVE_USER:
      // const newUser = { [action.user.id]: action.user};
      let newState = Object.assign({}, state, action.user);
      return newState;
      // const newState = merge({},state);
      // newState[action.user.id] = action.user;
      // return newState;
    case CLEAR_SEARCH_RESULTS:
     let  clearState = merge({}, state);
       clearState.search_results = [];
       return clearState;
    case RECEIVE_SEARCH:
      let  newSearchState = merge({}, state);
       newSearchState.search_results = action.results;
       return newSearchState;
     default:
       return state;
  }
};

export default usersReducer;