Skip to content

Commit 40dfa14

Browse files
committed
feat(reducer): allow routes after action creators
1 parent ab31a95 commit 40dfa14

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/helpers/getAction.js

+29-8
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,60 @@ import getRoute from '../helpers/getRoute';
33
import updateUrl from '../actions/updateUrl';
44
import { CHANGE_PAGE_TO } from '../constants';
55

6-
const getActionList = ( url, actionCreators, data ) => {
6+
const getActions = ( url, actionCreators, data ) => {
77
const actions = [
88
updateUrl( url ),
99
];
1010

1111
const keys = Object.keys( actionCreators );
1212

1313
keys.forEach( k => {
14-
if ( Array.isArray( actionCreators[ k ])) {
15-
actionCreators[ k ].forEach( actionCreator => {
14+
if ( k !== 'after' ) {
15+
if ( Array.isArray( actionCreators[ k ])) {
16+
actionCreators[ k ].forEach( actionCreator => {
17+
actions.push( actionCreator( data[ k ]));
18+
});
19+
} else {
20+
const actionCreator = actionCreators[ k ];
1621
actions.push( actionCreator( data[ k ]));
17-
});
18-
} else {
19-
const actionCreator = actionCreators[ k ];
20-
actions.push( actionCreator( data[ k ]));
22+
}
2123
}
2224
});
2325

2426
return actions;
2527
};
2628

29+
const getAfter = actionCreators => {
30+
const after = [];
31+
32+
if ( actionCreators.after ) {
33+
if ( Array.isArray( actionCreators.after )) {
34+
actionCreators.after.forEach( actionCreator => {
35+
after.push( actionCreator );
36+
});
37+
} else {
38+
after.push( actionCreators.after );
39+
}
40+
}
41+
42+
return after;
43+
};
44+
2745
const getAction = ( to, routes ) => {
2846
const { query, url } = getLocation( to );
2947
const { actionCreators, params } = getRoute( url, routes );
3048

31-
const actions = getActionList(
49+
const actions = getActions(
3250
url,
3351
actionCreators,
3452
{ ...query, ...params }
3553
);
3654

55+
const after = getAfter( actionCreators );
56+
3757
return {
3858
actions,
59+
after,
3960
type: CHANGE_PAGE_TO,
4061
url,
4162
};

src/reducers/router.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ import { CHANGE_PAGE_TO } from '../constants';
66
const router = reducers => {
77
const rootReducer = combineReducers({ ...reducers, url });
88

9+
const actionCreatorReducer = ( state, actionCreator ) => {
10+
const action = actionCreator( state );
11+
return rootReducer( state, action );
12+
};
13+
914
const batchReducer = ( state, action ) => {
1015
switch ( action.type ) {
1116
case CHANGE_PAGE_TO:
12-
return action.actions.reduce( rootReducer, state );
17+
let newState = action.actions.reduce( rootReducer, state );
18+
19+
if ( action.after.length ) {
20+
newState = action.after.reduce( actionCreatorReducer, newState );
21+
}
22+
23+
return newState;
1324
default:
1425
return rootReducer( state, action );
1526
}

test/helpers/getAction.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@ describe( 'helper', () => {
1010
it( 'should get to url and associated route actions', () => {
1111
const actionFoo = () => ({ type: 'FOO' });
1212
const actionBar = () => ({ type: 'BAR' });
13+
const actionBaz = () => ({ type: 'BAZ' });
1314

1415
const to = [ 'hello', 'world' ];
1516

1617
const routes = [
17-
[ 'hello', 'world', { foo: actionFoo, bar: actionBar }, <p>Hello world</p> ],
18+
[
19+
'hello',
20+
'world',
21+
{ foo: actionFoo, bar: actionBar, after: [ actionBaz ]},
22+
<p>Hello world</p>,
23+
],
1824
];
1925

2026
const url = '/hello/world';
2127

2228
const expectedAction = {
2329
actions: [ updateUrl( url ), actionFoo(), actionBar() ],
30+
after: [ actionBaz ],
2431
type: CHANGE_PAGE_TO,
2532
url,
2633
};

test/reducers/router.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe( 'reducer', () => {
3939

4040
expect( reducer({}, {
4141
actions: [{ type: UPDATE_URL, url: '/hello/world' }, { page: undefined }],
42+
after: [],
4243
to: [ 'hello', 'world' ],
4344
type: CHANGE_PAGE_TO,
4445
})).toEqual( expectedState );

0 commit comments

Comments
 (0)