UNPKG

@dormakaba/digital-reactnative-client

Version:
104 lines (88 loc) 2.29 kB
import { NavigationActions } from 'react-navigation'; let _container; // eslint-disable-line export function setContainer(container) { _container = container; } export function reset(routeName, params, onRoot = true) { const action = { index: 0, actions: [ NavigationActions.navigate({ type: 'Navigation/NAVIGATE', routeName: 'Drawer', params: {}, }), ], }; if (onRoot) action.key = null; _container.dispatch(NavigationActions.reset(action)); navigate(routeName, params); } export function back(key) { _container.dispatch( NavigationActions.back({ key, }) ); } export function navigate(routeName, params) { _container.dispatch( NavigationActions.navigate({ type: 'Navigation/NAVIGATE', routeName, params, }) ); } export function navigateDeep(actions) { _container.dispatch( actions.reduceRight( (prevAction, action) => NavigationActions.navigate({ type: 'Navigation/NAVIGATE', routeName: action.routeName, params: action.params, action: prevAction, }), undefined ) ); } // get getCurrentRouteName supporting drawer -> does not return DrawerOpen // but current route on DrawerClose (stupid shit router) export function getCurrentRouteForState(navigationState) { if (!navigationState) return null; const route = navigationState.routes[navigationState.index]; if (route.routes) { let goDeepTo = route; for (let i = 0; i < route.routes.length; i++) { if (route.routes[i].routeName === 'DrawerClose' && route.routes[i].routes) { goDeepTo = route.routes[i]; break; } } // dive into nested navigator (it's a navigator if routes property is present) return getCurrentRouteForState(goDeepTo); } return route; } export function getCurrentRoute() { if (!_container || !_container.state.nav) { return null; } return getCurrentRouteForState(_container.state.nav); } export function getCurrentRouteName() { const route = getCurrentRoute(); return route && route.routeName; } export default { setContainer, navigateDeep, navigate, back, reset, getCurrentRoute, getCurrentRouteName, getCurrentRouteForState, };