-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathindex.js
60 lines (54 loc) · 1.27 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
54
55
56
57
58
59
60
import { combineReducers } from 'redux';
import {
SELECT_REDDIT, INVALIDATE_REDDIT,
REQUEST_POSTS, RECEIVE_POSTS
} from '../constants/ActionTypes';
import { fromJS } from 'immutable'
function selectedReddit(state = 'angularjs', action) {
switch (action.type) {
case SELECT_REDDIT:
return action.reddit;
default:
return state;
}
}
function posts(state = fromJS({
isFetching: false,
didInvalidate: false,
items: []
}), action) {
switch (action.type) {
case INVALIDATE_REDDIT:
return state.set('didInvalidate', true);
case REQUEST_POSTS:
return state.mergeDeep(fromJS({
isFetching: true,
didInvalidate: false,
}));
case RECEIVE_POSTS:
var updatedState = state.mergeDeep(fromJS({
isFetching: false,
didInvalidate: false,
items: action.posts,
lastUpdated: action.receivedAt
}));
return updatedState;
default:
return state;
}
}
function postsByReddit(state = fromJS({}), action) {
switch (action.type) {
case INVALIDATE_REDDIT:
case RECEIVE_POSTS:
case REQUEST_POSTS:
return state.set(action.reddit, posts(state.get(action.reddit), action));
default:
return state;
}
}
const rootReducer = combineReducers({
postsByReddit,
selectedReddit
});
export default rootReducer;