-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (43 loc) · 1.34 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
import { createStore, combineReducers, applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import createMeta from 'redux-meta-reducer';
const initialState = {
users: [],
};
function resource(state = initialState, action = {}) {
switch (action.type) {
case 'RECEIVE_USERS_SUCCESS':
return { users: action.users };
case 'RECEIVE_USERS_FAILURE':
return { users: [] };
default:
return state;
}
}
// create the root reducer
const reducer = combineReducers({
meta: createMeta({
request: 'REQUEST_USERS',
success: 'RECEIVE_USERS_SUCCESS',
failure: 'RECEIVE_USERS_FAILURE',
}),
resource,
});
// example action creators
function requestUsers() {
return { type: 'REQUEST_USERS' };
}
function receiveUsersSuccess(users) {
return { type: 'RECEIVE_USERS_SUCCESS', now: Date.now(), users };
}
function receiveUsersFailure(error) {
return { type: 'RECEIVE_USERS_FAILURE', now: Date.now(), error };
}
const store = createStore(reducer, applyMiddleware(createLogger()));
// see the developer console for logging of previous/current states
store.dispatch(requestUsers());
store.dispatch(receiveUsersSuccess(['me', 'you']));
store.dispatch(requestUsers());
store.dispatch(receiveUsersFailure({ msg: 'Ah! Error!' }));
store.dispatch(requestUsers());
store.dispatch(receiveUsersSuccess(['him', 'her']));