Repository navigation
Expand file tree
/
Copy pathbackActions.js
More file actions
executable file
·62 lines (56 loc) · 1.6 KB
/
Copy pathbackActions.js
File metadata and controls
executable file
·62 lines (56 loc) · 1.6 KB
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
/**
* 记录Android物理返回按钮的事件
*/
import { fromJS } from 'immutable'
import { handleActions } from 'redux-actions'
import NavigationService from '../containers/NavigationService'
// Constants
// --------------------------------------------------
export const BACK_ACTION_SET = 'BACK_ACTION_SET'
export const BACK_ACTION_REMOVE = 'BACK_ACTION_REMOVE'
// Actions
// --------------------------------------------------
export const setBackAction = (params = {}) => async (dispatch, getState) => {
if (!params.key) {
let nav = getState().nav.toJS()
// 防止初始化时,nav为空
if (!nav || !nav.routes) {
nav = NavigationService.getTopLevelNavigator().state.nav
}
let current = nav.routes[nav.index]
while (current.routes) {
current = current.routes[current.index]
}
params.key = current.routeName
}
await dispatch({
type: BACK_ACTION_SET,
payload: params,
})
}
export const removeBackAction = (params = {}) => async (dispatch, getState) => {
if (!params.key) {
let nav = getState().nav.toJS()
let current = nav.routes[nav.index]
while (current.routes) {
current = current.routes[current.index]
}
params.key = current.routeName
}
await dispatch({
type: BACK_ACTION_REMOVE,
payload: params,
})
}
const initialState = fromJS({})
export default handleActions(
{
[`${BACK_ACTION_SET}`]: (state, { payload }) => {
return state.setIn([payload.key], fromJS(payload.action))
},
[`${BACK_ACTION_REMOVE}`]: (state, { payload }) => {
return state.removeIn([payload.key])
},
},
initialState,
)