forked from jeffbski/redux-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.js
41 lines (38 loc) · 1005 Bytes
/
reducer.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
import { key, USER_PROFILE_FETCH, USER_PROFILE_FETCH_CANCEL, USER_PROFILE_FETCH_FULFILLED,
USER_PROFILE_FETCH_REJECTED } from './actions';
export const selectors = {
user: state => state[key].user,
fetchStatus: state => state[key].fetchStatus
};
const initialState = {
user: null,
fetchStatus: ''
};
export default function reducer(state = initialState, action) {
switch(action.type) {
case USER_PROFILE_FETCH:
return {
...state,
fetchStatus: `fetching ID:${action.payload}... ${(new Date()).toLocaleString()}`,
user: null
};
case USER_PROFILE_FETCH_FULFILLED:
return {
...state,
user: action.payload,
fetchStatus: `Results from ${(new Date()).toLocaleString()}`
};
case USER_PROFILE_FETCH_REJECTED:
return {
...state,
fetchStatus: `errored: ${action.payload}`
};
case USER_PROFILE_FETCH_CANCEL:
return {
...state,
fetchStatus: 'user cancelled'
};
default:
return state;
}
}