-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.js
88 lines (76 loc) · 1.71 KB
/
store.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'
import {
RECEIVE_POSTS,
RECEIVE_NEW_POSTS,
SET_ACTIVE_TAB,
RECEIVE_LOCATION,
RECEIVE_PARAMS,
RECEIVE_LOADING,
MERGE_NEW_POSTS,
} from './actions'
const initialState = {
activeTab: '',
params: {},
location: {},
posts: { 0: [], 1: [], },
isLoading: false,
}
export const reducer = (state = initialState, action) => {
switch (action.type) {
case RECEIVE_POSTS:
return {
...state,
posts: {
...state.posts,
[action.page]: action.posts,
},
}
case RECEIVE_NEW_POSTS:
return {
...state,
posts: {
...state.posts,
[action.page]: action.posts.concat(state.posts[action.page] || []),
}
}
case MERGE_NEW_POSTS:
return {
...state,
posts: {
...state.posts,
0: [],
1: [...(state.posts[0] || []), ...state.posts[1]]
}
}
case RECEIVE_LOADING:
return {
...state,
isLoading: action.isLoading
}
case RECEIVE_PARAMS:
return {
...state,
params: action.params
}
case RECEIVE_LOCATION:
return {
...state,
location: action.location,
posts: {
1: state.posts[1]
},
}
case SET_ACTIVE_TAB:
return {
...state,
activeTab: action.activeTab
}
default:
return state
}
}
export const initStore = (initialState = initialState) => {
return createStore(reducer, initialState, composeWithDevTools(applyMiddleware(thunkMiddleware)))
}