Skip to content

Instantly share code, notes, and snippets.

@malectro
Last active May 18, 2017 18:35
Show Gist options
  • Save malectro/601b0cdb88045fb6f1e2049d22a2ea22 to your computer and use it in GitHub Desktop.
Save malectro/601b0cdb88045fb6f1e2049d22a2ea22 to your computer and use it in GitHub Desktop.
const usersReducer = (state = {users: {}, fetching: {}, cache: {}}, {type, payload}) => {
switch (type) {
case START_FETCHING:
return {
...state,
fetching: {
...state.fetching,
[payload.userId]: payload.promise,
},
};
case STOP_FETCHING:
return {
...state,
fetching: {
...state.fetching,
[payload.userId]: null,
},
};
case RECEIVE_USER:
return {
...state,
cache: {
...state.fetching,
[payload.user.id]: {
expireTime: Date.now() + 60000,
},
},
users: {
...state.users,
[payload.user.id]: payload.user,
},
};
}
return state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment