Skip to content

Commit 568dce0

Browse files
committed
Refine reducers:
1. move initialState to store and inject it when store creating 2. separate reducers
1 parent 8dbd698 commit 568dce0

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src-react/reducers/index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import * as types from 'constants/action-types';
1+
import { combineReducers } from 'redux';
22

3-
const initialState = {
4-
ungitConfig: null
5-
};
3+
import path from './path';
4+
import ungitConfig from './ungit-config';
65

7-
const ungitApp = function(state = initialState, action) {
8-
switch(action.type) {
9-
case types.RECEIVE_UNGIT_CONFIG:
10-
const { ungitConfig } = action;
11-
return { ...state, ungitConfig };
12-
default:
13-
return { ...state };
14-
}
15-
}
6+
7+
const ungitApp = combineReducers({
8+
ungitConfig,
9+
path
10+
});
1611

1712
export default ungitApp;

src-react/reducers/path.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as types from 'constants/action-types';
2+
3+
function path(state, action) {
4+
switch(action.type) {
5+
case types.PATH_PAGE_PENDING:
6+
return { ...state, pending: true };
7+
case types.RECEIVE_UNGIT_CONFIG:
8+
return { ...state, pending: false };
9+
case types.PATH_PAGE_API_ERR:
10+
const { payload: errMessage } = action
11+
return { ...state, pending: false, errMessage };
12+
default:
13+
return { ...state };
14+
}
15+
}
16+
17+
export default path;

src-react/reducers/ungit-config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as types from 'constants/action-types';
2+
3+
function ungitConfig(state, action) {
4+
switch(action.type) {
5+
case types.RECEIVE_UNGIT_CONFIG:
6+
const { payload: ungitConfig } = action;
7+
return { ...ungitConfig };
8+
default:
9+
return { ...state };
10+
}
11+
}
12+
13+
export default ungitConfig;

src-react/store.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { createStore, applyMiddleware } from 'redux';
22
import thunk from 'redux-thunk';
33
import ungitApp from './reducers';
44

5-
const store = createStore(ungitApp, applyMiddleware(thunk));
5+
const initialState = {
6+
ungitConfig: null,
7+
path: {
8+
pending: false,
9+
errMessage: null
10+
}
11+
};
12+
13+
const store = createStore(ungitApp, initialState, applyMiddleware(thunk));
614

715
export default store;

0 commit comments

Comments
 (0)