-
Notifications
You must be signed in to change notification settings - Fork 68
/
store.js
92 lines (81 loc) · 2.39 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
89
90
91
92
import { createAction, handleActions } from 'redux-actions';
import { getMutableState, getImmutableState } from './helpers/state';
export const clearKey = createAction('@redux-conn/CLEAR');
export const beginGlobalLoad = createAction('@redux-conn/BEGIN_GLOBAL_LOAD');
export const endGlobalLoad = createAction('@redux-conn/END_GLOBAL_LOAD');
export const load = createAction('@redux-conn/LOAD', (key) => ({ key }));
export const loadSuccess = createAction('@redux-conn/LOAD_SUCCESS', (key, data) => ({ key, data }));
export const loadFail = createAction('@redux-conn/LOAD_FAIL', (key, error) => ({ key, error }));
const initialState = {
loaded: false,
loadState: {},
};
export const reducer = handleActions({
[beginGlobalLoad]: (state) => ({
...state,
loaded: false,
}),
[endGlobalLoad]: (state) => ({
...state,
loaded: true,
}),
[load]: (state, { payload }) => ({
...state,
loadState: {
...state.loadState,
[payload.key]: {
loading: true,
loaded: false,
},
},
}),
[loadSuccess]: (state, { payload: { key, data } }) => ({
...state,
loadState: {
...state.loadState,
[key]: {
loading: false,
loaded: true,
error: null,
},
},
[key]: data,
}),
[loadFail]: (state, { payload: { key, error } }) => ({
...state,
loadState: {
...state.loadState,
[key]: {
loading: false,
loaded: false,
error,
},
},
}),
[clearKey]: (state, { payload }) => ({
...state,
loadState: {
...state.loadState,
[payload]: {
loading: false,
loaded: false,
error: null,
},
},
[payload]: null,
}),
}, initialState);
export const immutableReducer = function wrapReducer(immutableState, action) {
// We need to convert immutable state to mutable state before our reducer can act upon it
let mutableState;
if (immutableState === undefined) {
// if state is undefined (no initial state yet) then we can't convert it, so let the
// reducer set the initial state for us
mutableState = immutableState;
} else {
// Convert immutable state to mutable state so our reducer will accept it
mutableState = getMutableState(immutableState);
}
// Run the reducer and then re-convert the mutable output state back to immutable state
return getImmutableState(reducer(mutableState, action));
};